Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to perform image cross-correlation with subpixel accuracy with scipy

The image below shows two circles of same radius, rendered with antialiasing, only that the left circle is shifted half pixel horizontally (notice that the circle horizontal center is at the middle of a pixel at the left, and at the pixel border at the right).

If I perform a cross-correlation, I can take the position of the maximum on the correlation array, and then calculate the shift. But since pixel positions are always integers, my question is:

"How can I obtain a sub-pixel (floating point) offset between two images using cross-correlation in Numpy/Scipy?"

In my scripts, am using either of scipy.signal.correlate2d or scipy.ndimage.filters.correlate, and they seem to produce identical results.

The circles here are just examples, but my domain-specific features tend to have sub-pixel shifts, and currently getting only integer shifts is giving results that are not so good...

Any help will be much appreciated!

enter image description here

like image 872
heltonbiker Avatar asked Dec 05 '12 19:12

heltonbiker


2 Answers

The discrete cross-correlation (implemented by those) can only have a single pixel precision. The only solution I can see is to interpolate your 2D arrays to a finer grid (up-sampling).

Here's some discussion on DSP about upsampling before cross-correlation.

like image 116
tiago Avatar answered Sep 20 '22 22:09

tiago


I had a very similar issue, also with shifted circles, and stumbled upon a great Python package called 'image registration' by Adam Ginsburg. It gives you sub-pixel 2D images shifts and is fairly fast. I believe it's a Python implementation of a popular MATLAB module, which only upsamples images around the peak of the x-correlation.

Check it out: https://github.com/keflavich/image_registration

I've been using 'chi2_shifts.py' with good results.

like image 4
beermat Avatar answered Sep 18 '22 22:09

beermat