Hello,
After detecting the lines in an image using Houghlines, how can I use it to calculate the change in angle(rotation) of the lines of a reference image?
Thanks.
Hello,
After detecting the lines in an image using Houghlines, how can I use it to calculate the change in angle(rotation) of the lines of a reference image?
Thanks.
Not sure what the Matlab implementation of the Hough transform is but the orientation of the line will be simply be at a right angle (90 degrees or pi/2 radians) to the angle you've used to identify the line in the first place.
Hope that helps. There's decent coverage of Hough transforms on the web and wikipedia is a good place to start.
Note to the reader: Since this is more of a follow-up question, make sure to refer to these first:
Now, the procedure is almost the same as I showed before. Below I am using the images from your previous question (since you provided only one, I created the other by rotating the first by 10 degrees)
We start by detecting the lines for the two images. We do this with the help of the Hough Transform functions. This what it looks like applied to the reference image (similar thing for the other):
Next we want to perform image registration using the lines endpoints as control-points. First we make sure the points correspond to each other in the two images. I do this by computing the convex hull using convhull which automatically sorts them in counterclockwise-order (or is it in the opposite direction!).
Finally, we use the function cp2tform to get the transformation matrix, which we use to align the images and extract the translation, rotation, and scaling.
The following is the complete code:
%% # Step 1: read and prepare images
%# (since you provided only one, I created the other by rotating the first).
I1 = imread('http://i182.photobucket.com/albums/x11/veronicafmy/FYP/pict1.jpg');
I1 = rgb2gray( imcrop(I1, [85 35 445 345]) ); %# get rid of white border
I2 = imrotate(I1, -10, 'bilinear', 'crop'); %# create the 2nd by rotating 10 degrees
%% # Step 2: detect the cross sign endpoints (sorted in same order)
p1 = getCross(I1);
p2 = getCross(I2);
%% # Step 3: perform Image Registration
%# find transformation that maps I2 to I1 using the 4 control points for each
t = cp2tform(p2,p1,'affine');
%# transform I2 to be aligned with I1
II2 = imtransform(I2, t, 'XData',[1 size(I1,2)], 'YData',[1 size(I1,1)]);
%# plot
figure('menu','none')
subplot(131), imshow(I1), title('I1')
subplot(132), imshow(I2), title('I2')
subplot(133), imshow(II2), title('I2 (aligned)')
%# recover affine transformation params (translation, rotation, scale)
ss = t.tdata.Tinv(2,1);
sc = t.tdata.Tinv(1,1);
tx = t.tdata.Tinv(3,1);
ty = t.tdata.Tinv(3,2);
scale = sqrt(ss*ss + sc*sc)
rotation = atan2(ss,sc)*180/pi
translation = [tx ty]
and here's the function that extract the lines endpoints:
function points = getCross(I)
%# get edges (simply by thresholding)
I = imfilter(I, fspecial('gaussian', [7 7], 1), 'symmetric');
BW = imclearborder(~im2bw(I, 0.5));
%# Hough Transform
[H T R] = hough(BW);
%# detect peaks
P = houghpeaks(H, 2);
%# detect lines
lines = houghlines(BW, T, R, P);
%# sort 2D points in counterclockwise order
points = [vertcat(lines.point1); vertcat(lines.point2)];
idx = convhull(points(:,1), points(:,2));
points = points(idx(1:end-1),:);
end
with the result:
scale =
1.0025
rotation =
-9.7041
translation =
32.5270 -38.5021
(Note in our example there was translation because rotation was not around the center of the cross sign).