Reading out specific points off the Matlab / Octave fft2() function output

不问归期 提交于 2019-12-24 00:38:44

问题


I am getting familiarized with Octave and the function fft2(). In this toy example, I am aiming at producing the 2D DFT of the following 256 x 256 png image:

To be able to understand the output easily, I try to convert this image into a 256 x 256 image, eliminating color information:

Im = imread('circ.png');
pkg load image
Im = rgb2gray(Im);
figure, imshow(Im)

After this bookkeeping preliminaries I run:

A = fft2(double(Im));


OK. Now I take the same image, and analyze it with ImageJ, checking the output at point (157, 96), like this:

So the magnitude is going to be sqrt(7.448^2 + 10.458^2) = 12.83

And the phase, arctan(-10.458 / 7.448) = 54.54 degrees.

The question is, How can I get these values out of the fft2() output?


In case it makes a difference, this is how I plotted the Octave output 2D DFT:

subplot(132);
h = imshow(abs(fftshift(A)),[24 100000]);
h2 = get(h,'Parent');
set(h2,'YDir','Normal');
axis equal tight;
title("2D FFT Magnitude");

subplot(133);
h = imshow(angle(fftshift(A)),[-pi pi]);
h2 = get(h,'Parent');
set(h2,'YDir','Normal');
axis equal tight;
title("2D FFT Phase");

and this is the process in ImageJ:


回答1:


Here are a few observations which should clarify the scaling used:

  • ImageJ's X and Y positions are 0-based whereas Matlab's indexing is 1-based.
  • Incrementing ImageJ's X position corresponds to incrementing the column index in Matlab, and incrementing ImageJ's Y position corresponds to incrementing the row index in Matlab, so that an (X,Y) coordinate pair in ImageJ will be found at index (Y+1,X+1) in Matlab.
  • ImageJ displays the image with the 0 frequency component in the middle of the image, so measurements are effectively made on the equivalent of fftshift(Im)
  • ImageJ scales the 0-255 grayscale values to floating point values in the 0.0-1.0 range (i.e. scaling all values by dividing by 255)

So with that in mind, we have:

>> Ashifted = fftshift(A);
>> Ashifted(97,158)/255
ans =   7.4484 - 10.4582i
>> Ashifted(93,165)/255
ans =  12.1928 -  4.9850i

which correspond exactly to your illustrated measurements of the real and imaginary parts at positions (X,Y) = (157,96) and (X,Y) = (164,92) respectively.

Note that by the linearity property of the FFT, you could also divide the input and get the same results:

A = fft2(double(Im)/255.0);
>> Ashifted = fftshift(A);
>> Ashifted(97,158)
ans =   7.4484 - 10.4582i
>> Ashifted(93,165)
ans =  12.1928 -  4.9850i


来源:https://stackoverflow.com/questions/43192747/reading-out-specific-points-off-the-matlab-octave-fft2-function-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!