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:
Image Processing (phase correlation)

Subject: Image Processing (phase correlation)

From: Brad White

Date: 20 Feb, 2008 20:36:01

Message: 1 of 11

I am trying to perform the steps necessary to do a phase
correlation between two images in order to determine the
amount of shift between them. These steps according to an
article on wikipedia about phase correlation are:
or at http://en.wikipedia.org/wiki/Phase_correlation

1) apply a window function to images (hamming window) for
edge effects.
2)then do a 2d fast fourier transformation on one image and
the complex conjugate of a 2d fast fourier transformation
on the other.
3)then multiply them by each other and divide by the
absolute value of them multiplied by each other.
4) do the inverse fast fourier transformation of that
result and then determine the location of the peak.

I belive my program isn't working right because i can't
seem to get a hamming window to work properly because it
leaves me with multiplication errors with the matrices.
I've tried many variations with my code but i can't seem to
get anything to work out right.
And i know i should be getting a black image with a bright
circle corresponding to the shift but i'm not getting
anything close to that.

Any help will be greatly appreciated.

What I have so far is:

image1 = imread('c:\....etc);
image2 = imread('c:\....etc);
Image1 = double(image1)*hamming(length(image1));
Image2 = double(image1)*hamming(length(image2));

FFT1 = fft2(double(Image1));
FFT2 = conj(fft2(double(Image2)));
FFTR = FFT1*FFT2;
FFTRN = (FFTR/abs((FFT1*FFT2)));
result = ifft2(double(FFTRN));

Subject: Image Processing (phase correlation)

From: Dave Robinson

Date: 21 Feb, 2008 09:35:03

Message: 2 of 11

"Brad White" <brad.white@mathworks.com> wrote in message
<fpi2vh$g8q$1@fred.mathworks.com>...
> I am trying to perform the steps necessary to do a phase
> correlation between two images in order to determine the
> amount of shift between them. These steps according to an
> article on wikipedia about phase correlation are:
> or at http://en.wikipedia.org/wiki/Phase_correlation
>
> 1) apply a window function to images (hamming window) for
> edge effects.
> 2)then do a 2d fast fourier transformation on one image and
> the complex conjugate of a 2d fast fourier transformation
> on the other.
> 3)then multiply them by each other and divide by the
> absolute value of them multiplied by each other.
> 4) do the inverse fast fourier transformation of that
> result and then determine the location of the peak.
>
> I belive my program isn't working right because i can't
> seem to get a hamming window to work properly because it
> leaves me with multiplication errors with the matrices.
> I've tried many variations with my code but i can't seem to
> get anything to work out right.
> And i know i should be getting a black image with a bright
> circle corresponding to the shift but i'm not getting
> anything close to that.
>
> Any help will be greatly appreciated.
>
> What I have so far is:
>
> image1 = imread('c:\....etc);
> image2 = imread('c:\....etc);
> Image1 = double(image1)*hamming(length(image1));
> Image2 = double(image1)*hamming(length(image2));
>
> FFT1 = fft2(double(Image1));
> FFT2 = conj(fft2(double(Image2)));
> FFTR = FFT1*FFT2;
> FFTRN = (FFTR/abs((FFT1*FFT2)));
> result = ifft2(double(FFTRN));

Have you tried

FFTR = FFT1.*FFT2;

and

FFTRN = (FFTR/abs((FFT1.*FFT2)));

You appear to be doing a matrix multiplication rather than a
frequency by frequency multiplication.

Regards

Dave Robinson

Subject: Image Processing (phase correlation)

From: Brad

Date: 21 Feb, 2008 14:41:03

Message: 3 of 11

"Dave Robinson" <dave.robinson@somewhere.biz> wrote in
message <fpjgk7$qje$1@fred.mathworks.com>...
> "Brad White" <brad.white@mathworks.com> wrote in message
> <fpi2vh$g8q$1@fred.mathworks.com>...
> > I am trying to perform the steps necessary to do a
phase
> > correlation between two images in order to determine
the
> > amount of shift between them. These steps according to
an
> > article on wikipedia about phase correlation are:
> > or at http://en.wikipedia.org/wiki/Phase_correlation
> >
> > 1) apply a window function to images (hamming window)
for
> > edge effects.
> > 2)then do a 2d fast fourier transformation on one image
and
> > the complex conjugate of a 2d fast fourier
transformation
> > on the other.
> > 3)then multiply them by each other and divide by the
> > absolute value of them multiplied by each other.
> > 4) do the inverse fast fourier transformation of that
> > result and then determine the location of the peak.
> >
> > I belive my program isn't working right because i can't
> > seem to get a hamming window to work properly because
it
> > leaves me with multiplication errors with the matrices.
> > I've tried many variations with my code but i can't
seem to
> > get anything to work out right.
> > And i know i should be getting a black image with a
bright
> > circle corresponding to the shift but i'm not getting
> > anything close to that.
> >
> > Any help will be greatly appreciated.
> >
> > What I have so far is:
> >
> > image1 = imread('c:\....etc);
> > image2 = imread('c:\....etc);
> > Image1 = double(image1)*hamming(length(image1));
> > Image2 = double(image1)*hamming(length(image2));
> >
> > FFT1 = fft2(double(Image1));
> > FFT2 = conj(fft2(double(Image2)));
> > FFTR = FFT1*FFT2;
> > FFTRN = (FFTR/abs((FFT1*FFT2)));
> > result = ifft2(double(FFTRN));
>
> Have you tried
>
> FFTR = FFT1.*FFT2;
>
> and
>
> FFTRN = (FFTR/abs((FFT1.*FFT2)));
>
> You appear to be doing a matrix multiplication rather
than a
> frequency by frequency multiplication.
>
> Regards
>
> Dave Robinson


Thanks Dave it worked out great...

Brad

Subject: Image Processing (phase correlation)

From: fido genial

Date: 19 Mar, 2008 02:28:02

Message: 4 of 11

"Brad " <brad.white@mathworks.com> wrote in message
<fpk2hv$65p$1@fred.mathworks.com>...
> "Dave Robinson" <dave.robinson@somewhere.biz> wrote in
> message <fpjgk7$qje$1@fred.mathworks.com>...
> > "Brad White" <brad.white@mathworks.com> wrote in message
> > <fpi2vh$g8q$1@fred.mathworks.com>...
> > > I am trying to perform the steps necessary to do a
> phase
> > > correlation between two images in order to determine
> the
> > > amount of shift between them. These steps according to
> an
> > > article on wikipedia about phase correlation are:
> > > or at http://en.wikipedia.org/wiki/Phase_correlation
> > >
> > > 1) apply a window function to images (hamming window)
> for
> > > edge effects.
> > > 2)then do a 2d fast fourier transformation on one image
> and
> > > the complex conjugate of a 2d fast fourier
> transformation
> > > on the other.
> > > 3)then multiply them by each other and divide by the
> > > absolute value of them multiplied by each other.
> > > 4) do the inverse fast fourier transformation of that
> > > result and then determine the location of the peak.
> > >
> > > I belive my program isn't working right because i can't
> > > seem to get a hamming window to work properly because
> it
> > > leaves me with multiplication errors with the matrices.
> > > I've tried many variations with my code but i can't
> seem to
> > > get anything to work out right.
> > > And i know i should be getting a black image with a
> bright
> > > circle corresponding to the shift but i'm not getting
> > > anything close to that.
> > >
> > > Any help will be greatly appreciated.
> > >
> > > What I have so far is:
> > >
> > > image1 = imread('c:\....etc);
> > > image2 = imread('c:\....etc);
> > > Image1 = double(image1)*hamming(length(image1));
> > > Image2 = double(image1)*hamming(length(image2));
> > >
> > > FFT1 = fft2(double(Image1));
> > > FFT2 = conj(fft2(double(Image2)));
> > > FFTR = FFT1*FFT2;
> > > FFTRN = (FFTR/abs((FFT1*FFT2)));
> > > result = ifft2(double(FFTRN));
> >
> > Have you tried
> >
> > FFTR = FFT1.*FFT2;
> >
> > and
> >
> > FFTRN = (FFTR/abs((FFT1.*FFT2)));
> >
> > You appear to be doing a matrix multiplication rather
> than a
> > frequency by frequency multiplication.
> >
> > Regards
> >
> > Dave Robinson
>
>
> Thanks Dave it worked out great...
>
> Brad
>

Hi Brad

Is this code working...
I could see only black image without any white dot for
images with motion.

Let know if any changes are required to the code.

thanks
fido

Subject: Image Processing (phase correlation)

From: poonam jadhav

Date: 11 Apr, 2008 09:10:07

Message: 5 of 11

"Brad White" <brad.white@mathworks.com> wrote in message
<fpi2vh$g8q$1@fred.mathworks.com>...
> I am trying to perform the steps necessary to do a phase
> correlation between two images in order to determine the
> amount of shift between them. These steps according to an
> article on wikipedia about phase correlation are:
> or at http://en.wikipedia.org/wiki/Phase_correlation
>
> 1) apply a window function to images (hamming window) for
> edge effects.
> 2)then do a 2d fast fourier transformation on one image and
> the complex conjugate of a 2d fast fourier transformation
> on the other.
> 3)then multiply them by each other and divide by the
> absolute value of them multiplied by each other.
> 4) do the inverse fast fourier transformation of that
> result and then determine the location of the peak.
>
> I belive my program isn't working right because i can't
> seem to get a hamming window to work properly because it
> leaves me with multiplication errors with the matrices.
> I've tried many variations with my code but i can't seem to
> get anything to work out right.
> And i know i should be getting a black image with a bright
> circle corresponding to the shift but i'm not getting
> anything close to that.
>
> Any help will be greatly appreciated.
>
> What I have so far is:
>
> image1 = imread('c:\....etc);
> image2 = imread('c:\....etc);
> Image1 = double(image1)*hamming(length(image1));
> Image2 = double(image1)*hamming(length(image2));
>
> FFT1 = fft2(double(Image1));
> FFT2 = conj(fft2(double(Image2)));
> FFTR = FFT1*FFT2;
> FFTRN = (FFTR/abs((FFT1*FFT2)));
> result = ifft2(double(FFTRN));

Subject: Image Processing (phase correlation)

From: poonam jadhav

Date: 11 Apr, 2008 09:13:08

Message: 6 of 11

Hi;
i am using the same code for finding out the shift between
the two images. for that i have to plot the ifft,to find the
location of peak.
i.e.To plot "result" in the code you have given ,but i am
not getting it,pls help me regarding this.





"Brad White" <brad.white@mathworks.com> wrote in message
<fpi2vh$g8q$1@fred.mathworks.com>...
> I am trying to perform the steps necessary to do a phase
> correlation between two images in order to determine the
> amount of shift between them. These steps according to an
> article on wikipedia about phase correlation are:
> or at http://en.wikipedia.org/wiki/Phase_correlation
>
> 1) apply a window function to images (hamming window) for
> edge effects.
> 2)then do a 2d fast fourier transformation on one image and
> the complex conjugate of a 2d fast fourier transformation
> on the other.
> 3)then multiply them by each other and divide by the
> absolute value of them multiplied by each other.
> 4) do the inverse fast fourier transformation of that
> result and then determine the location of the peak.
>
> I belive my program isn't working right because i can't
> seem to get a hamming window to work properly because it
> leaves me with multiplication errors with the matrices.
> I've tried many variations with my code but i can't seem to
> get anything to work out right.
> And i know i should be getting a black image with a bright
> circle corresponding to the shift but i'm not getting
> anything close to that.
>
> Any help will be greatly appreciated.
>
> What I have so far is:
>
> image1 = imread('c:\....etc);
> image2 = imread('c:\....etc);
> Image1 = double(image1)*hamming(length(image1));
> Image2 = double(image1)*hamming(length(image2));
>
> FFT1 = fft2(double(Image1));
> FFT2 = conj(fft2(double(Image2)));
> FFTR = FFT1*FFT2;
> FFTRN = (FFTR/abs((FFT1*FFT2)));
> result = ifft2(double(FFTRN));

Subject: Image Processing (phase correlation)

From: khoo

Date: 7 May, 2009 16:04:01

Message: 7 of 11

bro, can update how your code?? i use your code also can't get the peak value and got black image without any white dot..

Subject: Image Processing (phase correlation)

From: bryan cahiles

Date: 12 Dec, 2010 05:39:05

Message: 8 of 11

"khoo" <jim_khoo@hotmail.com> wrote in message <gtv0ph$69s$1@fred.mathworks.com>...
> bro, can update how your code?? i use your code also can't get the peak value and got black image without any white dot..

please update the code

image1 = imread('a.jpg');
image2 = imread('aa.jpg');
Image1 = double(image1)*hamming(length(image1));
Image2 = double(image1)*hamming(length(image2));

FFT1 = fft2(double(Image1));
FFT2 = conj(fft2(double(Image2)));
FFTR = FFT1.*FFT2;
FFTRN = (FFTR/abs((FFT1.*FFT2)));
result = ifft2(double(FFTRN));


i have an error it says :

??? Error using ==> mtimes
Input arguments must be 2-D.

Subject: Image Processing (phase correlation)

From: Mohammad

Date: 13 Aug, 2011 00:47:29

Message: 9 of 11

HERE IS A MAJOR TYPO!!!

FFTRN = (FFTR/abs((FFT1*FFT2)));

SHOULD BE ->

FFTRN = (FFTR./abs((FFT1*FFT2)));
 
(notice how FFTR has a period after it...)

Subject: Image Processing (phase correlation)

From: Mohammed

Date: 21 Jan, 2013 07:09:08

Message: 10 of 11

"Brad" wrote in message <fpi2vh$g8q$1@fred.mathworks.com>...
> I am trying to perform the steps necessary to do a phase
> correlation between two images in order to determine the
> amount of shift between them. These steps according to an
> article on wikipedia about phase correlation are:
> or at http://en.wikipedia.org/wiki/Phase_correlation
>
> 1) apply a window function to images (hamming window) for
> edge effects.
> 2)then do a 2d fast fourier transformation on one image and
> the complex conjugate of a 2d fast fourier transformation
> on the other.
> 3)then multiply them by each other and divide by the
> absolute value of them multiplied by each other.
> 4) do the inverse fast fourier transformation of that
> result and then determine the location of the peak.
>
> I belive my program isn't working right because i can't
> seem to get a hamming window to work properly because it
> leaves me with multiplication errors with the matrices.
> I've tried many variations with my code but i can't seem to
> get anything to work out right.
> And i know i should be getting a black image with a bright
> circle corresponding to the shift but i'm not getting
> anything close to that.
>
> Any help will be greatly appreciated.
>
> What I have so far is:
>
> image1 = imread('c:\....etc);
> image2 = imread('c:\....etc);
> Image1 = double(image1)*hamming(length(image1));
> Image2 = double(image1)*hamming(length(image2));
>
> FFT1 = fft2(double(Image1));
> FFT2 = conj(fft2(double(Image2)));
> FFTR = FFT1*FFT2;
> FFTRN = (FFTR/abs((FFT1*FFT2)));
> result = ifft2(double(FFTRN));

NO WHITE DOT. ONLY BLACK IMAGE. URGENT HELP NEEDED

Subject: Image Processing (phase correlation)

From: Todd

Date: 22 Jan, 2013 20:22:05

Message: 11 of 11

I have just tried to fix up the post so the code is useable first with an example on a single translated imaage and then on a more general two image input case. Here is working code for an example that uses a single image and cuts out a 256x256 pixel portion of the image from one location and then cuts out the same size portion from a location that has been translated by 11 pixels in the x direction and 7 pixels in the y direction. This shows that the basic technique does work when you have the code formatting correct.

image1 = rgb2gray(imread('frame429.jpg')); % read in image and convert from RGB ro grayscale
rect1 = [1 1 255 255]; % define first cutout region [xmin ymin width height]
rect2 = [12 8 255 255]; % define second translated cutout region [xmin ymin width height]
sub_image1 = imcrop(image1,rect1); % 1st cut out portion of original image
sub_image2 = imcrop(image1,rect2); % 2nd cut out portion of original image
filter1=blackman(length(sub_image1),'symmetric');
filtimg1=(filter1*filter1');
Image1=double(sub_image1).*filtimg1;
Image2=double(sub_image2).*filtimg1
FFT1 = fft2(Image1);
FFT2 = conj(fft2(Image2));
FFTR = FFT1.*FFT2;
magFFTR = abs(FFTR);
FFTRN = (FFTR./magFFTR);
result = ifft2(double(FFTRN));
figure;
colormap('gray'); % choose colormap for plotting
imagesc(result);
figure;
colormap(jet);
mesh(result);

The code can easily be changed to use two different images of the same dimensions:

image1 = rgb2gray(imread('frame429.jpg')); % read image 1 (convert to grayscale if RGB)
image2 = rgb2gray(imread('frame430.jpg')); % read image 2 must have same dimensions as image 1
filter1=blackman(size(image1,1),'symmetric'); % setup window in one direction - I chose Blackman window - could use Hamming or others
filter2=blackman(size(image1,2),'symmetric'); % setup window in second direction
filtimg1=(filter1*filter2'); % make 2D windowing function that is sized to match image
Image1=double(image1).*filtimg1; % apply windowing function to image
Image2=double(image2).*filtimg1;
FFT1 = fft2(Image1); % 2d FFT
FFT2 = conj(fft2(Image2));
FFTR = FFT1.*FFT2;
magFFTR = abs(FFTR);
FFTRN = (FFTR./magFFTR);
result = ifft2(double(FFTRN));
figure;
colormap('gray'); % choose colormap for plotting
imagesc(result);
figure;
colormap(jet);
mesh(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