Find best rotation angle for alignment
10 views (last 30 days)
Show older comments
I have many images which look as the example attached. I wish to find the optimal rotation angle (for each image) such that the two bright lines would be perpendicular to the ground.
I tried by optimizing the vertical+horizontal variance of the Fourier transform of the image, but have to manually check every angle.
I'm searching for a general algorithm which will calculate it without going over all the options (like a fit function).

0 Comments
Answers (2)
Cam Salzberger
on 31 Aug 2017
Hello Alex,
So I haven't actually played much with the Hough transformation before, but I believe it can be useful to you here. It can help you to determine the orientation of the strongest lines in the image. Here's something that I just whipped up, but you can play with it to see if it'll do what you need:
% Get the image
I = imread('8.png');
I2 = imrotate(I,30,'crop');
figure;
imshow(I2)
% Find all the edges in the image
BW = edge(I2,'canny');
% Find the Hough information about the lines
[H, theta, rho] = hough(BW);
If you drew a line from the origin to the closest point on the detected line, the rho would be the distance from origin to point, and theta would be the angle of the drawn line from origin to point. So you just need the theta values corresponding to the strongest lines. These theta values will be perpendicular to the lines, so it can tell you how much to rotate the image.
% Get the strongest line
% First column is rho index, second is theta index
P = houghpeaks(H,1);
% Find out how much it needs to rotate
thetaPeak = theta(P(1,2));
% Fix the image
I3 = imrotate(I2,thetaPeak);
figure
imshow(I3)
You may need to deal with lines that are 180 degrees around or things like that, but this should hopefully help you get started. Here is the demo that I used as a basis.
-Cam
0 Comments
Matt J
on 31 Aug 2017
Edited: Matt J
on 31 Aug 2017
REGIONPROPS might also serve your purpose in this case
I = imread('8.png');
I2 = imrotate(I,30,'crop');
R=regionprops(bwareafilt(I2>50,2),'Orientation');
x=mean([R.Orientation]);
result=(x-90*sign(x))
1 Comment
Cam Salzberger
on 31 Aug 2017
Oh, I forgot about orientation. Good call! That's much simpler than the Hough transform.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!