# matlab - Find the lowest and rightmost pixel to find angle of image

I am trying to apply an imrotate to an image however the angle of the image is unknown and changes with each image.

If i have converted the image to binary would it be possible to find the lowest y position '1' and the leftmost'1' and use the gradient/angle between them as the angle for my image rotate?

for example:

binary positions diagram

Using the angle between these two positions and aligning it with the x axis?

Current Progress - converted to binary and made the edges more distinguishable:

``% convert to binarygreyImage = rgb2gray(C); % greyscalecannyImage = edge(greyImage, 'canny'); % canny edge detection% fill the gaps in the shapese = strel('disk',2);bw = imclose(cannyImage, se);filled = imfill(bw, 'holes');imshow(filled);[~,lowerMostCol] = max(cumsum(sum(filled,2)));[~,leftMostRow] = max(sum(filled,1)==1);``

Approach #1

With `a` as the binary image, you could do something like this -

``````[~,lowermost] = max(cumsum(sum(a,2)));
lowermostpt = [lowermost,find(a(lowermost,:),1,'first')]

[~,rightmost] = max(cumsum(sum(a,1)));
rightmostpt = [find(a(:,rightmost),1,'first'),rightmost]

[~,topmost] = max(sum(a,2)==1);
topmostpt = [topmost,find(a(topmost,:),1,'first')]

[~,leftmost] = max(sum(a,1)==1);
leftmostpt = [find(a(:,leftmost),1,'first'),leftmost]
``````

For performance efficiency, it might be a good idea to store the summations once and re-use later on.

Sample run -

``````a =
0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0     0     1     0     0     0     0     0
0     0     0     0     0     0     0     0     1     1     1     0     0     0     0
0     0     0     0     0     0     0     1     1     1     1     1     0     0     0
0     0     0     0     0     0     1     1     1     1     1     1     1     0     0
0     0     0     0     0     1     1     1     1     1     1     1     1     1     0
0     0     0     0     0     0     1     1     1     1     1     1     1     0     0
0     0     0     0     0     0     0     1     1     1     1     1     0     0     0
0     0     0     0     0     0     0     0     1     1     1     0     0     0     0
0     0     0     0     0     0     0     0     0     1     0     0     0     0     0
0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
0     0     0     0     0     0     0     0     0     0     0     0     0     0     0
lowermostpt =
12    10
rightmostpt =
8    14
topmostpt =
4    10
leftmostpt =
8     6
``````

Approach #2 Using `bwboundaries` from Image-processing toolbox -

``````idx = cell2mat(bwboundaries(a))

[~,p1] = min(idx(:,1))
topmostpt = idx(p1,:)
[~,p2] = max(idx(:,1))
lowermostpt = idx(p2,:)

[~,p3] = min(idx(:,2))
leftmostpt = idx(p3,:)
[~,p4] = max(idx(:,2))
rightmostpt = idx(p4,:)
``````