Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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;
Mat imagePart_pad,imageBig_padded, imagePart_padded, mul_output;
int m,n;
Mat complexI_big, complexI_part;
void corr_frq()

  c_big= imageBig.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));

   Mat planes[] = {Mat_<float>(imageBig_padded), Mat::zeros(imageBig_padded.size(), CV_32F)};
   Mat planes2[] = {Mat_<float>(imagePart_padded), Mat::zeros(imagePart_padded.size(), CV_32F)};

   merge(planes, 2, complexI_big);
   merge(planes2, 2, 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);

  imshow("image part pad",imagePart_pad);

 // waitKey(0);


After performing above operation I am getting this output

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