Explaing Cross Correlation and Normalization for openCV's Match Template

JoeCodeCreations picture JoeCodeCreations · Feb 13, 2015 · Viewed 8.3k times · Source

My boss and I disagree as to what is going on with the CV_TM_CCORR_NORMED method for matchTemplate(); in openCV.

Can you please explain what is happening here especially the square root aspect of this equation.

Answer

Muhammet Ali Asan picture Muhammet Ali Asan · Feb 16, 2015

Correlation is similarity of two signals,vectors etc. Suppose you have vectors

 template=[0 1 0 0 1 0 ]   A=[0 1 1 1 0 0] B =[ 1 0 0 0 0 1]  

if you perform correlation between vectors and template to get which one is more similar ,you will see A is similar to template more than B because 1's are placed in corresponding indexes.This means the more nonzero elements corresponds the more correlation between vectors is.

In grayscale images the values are in the range of 0-255.Let's do that :

template=[10 250 36 30] A=[10 250 36 30] B=[220 251 240 210] .

Here it is clear that A is the same as template but correlation between B and template is bigger than A and template.In normalized cross correlation denumerator part of formula is solving this problem. If you check the formula below you can see that denumerator for B(x)template will be much bigger than A(x)template.

Formula as stated in opencv documentation : enter image description here

In practice if you use cross correlation,if there is a brightness in a part of image , the correlation between that part and your template will be larger.But if you use normalized cross correlation you will get better result.

Think formula is this :

enter image description here

Before multiplying element by element you are normalizing two matrixes.By dividing root of square sum of all elements in matrix you are removing the gain;if all elements are large then divisor is large.

Think that you are dividing sum of all elements in matrix.If a pixel value is in a brighter area then its neighbours pixel values will be high.By dividing sum of its neighbourhood you are removing illumination effect.This is for image processing where pixel values are always positive.But for 2D matrix there may be some negative values so squaring ignores sign.