# How to find angle of rotation between two images

6 views (last 30 days)

Show older comments

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

I = imread('rhs.jpg');

% 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);

% figure, imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,...

% '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;

I = imread('lhs.jpg');

% 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);

% figure, imshow(imadjust(mat2gray(H)),[],'XData',theta,'YData',rho,...

% '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

##### 0 Comments

### Answers (2)

Sharvil
on 20 Oct 2013

##### 0 Comments

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!