Ask Your Question

Manual calculation of template matching

asked 2015-06-08 22:51:31 -0500

Syarif Muhammad gravatar image

updated 2015-06-26 10:48:23 -0500

pklab gravatar image

hi master, i'm working on the final project to get undergraduate degree. i took topic about template matching, and i have problem with 'how to template matching work'. So, can you give me example of manual calculation of template matching? just simple example, maybe 20 x 20 pixels image example as original image, and 5 x 5 pixels as template image. And second question, i also confused different about template matching method : CV_TM_SQDIFF, CV_TM_CCORR, CV_TM_CCOEFF, because in my final project, i took CV_TM_SQDIFF and CV_TM_SQDIFF_NORMED method as template matching method. Thank you and sorry for my bad english.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2015-06-09 14:07:03 -0500

pklab gravatar image

Template matching measure the difference between a template and a region of your image. There are different way to calculate

  • SQDIFF Calculates Square difference
  • CCORR Calculates Correlation
  • COEFF Uses Correlation Coefficients
  • NORMED version normalizes the variation of energy in the image. You should use NORMED version because of most robust to light variation between template and image.

SQDIFF calculates the difference than best match have 0

CCORR e COEFF calculates the similarity than best match have 100%

In my experience SQDIFF and COEFF are less robust than CCORR.

Using NORMED version of algorithms you will have a result within the range of 0..1 where 1 means perfect matching (with SQDIFF 0 means perfect) so you can set a threshold for accept/discard the matching.

You could accept matching where the score is more than 0.9 (or less than 0.1 is SQDIFF)

Be careful because the examples uses to normalize (again) the result of the matching. If you do this you will have always a matching with max score because of successive normalization.

edit flag offensive delete link more


thank you sir, but i want to get explain of template matching algorithm. example with this formulas from this page so that process, not only theory. thanks

Syarif Muhammad gravatar imageSyarif Muhammad ( 2015-06-09 23:26:38 -0500 )edit

I think that the "How does it work?" chapter in examples provided with opencv is clear, don't you agree?

If you are looking for the code that realize the algorithm look at <opencv_dir>\sources\modules\imgproc\src\templmatch.cpp finally you could implement chosen formula by yourself using c++ code and many for loop, matrix calculation numerical optimization and so on but this is a pure maths c++ exercise

pklab gravatar imagepklab ( 2015-06-10 03:01:24 -0500 )edit

hmm that formula explains exactly what happens for each pixel right? I don't understand how people can explain that more clearly?

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-10 04:09:28 -0500 )edit

answered 2015-06-08 23:49:09 -0500

visionAwry gravatar image

You may want to take a look at the examples provided with opencv. Please check the following page

edit flag offensive delete link more


Like he said ^_^

StevenPuttemans gravatar imageStevenPuttemans ( 2015-06-09 04:40:25 -0500 )edit

thank you sir, but i want to get explain of formulas on that page. process why template image as same as with original image. thank you.

Syarif Muhammad gravatar imageSyarif Muhammad ( 2015-06-09 23:31:17 -0500 )edit

Question Tools



Asked: 2015-06-08 22:51:31 -0500

Seen: 2,931 times

Last updated: Jun 09 '15