# Phase Correlation and Negative Shifts

I am implementing phase correlation algorithm to determine shift between two images.

It generally works, but I am not sure how to interpret the resulting shift.

Pseudocode:

ft1 = FT(input1)
ft2 = FT(input2)

for each pixel p
product = ft1[p] * conj(ft2[p])
cross_power[p] = product / abs(product)

cross_correlation = IFT(cross_power)


where FT and IFT are Discrete Fourier Transform and its inverse, respectively.

Here are input images and resulting phase correlation taken from Wikipedia:

The peak is located at (20, 21) which corresponds to shifting of the second image.

I have obtained similar result, but it comes out flipped:

When I flip the input images the peak appears at the top left corner of the phase correlation image but how to interpret this as a negative shift (-20, -21)? Based on image quadrants?

I am using Fourier transform routine from Math.NET Numerics package. It provides only 1D FT, so I am doing transforms of rows and then columns to obtain 2D FT.

How to extract shift from the peak position allowing for negative shifts as well?

Why is the peak located differently in my result?

• I think that if you switch the two inputs, you'll get the opposite result. Maybe it wasn't clear which one was treated as input1 and input2 on the Wikipedia example. Apr 24 '13 at 17:49
• Is the pixel offset of the two pictures (20, 21)? That looks to me to be about correct just going by the naked eye. Apr 24 '13 at 18:00
• Maybe the images were indeed flipped on Wikipedia. Apr 24 '13 at 18:02
• Also, I think that if you conjugate fp1 instead of fp2, you should get that image as well. Haven't tried it myself. Apr 25 '13 at 10:55

the matter is due to this one:

product = ft1[p] * conj(ft2[p])


You can change to:

product = ft2[p] * conj(ft1[p])


I guess input1 is the reference image.

How to extract shift from the peak position allowing for negative shifts as well?

The result can be interpreted with quadrants as you suggest, since it follows periodic boundary conditions.

In pseudo code, where xtrans,ytrans is the location of the peak value:

if xtrans > imgwidth/2: xtrans = -(imgwidth-xtrans)
if ytrans > imgheight/2: ytrans = -(imgheight-ytrans)