# Manual calculation of template matching 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 close merge delete

Sort by » oldest newest most voted 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.

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

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

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

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

more

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.

Official site

GitHub

Wiki

Documentation