# Template matching with the CV_TM_CCOEFF algorithm

I can't figure out the CV_TM_CCOEFF algorithm. (I get the CCORR & Least squares)

The O'Reilly book explains that "These methods match a template relative to its mean against the image relative to its mean, so a perfect match will be 1 and a perfect mismatch will be -1; a value of 0 simply means that there is no correlation". However, the equation given for CV_TM_CCOEFF doesn't subtract the mean from each pixel value but instead subtracts the reciprocal of the pixel value sum TIMES the number of pixels (shouldn't it be a division?). Plus, all the simple examples I work out on paper (with small, one dimensional signals) usually don't give me 1, 0, or -1. I also Googled Correlation Coefficient and found variations of this: Pearson Correlation Coefficient, which has all kinds of covariant and squared terms I can't reconcile with the OpenCV equation.

edit retag close merge delete

i have question like u about CCOEFF algorithm in theory. do u know, what do x'' & y'' refers to ? i know x&y are coordinates for source image, x' & y' are coordinates for template.

( 2014-06-05 07:59:18 -0500 )edit

Sort by » oldest newest most voted

According to the documentation CV_TM_CCOEFF and CV_TM_CCOEFF_NORMED both subtract the mean from the window and the template, and yes, '1' shoud denote a perfect match.

more

what do x'' & y'' refers to ? i know x&y are coordinates for source image, x' & y' are coordinates for template.

( 2014-06-05 07:50:53 -0500 )edit

x'' and y'' are just indices like x' and y', they were only needed since x' and y' already existed. For CV_TM_CCOEFF and for CV_TM_CCOEFF_NORMED the template and the window in the image at position x,y are made zero-mean. Therefore you need to go through all locations of the template and the window.

( 2014-06-06 05:37:13 -0500 )edit

This method is called Normalized Correlation Coefficient or Normalized Cross-Correlation (NCC). I looked through OpenCV source code, and then found paper [1] which contains clear description and probably was used in CV_TM_CCOEFF and CV_TM_CCOEFF_NORMED implementation.

Also I found paper [2] which shows promising NCC computation efficiency, but there is no implementation available.

more

Official site

GitHub

Wiki

Documentation