Phase Correlation is a method to check the similarity of two images with equal size. It can be used for template matching, object tracking, motion estimation, etc. In this article, I will explain the basics and the code to perform Phase Correlation in OpenCV. The source code used in this tutorial is available for you to download. The package contains all necessary files and some installation notes.
Table of Contents:
To obtain the Phase Correlation of two images, perform these steps:

is the complex conjugate of G. The result is a 2D array with each element has a value between 0 to 1. The location of the highest value corresponds with the object translation movement from image 1 to image 2.
The main code performs a very simple task. It loads the reference and template images, call phase_correlation() function and display the result.
Note: In the code listings below I removed all error checking lines to make things simple. Remember that you should always have some error checking in your programs.
First, load the template and the reference image. Since Phase Correlation computes 2 equal images, the width and the height of both images need to be checked.
Listing 1: Load reference and template image
Next create a new image, poc, for the Phase Correlation result. Then call phase_correlation() function with tpl, ref and poc passed as parameters.
Listing 2: Perform Phase Correlation
Now that poc contains the result from Phase Correlation, find its maximum value and the location using cvMinMaxLoc. The result then displayed in the command line.
Listing 3: Get maximum value
The last step, display the images and free memory.
Listing 4: Display images and free memory
In the next section I will explain the phase_correlation() function, line by line.
This function performs Phase Correlation operation. It takes 3 parameters:
IplImage *ref: The reference imageIplImage *tpl: The template imageIplImage *poc: Empty image to store the resultTo obtain the DFT of the images, I use the FFTW library rather than OpenCV's DFT functions. This will increase the speed since FFTW computes FFT very fast.
First get the width and the height of the image. These properties are needed for the rest of the function. Also setup the pointers to copy the images to FFTW input and vice versa.
Listing 5: Phase Correlation Function
Note that the function above assumes that the three images have equal width and height. Next initialize some FFTW input/output arrays and plans, load the reference image to img1 and the template image to img2.
Listing 6: Initialize FFTW input and output arrays
In the listing above res will hold the end result of the phase correlation operation. Next is the heart of this function. It performs Phase Correlation and store the result to poc.
Listing 7: Compute phase correlation
Note that we have to normalize the IFFT result since FFTW computes an unnormalized DFT. Now all we have to do is just free memory and return to main function.
Listing 8: Free Memory
The Phase Correlation result is stored in poc. The main code should find the maximum value and its location from this result.
Here are some results I've got when computing phase correlation using this code.
Table 1. Phase Correlation Results
| # | Image 1 | Image 2 | Result |
|---|---|---|---|
| 1. | ![]() | ![]() (Image 2 is identical with image 1) | Max. value = 1.0 in (0,0) |
| 2. | ![]() | ![]() (image 1 with some noise) | Max. value = 0.2729 in (0,0) |
| 3. | ![]() | ![]() (object moved 6 pixels to the right and 15 pixels to the bottom) | Max. value = 0.6344 in (6,15) |
The first result shows the maximum value is 1.0 at (0, 0). This means both images are identical.
The second result shows the maximum value is 0.2729 at (0, 0). This means there is no translation from image 1 to image 2, but image 2 has some noise in it.
The last result shows the maximum value is 0.6344 at (6, 15). This means image 1 has a translative movement 6 pixels to the right and 15 pixels to the bottom.
This article covered Phase Correlation function in OpenCV. The code makes use FFTW library to obtain the DFTs rather than using OpenCV's DFT functions. This will increase the speed since FFTW computes FFT very fast.
The source code in this tutorial is available for you to download. The package contains all necessary files and some installation notes.
Send your comments, questions and bug reports to me [at] nashruddin.com.
Learning OpenCV: Computer Vision with the OpenCV Library
By: Gary Bradski, Adrian Kaehler
This book is the "de facto" OpenCV User's Manual. It provides a practical, pragmatic, accessible book on computer vision, with algorithmic explanation and concrete example code snippets. Written by the creators of OpenCV, no doubt you should obtain a copy.
Marcos Rodrigues on Jan 14, 2009:
Nash on Jan 14, 2009:
ABHAY SHANKAR on Feb 26, 2009:
Nash on Feb 26, 2009:
Makefile included in the download above for an example on how compiling fftw programs.Asad on Feb 27, 2009:
jane_doe on Feb 27, 2009:
ABHAY SHANKAR on Mar 2, 2009:
Nash on Mar 2, 2009:
Chandra Praksh on Mar 2, 2009:
ABHAY SHANKAR on Mar 2, 2009:
Nash on Mar 3, 2009:
Deb on Apr 8, 2009:
C:\Phase_Correlation>mingw32-make
gcc main.o phase_correlation.o -L"C:\OpenCV\lib" -L"C:\OpenCV\fftw" -lcxcore -lcv -lcvaux -lhighgui -lml -lcvcam -lfftw3-3 -o phase_correlation
C:\MinGW\bin\..\lib\gcc\mingw32\3.4.5\..\..\..\..\mingw32\bin\ld.exe: cannot find -lcvcam
collect2: ld returned 1 exit status
mingw32-make: *** [all] Error 1Nash on Apr 9, 2009:
$ gcc phase_correlation.c -o phase_correlation `pkg-config --cflags opencv` `pkg-config --cflags fftw` `pkg-config --libs opencv` `pkg-config --libs fftw` -lcxcore -lcv -lhighgui -lfftw3
$ ./phase_correlation 0201_1.jpg 0201_1.jpg
Maxval at (0, 0) = 1.0000
$ ./phase_correlation 0201_4.jpg 0201_5.jpg
Maxval at (0, 0) = 0.2729
$ ./phase_correlation 0201_2.jpg 0201_3.jpg
Maxval at (6, 15) = 0.6344 Nash on Apr 9, 2009:
lcvcam should be removed since the code doesn't use it. I've rewrite the code to make it simpler. Please download the package above and read how to compile in the README file. mclish on Apr 14, 2009:
Guenter Sandner on Apr 23, 2009:
| Tel. | +62 31 8662872 +62 856 338 6017 |
| ICQ | 489571630 |
| Skype | dede_bl4ckheart |
| Yahoo | dede_bl4ckheart |
| nashruddin.amin |
diyana on Sep 10, 2008: