0

I am doing a project in which I have a small template (T) and an image (imin). The aim is to use convolution along fft to find the location of the most similar pattern in imin. I want to use FFTW codes in C++ for this aim. But, I am new in C++ and don't know that how I should use this toolbox for this aim.


Thanks Paul. Actually, I want to use FFT for my 3D case in which I have a large 3D matrix and want to match with a small 3D matrix. That is why I want to use FFT. Since I should do it vey soon, do you know any C++ code for this aim? 2D and 3D (by FFT).

Kev
  • 118,037
  • 53
  • 300
  • 385
  • Do you mean convolution or correlation ? For template matching you would typically use cross correlation or normalised cross correlation. – Paul R Jul 27 '12 at 07:59

2 Answers2

0

Rather than use FFTW you might want to consider using OpenCV, which is a higher level API for computer vision and image processing in general, and is much easier to use than building your own routines from low level building blocks such as FFTs. OpenCV already has e.g. a template matching function cvMatchTemplate, and it can use efficient FFT implementations "under the hood" where needed so performance should not be a problem.

If you really do have to use FFTW then be prepared for some extensive reading of documentation and an initially steep learning curve. The steps for cross correlation (which I am assuming is what you want to use for your template matching) are typically:

  • create forward/reverse FFT plans for the larger image size
  • do FFT of target image using forward FFT plan
  • pad template image to size of target image with zeroes
  • do FFT of padded template image using forward FFT plan
  • take complex conjugate of padded image template FFT output
  • multiply target image FFT output by complex conjugate of padded image template FFT output
  • take IFFT of product using reverse FFT plan

You can then examine the result for one or more peak values, which should correspond to the location(s) of your template image within the target image.

Note that for better results you should consider using normalized cross correlation, but this is rather more complex to implement in the frequency domain.

Paul R
  • 208,748
  • 37
  • 389
  • 560
0

I believe in addition to the steps outlined in the previous post you have to make sure to shift the kernel before computing the FFT otherwise you will have a half kernel size displacement in the results.

user44010
  • 109
  • 1