开发者

Discrepancy Between Matlab and Numpy+Python FFT2?

So I was doing some homework on diffraction in Python with Numpy. My results simpl开发者_开发百科y wouldn't turn out how they should have, and I was confused.

The code I used to generate the FFTs is as follows:

Python:
aperaturearray = np.array(im) # Turn image into numpy array
Ta = np.fft.fftshift(np.fft.fft2(aperaturearray))
### I did some calculations here ###
ftfm = Image.fromarray(np.uint8(Utfm)) 
ftfm.save(("Path"))  

Matlab:
rect = imread('PATH\pyRectangle.jpg');
rectfft = fft2(rect);
imwrite(rectfft, 'C:\Users\Dan\Documents\python\DiffPhotos\matlabRectfft','jpg')
pyrectmat = ifft2(pyfftrect);
imwrite(pyrectmat, 'Path','jpg')

The images are here -> http://imgur.com/a/3Cw81#EdFoA

Now what's going on here? Why are the FFT images so different? Different implementations? Why doesn't my homework code work :(


To add to mor22's answer:

In Matlab you can use fftshift to move the lowest frequencies to the center. It essentialy just swaps the upper-left with the lower-right quadrant (and upper-right with lower-left).


To begin with, in Matlab, the values returned from fft2 are complex. I'm not sure how imwrite will deal with them. Trying this code seems to give an almost sensible results. (Taking the log10 helps with the display)

rect = imread('rect.jpg');
rectfft = fft2(rect);
pcolor(log10(abs(rectfft)));
shading flat

The image is offset about the origin because the pixel index is probably related to the spatial frequency of the image. ie low spatial frequencies appear close to 0,0. The higher pixel indicies are aliases of these frequencies. You could rearrange the image to put the (0,0) in the middle with some matrix manipulation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜