1

I have been using Matlab's normxcorr2 function to do template matching with images by performing normalized cross correlation. To find the maximum correspondence between a template and an image, one can simply run normxcorr2 and then find the maximum absolute value of all the values returned by normxcorr2 (the function returns values between -1.0 and 1.0).

From a quick Google search, I found out that a negative correlation coefficient means an inverse relationship between two variables (e.g. as x increases, y decreases), and that a positive correlation coefficient means the opposite (e.g. as x increases, y increases). How does this apply to image template matching? That is, what does a negative value from normxcorr2 mean conceptually with respect to template matching?

t2k32316
  • 993
  • 1
  • 13
  • 29

2 Answers2

1

View normalized cross correlation as a normalized vector dot product. If the angle between two vectors is zero, their dot product will be 1; if they are facing in the opposite direction, then their dot product with be negative 1. This is idea is actually direct if you take the array and stack the column end to end. The result is essentially a dot product between two vectors.

Also just as a personal anecdote: what confused me about template matching at first, was intuitively I believed element wise subtraction of two images would be a good metric for image similarity. When I first saw cross correlation, I wondered why it used element wise multiplication. Then I realized that the later operation is the same thing as a vector dot product. The vector dot product, as I mentioned before, indicates when two vectors are pointing in the same direction. In your case, the two vectors are normalized first; hence why the range is from -1 to 1. If you want to read more about the implementation, "Fast Normalized Cross Correlation" by J.P. Lewis is a classical paper on the subject.

JustinBlaber
  • 4,629
  • 2
  • 36
  • 57
  • Is the intuition behind two vectors being in the opposite direction the same as considering the template and corresponding image patch as being negatives of one another (as Dmitry mentioned in his answer)? I feel like there might be more one can say here, besides referring to only dot products and images being negatives of one another. But maybe this is the most insight one can draw from this... – t2k32316 Mar 12 '13 at 17:22
  • Recall what basic cross correlation is. It's elementwise multiplication followed by a summation operation. This is the exact same operation as the inner (or dot) product of two vectors. Moreover, if you do this operation on two arrays, the same still holds true. This is because if you were to reshape the arrays into a single column, the operation is exactly a dot product. In my opinion, this is the best way to view cross correlation for arrays - simply as dot products of vectors. The highest correlation coefficient indicates when two vectors point in the same direction. – JustinBlaber Mar 13 '13 at 04:34
  • Also, Dmitry subtracts by the mean values, this makes the correlation coefficient invariant to scalar shifts in value. If the templates are normalized, this makes the coefficient invariant to scaled values. If you are really interested in implementing cross correlation you should also look into the FFT. – JustinBlaber Mar 13 '13 at 04:38
0

Check the formula on wikipedia.

When f(x, y) - mean(f) and t(x,y) - mean(t) have different sign the result of an addendum will be negative (std is always positive). If there are a lot of such (x,y) then the whole sum will also be negative. You may think that if 1.0 means that one image is equal to another. -1.0 means that one image is a negative of another (try to find normxcorr2(x, -x))

Dmitry Galchinsky
  • 2,181
  • 2
  • 14
  • 15
  • normxcorr2(x, -x) gave a matrix full of 0's when x was an image; but when I made a sample x matrix, then yes, it gives a -1 in the middle of the returned matrix. – t2k32316 Mar 12 '13 at 17:18
  • What was the type of ans? It could return 0 because of the result was unsigned int, not double. Unfortunately I can't check it now – Dmitry Galchinsky Mar 12 '13 at 20:53
  • Yes, you're right, it was because of unsigned int's. After changing them to doubles, I got the -1 in the middle again. – t2k32316 Mar 13 '13 at 05:05