I am working on a Fourier Ptychography problem. My research problem requires me to shift the Fourier spectrum of an image by a floating-point value. For a real-valued image, we can simply use cv2.warpAffine to shift the image by floating-point values (aka subpixel shifting).

Taking fourier transform of an image produces a complex matrix. The problem is, cv2.warpAffine does not support complex matrices, and so I cannot use it on them. I tried searching for alternatives, but none of them seem to work. I came across numpy.roll, but the problem is, it does not support subpixel shifting. Rounding off the shifting values translates to loss of information in my case. Is there a solution in python, that allows for subpixel shifting on complex matrices?


EDIT: Based on Marcus' answer, I did some digging and implemented a nifty little script for subpixel shifting in python based on a Matlab script for the same.

Here's the link to the script. Hope it helps!


You'd not do it in frequency domain at all. Use the shift property of the discrete Fourier transform!

Simply multiply the rows of your original image with an $e^{j2\pi \Delta f_x x/W}$ pointwise ($x$: pixel index in that row, $W$: width of image) before transforming to frequency domain to shift by $\Delta f_x$ in row direction. Same for shifts in column direction; multiply columns with $e^{j2\pi \Delta f_y y/H}$ pointwise ($y$: pixel index in that column, $H$: height of image). There's no restrictions on the "fineness" of $\Delta f$ in either direction, and any 2D shift can be understood as a shift in row and one in column direction.

If necessary, transform your image from frequency to spatial domain, do the multiplications, and transform back.

  • $\begingroup$ Oh, that seems easy to implement by myself! I really wonder how none of the major python number-crunching libraries have this tiny little function implemented yet. Thank you for your help! Hadn't thought about it that way. $\endgroup$ Apr 28 '20 at 21:07
  • $\begingroup$ @SuyogJadhav why should they? It's not hard to implement oneself, and image processing people usually don't care about complex numbers :) $\endgroup$ Apr 29 '20 at 7:17
  • $\begingroup$ Yeah true. One doesn't usually need to do a shifting on complex matrices. $\endgroup$ Apr 29 '20 at 10:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.