Discussion:
Interpreting the peaks in a 2D fft of a non-square image
(too old to reply)
Ethan Montag
2017-02-21 16:02:03 UTC
Permalink
If I make an image of a horizontal sine wave grating, f, I know that in the 2D fft, the peak location can be interpreted as the frequency like this:

% f is n rows by m columns
F = fft2(f);
S = abs(F);

Discounting the DC at (1,1), I can find the peak in the first row.
Say it is at (1, 11). I can subtract 1 from the column location (Matlab indexing) and know that there are 10 cycles per the m columns of the grating. So the distance from the origin gives me the frequency. E.g. (10/m)cycles per pixel

The same thing occurs for a vertical grating. If I rotate the grating 90 degrees and the image is square (m=n), the peak will be at (11,1).

However if the image is a rectangle, the location of the peak is different. If the image height is half the width, then the peak will be at (6,1) meaning that there are 5 cycles per n rows of the image. This is the same frequency in terms of cycles per pixel.

So I made a very large horizontal sine grating image and rotated it 0 to 90 deg and cropped a rectangular part from the middle to get rotated gratings that fill the image. When I do the fft,
I can find the peak (r, c) and calculate the distance from the origin (1,1). But I can't figure out how to get the frequency of the peak. The distance from the origin gives the wrong answer (except the first row and column) and I tried some weighting functions but I couldn't figure it out. (The data looks like there is some sort of sinc function involved but I'm not sure.)

I am looking for the relationship where I can calculate the spatial frequency for any location(r,c) in a 2D FFT from a rectangular shaped image: G(r-1,c-1, m, n) = special frequency.

Does anyone have any ideas.
Ethan Montag
2017-02-21 16:08:03 UTC
Permalink
Oops, I mean a vertical grating in the beginning.
Ethan Montag
2017-02-22 15:57:03 UTC
Permalink
I think I figured it out if anyone cares.

Let:
width = the width of the image in pixels and
height = the height of the image in pixels

For a [column, row] coordinate pair from the unshifted fft2 from the upper left quadrant [c, r], subtract 1 to get [x, y] values.

[x,y] = [c-1,r-1]

(For the other quadrants the [x, y] values go inwards from the corners, eg. the second row, last column is (x,y) = (1,1). The top row and left column are special. To get all the frequencies you will need to do either the top left quadrant and the top right quadrant or the top left quadrant and the bottom left quadrant.)

Normalize the (x,y) values by the width and height:

xn = x/width
yn = y/height

calculate theta;

theta = atan(yn/xn);

Let
f = x/cos(theta)
g = y/sin(theta)

then the frequency, v, in cycles per pixel is
v=f/width
or
v=g/height

(I am ignoring the direction so if you want to know if the component is oriented one way or another, you have to add signs for different quadrants.)
Loading...