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

heltonbiker picture heltonbiker · Dec 5, 2012 · Viewed 8.4k times · Source

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

Answer

tiago picture tiago · Dec 6, 2012

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.