4

I'm trying to use opencv to find some template in images. While opencv has several template matching methods, I have big trouble to understand the difference and when to use which by looking at their mathematic equization:

  • CV_TM_SQDIFF
  • CV_TM_SQDIFF_NORMED
  • CV_TM_CCORR
  • CV_TM_CCORR_NORMED
  • CV_TM_CCOEFF

Can someone explain the major difference between all these method in a non-mathematical way?

Aaron Shen
  • 8,124
  • 10
  • 44
  • 86

1 Answers1

11

The general idea of template matching is to give each location in the target image I, a similarity measure, or score, for the given template T. The output of this process is the image R.

Each element in R is computed from the template, which spans over the ranges of x' and y', and a window in I of the same size.

Now, you have two windows and you want to know how similar they are:

CV_TM_SQDIFF - Sum of Square Differences (or SSD):

Simple euclidian distance (squared):

  • Take every pair of pixels and subtract
  • Square the difference
  • Sum all the squares

CV_TM_SQDIFF_NORMED - SSD Normed

This is rarely used in practice, but the normalization part is similar in the next methods.

The nominator term is same as above, but divided by a factor, computed from the - square root of the product of:

  • sum of the template, squared
  • sum of the image window, squared

CV_TM_CCORR - Cross Correlation

Basically, this is a dot product:

  • Take every pair of pixels and multiply
  • Sum all products

CV_TM_CCOEFF - Cross Coefficient

Similar to Cross Correlation, but normalized with their Covariances (which I find hard to explain without math. But I would refer to mathworld or mathworks for some examples

Eran W
  • 1,696
  • 15
  • 20
  • awesome explanation. but there is a question: when CV_TM_CCOEFF "normalized with their Covariance" then why we have (need) CV_TM_CCOEFF_NORMED ? – Amir Mohammad Moradi Jan 20 '19 at 21:36
  • check out this link for good visuals of the different methods: https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_imgproc/py_template_matching/py_template_matching.html – Eran W Jul 20 '19 at 20:36
  • 1
    From what I see in the [OpenCV docs](https://docs.opencv.org/3.4.10/df/dfb/group__imgproc__object.html#ga3a7850640f1fe1f58fe91a2d7583695d), **CV_TM_CCOEFF** only performs mean centering i.e., the window means of the target and template are subtracted from each pixel in the respective windows. The full [normalised variant](https://en.wikipedia.org/wiki/Cross-correlation#Zero-normalized_cross-correlation_(ZNCC)) is **CV_TM_CCOEFF_NORMED**, which does the mean centering and also divides by the standard deviations. – zepman May 21 '20 at 14:41