5

This seems like a very easy question, yet I couldn't find any documentation for this.

I have an image in Numpy, and i want to imshow the FFT.

In Matlab I can just do

F = fft(myimg)
imshow(F)

I can't do the same in Numpy because F is complex valued. Trying to do imshow(real(F)) gives me an all black image - I'm guessing because in [0,1] instead of 0..255. Multiplying by 255 also doesn't fix the issue.

Any ideas on how to get my plot?

Update:

Okay, natan pointed out how I incorrectly simplified this problem. Allow me to backtrack a bit. I have a video matrix, of dimensions (200, 30, 30, 3). 200 frames, 30x30 pixels, 3 color channels. For each color channel of each pixel, I want to compute fft of that pixel across time in the series. That should give me a new matrix which is (200,30,30,3). For each pixel, across each color channel, a 200-dim temporal fourier transform of each pixel. Then I should be able to look at e.g. the image created by the values of the first coefficient of the fourier transform in each pixel.

Note that the matlab fft operates on the first nonsingleton dimension, so F = fft(video) is doing what I'm after.

4
  • do you mean fft or fft2?
    – bla
    Mar 21, 2013 at 7:49
  • Here's a hint I hope it can give you a positive result ...
    – wim
    Mar 21, 2013 at 7:55
  • fft. Sorry, that was ambiguous. Technically what I'm doing is I have a time series of images (a video), and I'm computing FFT along the time dimension for each pixel. Mar 21, 2013 at 7:58
  • you use myimg and expect to get a 1D trace of some sort. what is your input dimensions?
    – bla
    Mar 21, 2013 at 8:02

1 Answer 1

9

Here's an example for a 2D image using scipy :

from scipy import fftpack
import numpy as np
import pylab as py

# Take the fourier transform of the image.
F1 = fftpack.fft2(myimg)

# Now shift so that low spatial frequencies are in the center.
F2 = fftpack.fftshift( F1 )

# the 2D power spectrum is:
psd2D = np.abs( F2 )**2

# plot the power spectrum
py.figure(1)
py.clf()
py.imshow( psf2D )
py.show()

For a 1D trace you can see and example here...

2
  • Why do you did ** 2? Also, how can I discard coefficients (simulating a lossy simple compression) not so important to F2? What are the criteria? Let's say we figure it out the max_coeff from F2 and then we can discard (put them to zero) all the coefficients that are abs(F2[x,y]) => abs(37/100*max_coeff) ? Mar 9, 2017 at 13:54
  • 1
    because the power or intensity is defined as the amplitude absolute valued square. see en.wikipedia.org/wiki/Spectral_density. about discarding coefficients, you ask about how to compress your data, you can rebin to a smaller resolution, or read about lossy compression elsewhere)
    – bla
    Mar 9, 2017 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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