Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Problem about using phase correlation to find rotation angle

Subject: Problem about using phase correlation to find rotation angle

From: Ja-yun

Date: 29 May, 2010 11:57:06

Message: 1 of 6

I have two template
1. http://img541.imageshack.us/img541/5199/dog90.png
2. http://img231.imageshack.us/img231/7269/dogk.png
fig 2 is fig 1 rotated by degree 90.
I want to find the rotation angle between them by phase correlation.
so far i have these code.
%%==============================
fig1=rgb2gray(imread('dog.bmp'));
fig2=rgb2gray(imread('dog90.bmp'));
F1=fft2(double(fig1));
F2=fft2(double(fig2));

PF1=imgpolarcoord(F1); % Transform F1,F2 into polar coordinates system
PF2=imgpolarcoord(F2); % P(radius,degree)

PhaseCorr=(PF1.*conj(PF2))./(abs(F3.*F4));
result=ifft2(double(PhaseCorr));
result=abs(result);
%%===============================
I plot the result and got this
http://img175.imageshack.us/img175/5299/phasecorr.jpg
The peak values appear at each cornor of this fig
Is it because my code's wrong or the process?
or it's because this method's not suitable for my fig?(OMG)
Can somebody help me, i've been stuck with this for a week

Subject: Problem about using phase correlation to find rotation angle

From: Matt J

Date: 29 May, 2010 12:30:22

Message: 2 of 6

"Ja-yun " <silodraw@gmail.com> wrote in message <htqvei$7r0$1@fred.mathworks.com>...
 
> PhaseCorr=(PF1.*conj(PF2))./(abs(F3.*F4));
===============


Shouldn't this be

PhaseCorr=(PF1.*conj(PF2))./(abs(PF1.*PF2));


> The peak values appear at each cornor of this fig
> Is it because my code's wrong or the process?
=======

help fftshift

Subject: Problem about using phase correlation to find rotation angle

From: Ja-yun

Date: 29 May, 2010 13:29:09

Message: 3 of 6

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <htr1cu$9pa$1@fred.mathworks.com>...
> "Ja-yun " <silodraw@gmail.com> wrote in message <htqvei$7r0$1@fred.mathworks.com>...
>
> > PhaseCorr=(PF1.*conj(PF2))./(abs(F3.*F4));
> ===============
>
>
> Shouldn't this be
>
> PhaseCorr=(PF1.*conj(PF2))./(abs(PF1.*PF2));
thank you for correcting my code,you're right about it
>
>
> > The peak values appear at each cornor of this fig
> > Is it because my code's wrong or the process?
> =======
>
> help fftshift
you mean like this?
F1=fftshift(fft2(double(fig1)));
F2=fftshift(fft2(double(fig2)));
.
.
.
I tried and got this
http://img180.imageshack.us/img180/9162/fftshift.jpg
I still can't see anything about "90 degrees"
did i misunderstand what you meant ?
thank you for your help

Subject: Problem about using phase correlation to find rotation angle

From: Matt J

Date: 29 May, 2010 15:24:21

Message: 4 of 6

The following seems to work


fig1=256-double( rgb2gray(imread('dogk.png')) );
fig2=256-double( rgb2gray(imread('dog90.png')) );

F1=radon(fig1);
F2=radon(fig2);

supp=num2cell(size(F1));
suppc=num2cell(size(F1)+size(F2));
PF1=fft2(F1,suppc{:});
PF2=fft2(F2,suppc{:});

den=abs(PF1.*PF2);

PhaseCorr=(PF1.*conj(PF2))./den;PhaseCorr(den==0)=0;


result=ifft2(PhaseCorr,supp{:});
result=abs(result);

Subject: Problem about using phase correlation to find rotation angle

From: Ja-yun

Date: 30 May, 2010 03:14:06

Message: 5 of 6

"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <htrbj5$e1q$1@fred.mathworks.com>...
> The following seems to work
>
>
> fig1=256-double( rgb2gray(imread('dogk.png')) );
> fig2=256-double( rgb2gray(imread('dog90.png')) );
>
> F1=radon(fig1);
> F2=radon(fig2);
>
> supp=num2cell(size(F1));
> suppc=num2cell(size(F1)+size(F2));
> PF1=fft2(F1,suppc{:});
> PF2=fft2(F2,suppc{:});
>
> den=abs(PF1.*PF2);
>
> PhaseCorr=(PF1.*conj(PF2))./den;PhaseCorr(den==0)=0;
>
>
> result=ifft2(PhaseCorr,supp{:});
> result=abs(result);
wow,it works!
Are the backgrounds of two figs have to be black?
and i don't quite understand what these steps mean
can you please explain these to me?
%%
 supp=num2cell(size(F1));
 suppc=num2cell(size(F1)+size(F2));
 PF1=fft2(F1,suppc{:});
                    ^^^^^^
 PF2=fft2(F2,suppc{:});
. ^^^^^^
.
 result=ifft2(PhaseCorr,supp{:});
                                   ^^^^^
%%
why fft2 PF1 and PF2 in suppc and ifft2 PhaseCorr in supp?
if i want to obtain a angle ranges from 0 to 360 degrees
how should I modify the code?
thank you so much!

Subject: Problem about using phase correlation to find rotation angle

From: Matt J

Date: 30 May, 2010 18:57:05

Message: 6 of 6

"Ja-yun " <silodraw@gmail.com> wrote in message <htsl5u$p3j$1@fred.mathworks.com>...

> Are the backgrounds of two figs have to be black?
=========

Yes, as Roger told you in the other thread you posted

http://www.mathworks.com/matlabcentral/newsreader/view_thread/283299

if the backgrounds are not black, the background will contribute to the radon transform, making the algorithm think that the object in the image consists of both the dog and the white area around it.

You don't want that, because the background white area does not rotate with the dog. It fills the same unrotated square region in both images, which will confuse the algorithm.


> and i don't quite understand what these steps mean
===============

Forget it. I had a number of mistakes. Here is a better version of the code, which also allows you to detect rotations over a full 360 degrees


clear, close all
fig2=255-double( rgb2gray(imread('dogk.png')) );
fig1=255-double( rgb2gray(imread('dog90.png')) );


fig1=fig1/norm(fig1(:)); fig2=fig2/norm(fig2(:));

F1=radon(fig1,0:359);
F2=radon(fig2,0:359);


PF1=fft2(F1);
PF2=fft2(F2);

den=abs(PF1.*PF2); %den=1;

PhaseCorr=(PF2.*conj(PF1))./den; PhaseCorr(den==0)=0;


result=ifft2(PhaseCorr);


result=abs(result);
figure(3); imdisp(result)

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us