Christina
Christina

Reputation: 935

Diagonal pixels of an image in MATLAB

I have an image of size 30 x 100 pixels.

  1. How can I count how many pixels are there on the diagonal? can I just apply the Pythagorean theorem? but if yes, we may obtain float number of pixels. So how can I address this issue?

  2. In addition, how can I extract automatically the x and y coordinate values of each pixel on the diagonal?

I have written a MATLAB code for a square matrix which is very easy. But how can I generalize my code to include images of any size (not only square images)..

%suppose an image of size 100 x 100
image = rand(100,100);
n = length(image);

%extract how many pixels are there on the diagonal
diagonal_pixels = sqrt(n^2 + n^2);

%Get the x and y coordinates values of each diagonal pixel
for i = 1: n
x_coordinate_diag(i) = i;
y_coordinate_diag(i) = i;
end 

Upvotes: 1

Views: 216

Answers (1)

phoenixstudio
phoenixstudio

Reputation: 2598

You could use these steps to get the diagonal pixels

  1. Get min width and height length (suppose it is the width)
  2. Loop through the min lenght(ex: width)
  3. get height cordinate by rounding i * height / width

For example 50 x 100

  1. Min is 50
  2. for i = 0, j = 0 x 100 / 50 = 0
  3. for i = 1, j = 1 x 100 / 50 = 2
  4. for i = 2, j = 2 x 100 / 50 = 4
  5. ...
  6. for i = 50, j = 50 x 100 / 50 = 100

Upvotes: 2

Related Questions