Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Template matching - cross correlation vs square diff - when choose one over the other?

TL;DR

When and why is cross correlation beneficial over square diff (when using template matching)?

Details

According to OpenCV's documentation of template matching (you have to scroll down a bit), square difference is defined as:

enter image description here

and cross correlation as:

enter image description here

(where T is the template and I the image)

If I'm not mistaken, square diff is the only method (also compared to cross correlation coefficient) that is guaranteed to find the best match (in a numerical sense) in each image.

If we take a look at cross correlation we notice that the multiplication yields higher results for brighter parts in the image (because bright pixels have a higher numerical value than dark pixels). This means, if we perform template matching with a dark template on a bright image we will most likely get a bad result when using cross correlation.

For example, if we take this image:

source image

and perfrom template matching with this template:

template

we get these results (red is cross correlation and green is square diff):

result

Obviously, square diff is the better choice here as cross correlation yields a very bad result. The only advantage I can see in using cross correlation over square diff is the computational complexity as cross correlation should be a bit faster because it only has to calculate a product (instead of a sum and a square).

My question is: are there any benefits that I don't see? When should I choose one method over the other?

like image 924
Timo Avatar asked Oct 16 '25 13:10

Timo


1 Answers

Rosa Gronchi is right, you should use normalized cross-correlation. The Normalized Cross Correlation measurement is the Cross Correlation of the normalized vectors so that all vectors have length 1 and mean 0.
This way brighter patches won't have "advantage" over darker patches.

Regarding complexity, the square diff also have to calculate the product because it is extended to:

enter image description here

The product is calculated using the Fast Fourier Transform algorithm.

like image 151
dafnahaktana Avatar answered Oct 18 '25 06:10

dafnahaktana



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!