# Revision history [back]

### Phase correlation for template matching

I am trying to implement template matching using phase correlation. I have already done it in the spatial domain. You can see here

My template image is imagePart and the image in which I am finding it is imageBig.

Now I am trying it using DFT to increase speed. I am following these steps as suggested by Cris Luengo

1. Pad the template (floating image) to the size of the other image
(with zeros).
2. Compute the FFT of both.

3. Flip the sign of the imaginary component of one of the results (complex conjugate).

4. Multiply the two.

5. Compute the IFFT of the result.

6. Find the location of the pixel with the largest value.

My code is:

int r_big,c_big,r_part,c_part;
int m,n;
Mat complexI_big, complexI_part;
void corr_frq()
{

r_big=imageBig.rows;
c_big= imageBig.cols;

r_part=imagePart.rows;
c_part=imagePart.cols;

//Pad template to match size of big image.

m = getOptimalDFTSize( imageBig.rows );
n = getOptimalDFTSize( imageBig.cols );

copyMakeBorder(imageBig, imageBig_padded, 0, m - imageBig.rows, 0, n - imageBig.cols, BORDER_CONSTANT, Scalar::all(0));
copyMakeBorder(imagePart_pad, imagePart_padded, 0, m - imageBig.rows, 0, n - imageBig.cols, BORDER_CONSTANT, Scalar::all(0));

merge(planes, 2, complexI_big);
merge(planes2, 2, complexI_part);

dft(complexI_big,complexI_big);
dft(complexI_part,complexI_part);

mulSpectrums(complexI_big, complexI_part, mul_output, 0, true );

cv::Mat inverseTransform;
cv::dft(mul_output, inverseTransform, cv::DFT_INVERSE|cv::DFT_REAL_OUTPUT);
normalize(inverseTransform, inverseTransform, 0, 1, CV_MINMAX);
imshow("Reconstructed", inverseTransform);
waitKey(0);


Shouldn't it give max output at the image location where the imagePart (tick sign is). Am I doing something wrong?