# How to find angle of rotation between two images

15 views (last 30 days)
Karan on 16 Aug 2012
Answered: Sharvil on 20 Oct 2013
Hi I'm using matlab to try and detect the angle by which the rectangles are inclined in the image. The code I used so far is below. I need to find the rotation between two images so I tried rotating one image and comparing it with the other image but that doesn't work as the image dimensions change on rotating. I know I need to use hough transform but can't figure out how. I have attached the three images i am using as lhs.jpg, center.jpg and rhs.jpg depending on the view the picture has been taken from.
clc;
clear all;
%%Procedure for RHS
% creating RGB matrices and storing dimensions
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
size = size(R);
rows = size(1,1);
columns = size(1,2);
% Manipulating pixels
for i = 1:rows
for j = 1:columns
if B(i,j) <= 70;
I(i,j,:) = 255;
end
end
end
figure, imshow(I)
%edge detection
I = rgb2gray(I);
I = imadjust(I, [0.3 0.9], [0 1]);
BW1 = edge(I,'canny',0.15);
figure, imshow(BW1);
[H,theta,rho] = hough(BW1);
% 'InitialMagnification','fit');
xlabel('\theta (degrees)'), ylabel('\rho');
axis on, axis normal, hold on;
colormap(hot)
P = houghpeaks(H,75,'threshold',ceil(0.11*max(H(:))));
lines = houghlines(BW1,theta,rho,P,'FillGap',5,'MinLength',5);
figure, imshow(BW1), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',3,'Color','green');
end
%%Procedure for LHS
clear I;
clear R;
clear G;
clear B;
clear size;
% creating RGB matrices and storing dimensions
R = I(:,:,1);
G = I(:,:,2);
B = I(:,:,3);
size = size(R);
rows = size(1,1);
columns = size(1,2);
% Manipulating pixels
for i = 1:rows
for j = 1:columns
if B(i,j) <= 70;
I(i,j,:) = 255;
end
end
end
figure, imshow(I)
%edge detection
I = rgb2gray(I);
I = imadjust(I, [0.3 0.9], [0 1]);
BW2 = edge(I,'canny',0.15);
figure, imshow(BW2);
[H,theta,rho] = hough(BW2);
% 'InitialMagnification','fit');
xlabel('\theta (degrees)'), ylabel('\rho');
axis on, axis normal, hold on;
colormap(hot)
P = houghpeaks(H,75,'threshold',ceil(0.11*max(H(:))));
lines = houghlines(BW2,theta,rho,P,'FillGap',5,'MinLength',5);
figure, imshow(BW2), hold on
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',3,'Color','green');
end
%%Finding angle of rotation by rotating by trial and error angles
diff = BW1 - BW2;
for i = 1:180
BW1 = imrotate(BW1,i);
newdiff = BW1-BW2;
if newdiff < diff
diff = newdiff;
angle = i;
end
end

Sean de Wolski on 16 Aug 2012
##### 3 CommentsShow 1 older commentHide 1 older comment
Sean de Wolski on 16 Aug 2012
It was introduced in R2012a.
Karan on 20 Aug 2012
I'm using 2011. Suggestions based on that please.

Sharvil on 20 Oct 2013
Heyy..This code is perfect but it shows an error in the last if condidion that : "Matrix dimensions must be same" Can you please resolve this error?