[Numpy-discussion] FFT's & IFFT's on images

Stéfan van der Walt stefan@sun.ac...
Wed Jul 2 16:56:03 CDT 2008

Hi Mike

2008/7/2 Mike Sarahan <msarahan@gmail.com>:
> I'm trying to do phase reconstruction on images which involves switching
> back and forth between Fourier space and real space.  I'm trying to test
> numpy (& scipy, for that matter) just to see if I can go back and forth.
> After an FFT/iFFT, the resulting image is garbage.  I'm using
> numpy.fft.fftn, but I've also tried fft2, rfftn, rfft2, and the
> corresponding inverse FFT's.
> >From looking at the matrices, it appears to be creating complex
> components that aren't in the matrix prior to any FFT's.  Real fft's
> seem to add some small component to each value (<1).  I'm using
> Image.fromarray to convert arrays to images, and I'm working with 8-bit
> grayscale images.

Those components are very small!

In [59]: x = (np.random.random((15,15)) * 255).astype(np.uint8)

In [60]: np.fft.fft2(x).imag.sum()
Out[60]: -2.5011104298755527e-12

And you can see that the forward-reverse transformed values compare
well to the original:

In [61]: z = np.fft.ifft2(np.fft.fft2(x))

In [62]: np.abs(x - z).sum()
Out[62]: 2.5060395252422397e-11

If you have bigger problems, send us a code snippet and we'll take a look.


More information about the Numpy-discussion mailing list