El Dude
El Dude

Reputation: 5618

Normalize scipy.ndimage.filters.correlate

does anybody have an idea how to normalize the

scipy.ndimage.filters.correlate

function to get :

XCM = 1/N(xc(a-mu_a,b-mu_b)/(sig_a*sig_b))

What is N for the correlation? It usually is the # of datapoints / pixels for images. Which value shall I choose for scipy.ndimage.filters.correlate? My images differ in size. I guess the scipy correlate function pads the small image into zeros?

The size of the final matrix N = XCM.sizeX() * XCM.sizeY() ?

Thanks, El

Upvotes: 1

Views: 7127

Answers (2)

Neil Traft
Neil Traft

Reputation: 19676

Normalized Cross-Correlation (NCC) is also included in scikit-image as skimage.feature.match_template. See this template matching example.

You can also do the same with OpenCV with the matchTemplate method. There are many good bindings from Python to OpenCV, but it's a bit overkill if you only need template matching. I'd go with scikit-image.

Upvotes: 6

ali_m
ali_m

Reputation: 74172

It looks to me like you're trying to compute the normalized cross-correlation of two images (I suspect you're probably trying to do template matching?). This answer assumes that the normalized cross-correlation is what you want.

  • When you compute the normalized cross-correlation between your two images, you're doing the equivalent of subtracting the mean and dividing by the standard deviation to both your template and reference image in the region where they overlap.

  • Here, N would be equal to the number of pixels in your template, which is the same as the number of pixels in the local region of overlap between the template and the reference image as you slide the template over the reference.

  • You should read the Wikipedia article on cross-correlation, and in particular this bit for the definition of normalized cross-correlation and some explanation for what each of the terms mean.

  • This article by Lewis (1995) has a more in-depth explanation, and also describes some neat tricks for efficiently computing the normalized cross-correlation.

  • I also wrote my own Python functions for template matching including normalized cross-correlation based on Lewis and some snippets of MATLAB. You can find the source here.

Let me know if you have more questions and I'll have a go at explaining.

Upvotes: 8

Related Questions