image-processingrust# Get diagonal pixels from rectangular image

I have a 1920x1080 input image and want to analyze luminescence distribution over the diagonal. I could use just the middle row and "stretch" it as needed as an alternative, but I would like to stick with the actual diagonal if possible.

My current approach picks pixels calculated via dimension factors over the diagonal based on Pythagoras. This results in around 170 duplicates, I assume because of rounding.

Is there a proper way to achieve this? The input is always going to be in this format but of variable resolution.

```
let num_rows = img.height();
let num_cols = img.width();
let diag_steps = ((num_cols.pow(2) + num_rows.pow(2)) as f32)
.sqrt()
.floor() as u32;
let col_factor = num_cols as f32 / diag_steps as f32;
let row_factor = num_rows as f32 / diag_steps as f32;
let mut diag_values = vec![];
for s in 0..diag_steps {
let r = ((s as f32 * row_factor).round() as u32).min(num_rows - 1);
let c = ((s as f32 * col_factor).round() as u32).min(num_cols - 1);
diag_values.push(c, r);
}
```

This approach is purely based on what I thought should work and I am using the image crate for reading/writing images. I found a related question on SO but the answer does not help me, I need the actual diagonal through the middle from top-right to bottom-left, not a diagonal where one sets Y = X.

I also do averaging over pixels around each coordinate to account a bit for noise but that's not important for the duplicates.

I tried sampling the image like listed in the code block and expected to get pixels along the diagonal of an image. I got the expected amount of 2202 pixels, but these contained duplicates due to rounding and I assume this could lead to further problems later on.

Solution

You can step by the aspect ratio, like so:

```
fn greatest_common_factor(rhs: u32, lhs: u32) -> u32 {
todo!("Implement me or import a crate for me")
}
fn simplify_fraction(rhs: u32, lhs: u32) -> (u32, u32) {
let gcf = greatest_common_factor(rhs, lhs);
(rhs / gcf, lhs / gcf)
}
fn top_left_to_bottom_right<I>(image: &I) -> impl Iterator<Item = I::Pixel> + '_
where
I: GenericImageView,
{
let width = image.width();
let height = image.height();
let (run, rise) = simplify_fraction(width, height);
let x = (0..width).into_iter().step_by(run as usize);
//start from top right instead?
//let x = (0..width).into_iter().step_by(run as usize).rev();
let y = (0..height).into_iter().step_by(rise as usize);
let coords = x.zip(y);
coords.map(|(x, y)| image.get_pixel(x, y))
}
```

These are the *exact* pixels that fall on the *exact* diagonal line.

This obviously has the disadvantage of using whole numbers for the aspect ratio. The consequence is an image of 1920x1080 will have an aspect ratio of 16/9. Because we step by 16 and 9 pixels, respectively, we are skipping some pixels.

This function outputs 120 pixels for a 1920x1080 image (the gcf). If this is undesirable, you will need to tackle sub pixel manipulation and computation with mixed numbers (E.g. what is 1.5 pixels? Does the image crate support this out of the box? Would you round floats to integers and not care about uniformity?). You can then, either extrapolate points between your gcf number of points, or calculate your aspect ratio as a ratio of floats and use slopes as shown above.

- Imagemagick on linux to convert EMF to PNG
- What are the inaccuracies of this 'inverse map' function in OpenCV?
- Fast Bitmap Blur For Android SDK
- Inverting a real-valued index grid
- OpenCV check powder level
- Laserline in freespace... and how I can detect the Line for further calculations?
- How do I process an image using Python
- Discrete Wavelet Transform (Daubechies wavelet) of an array complex numbers
- Python Image - Finding largest branch from image skeleton
- OpenCV find contours of an RGBA image not working
- How can I locate something on my screen quickly in Python?
- I wonder how reverse image search services like tineye.com work ...?
- How to check whether a JPEG image is color or gray scale using only Python stdlib
- SSIM (Structural similarity index measure) performance
- Is there a way i can detect the image orientation and rotate the image to the right angle?
- How to understand the dimensionality of 3D MRI or Nifti images?
- How to modify patches made by patchify?
- Create 12 multichannel image starting from single RBG pictures
- Perform Cubic Spline Interpolation in an image Python
- Improving AI Image Upscale
- Random forest for image resize on python
- Use FFmpeg to resize image
- How to find the dominant/most common color in an image?
- Can ImageMagick return the image size?
- How do you generate an image where each pixel is a random color in python
- How do I efficiently segment 2D images into regions/blobs of similar values?
- How to find PSNR from two images with different size in python
- How to Divide Relative Distance into Three Equidistant Regions in MATLAB?
- selecting region of interest
- How to calculate the distance between object and camera, knowing the pixels occupied by the object in an image- segmentation