Re: Normalised cross correlation
Hi guys, I have used NCC a lot in my research, so I thought I'd take a look at this. How critical is it that skimage's result image is the same size as matlab's? The difference arises from the approach to the problem. Matlab is assuming that the template might match an area of the target image that isn't fully in frame. They probably pad the original image (preferably with the target image average), and then do the cross correlation on the padded image. This would explain the different result size - Matlab seems to be M+m-1 by N+n-1. That means they pad by the template width laterally, and the template height vertically. They then have an M+2m x N+2n image, which they then apply the template match to. I haven't figured out the problem with the results not matching, though. The "good" news is that the results with padding the matlab way (pre-match) are especially fishy (nans are cropping up). That's certainly an indicator of something rather fundamental being wrong. I'm out of time for now, but I hope this helps a little. I will investigate further when time allows, provided you all don't beat me to it. Best, Mike On Tue, Mar 20, 2012 at 9:31 AM, Tony Yu <tsyu80@gmail.com> wrote:
On Tue, Mar 20, 2012 at 12:12 PM, Malcolm Reynolds <malcolm.reynolds@gmail.com> wrote:
Wow, those results are incredibly different. Yes, please put together a small script to reproduce the different behaviors (ideally, with the matlab result saved as .npy---or if it's easier .mat---file; also, if you can reproduce with a smaller image, that would be great).
It'll be easy to check for switched arguments, but the rest might take some time for me to get to. In your original email, it sounded like you have some expertise in template matching; feel free to dig into the code and see if you can spot any errors in the code.
Cheers, -Tony
I've quickly whipped up some scripts in both matlab (if you have access to that) and python and put them here: https://github.com/malcolmreynolds/NCC-test-program - if you don't have access to matlab, then the .mat file included should contain the inputs and outputs from matlab's normxcorr2. To run it you just need to put any image called 'lena.png' in the directory and then I ran 'ipython --pylab -i run_ncc.py' at the shell on OS X. You may have your plotting set up a different way, or not use pylab, but if all else fails the result images are written out as pngs (also included in the repository). With matlab just get to the matlab prompt and do "run_ncc".
For reference, this is what I see when using a 256x256 downsampled version of the lena image: http://img843.imageshack.us/img843/2729/screenshot20120320at155.png (matlab on left, middle and right are python with and without the pad_output flag).
I'm afraid I'm definitely not an expert on template matching, but I reckon it's pretty crucial to have a good free (&fast!) implementation available in numpy/scipy/skimage in order to compete.. I will take a dig through your code sometime but unfortunately deadlines are always looming for me.. I'll hopefully get a chance this coming weekend.
Thanks for your quick response, let me know if you have any problems with the stuff on github.
Malcolm
Great. Thanks for putting this together and for the original bug report. I'll take a look at this soon.... I hope. :)
-Tony
participants (1)
-
Mike Sarahan