# Angle between two areas of an image of a 2D FFT

I'm trying to obtain the angle between the red line and the area surrounded by the red circle, related to the middle of the image. Usually the area surrounded by the red circle is lighter than the rest of the image. Do a 2D FFT with:

img = Import["Sample2.png"];
fft = Fourier[ImageData[img]];
fft = RotateLeft[fft, Floor[Dimensions[fft]/2]];
Image[Rescale[Log[Abs[fft] + 10.^-10]]]


And recieve an image like you can see above (Of course without the red marked lines).

As I told you, I can't find a way to obtain the angle between the red line and the area surrounded by the red circle, related to the middle of the image. I would be very thankful for any hint. Best regards

• Hello and welcome to Mathematica.SE! Please take some care in formatting your questions and upvote the answers that you find helpful. You should take the tour to learn more about this forum and the best way to write productive questions and answers. Note that you can format code using markdown or the buttons at the top of the question editor. Sep 14, 2015 at 10:28

First, I have a few improvement suggestions for your Fourier code:

The bright vertical and horizontal lines you see in your Fourier image are the sharp gradients at the borders of the image (because the Fourier transform assumes a periodic image). So you should get rid of the black border at the bottom:

img = Import["http://i.stack.imgur.com/bIUkE.png"];


and multiply your image with a window function:

{w, h} = ImageDimensions[noBorder];
wnd = Outer[Times, Array[HammingWindow, h, {-.5, .5}],
Array[HammingWindow, w, {-.5, .5}]];

rawPixels = ImageData[noBorder][[All, All, 1]];
imgTimesWnd = (rawPixels - Mean[Flatten[rawPixels]])*wnd;

ft = Fourier[imgTimesWnd];
center = Floor[Dimensions[ft]/2];
ft = RotateRight[ft, center];

Image[Rescale[Log[Abs[ft] + 10^-3]]] Much cleaner.

The next step is to find offset of the brightest point from the center:

brightestOffset = First[Position[Abs[ft], Max[Abs[ft]]]] - center


(Note: I had to replace RotateLeft with RotateRight above so this works nicely. You can try for yourself that even if you use e.g. center=Floor[Dimensions[ft]/2]-2 above, the brightestOffset will still be the same.)

and calculate the angle to the center:

maxAngle = ArcTan @@ N[brightestOffset / {h, w}]


Actually, this is not the angle you've drawn in you image: That would be ArcTan@@N[brightestOffset]. But I'm guessing you're really after an angle in the original image, rather than an angle in the unscaled Fourier transform image:

Module[{center = 0.5 {w, h},
dir = {Cos[maxAngle], Sin[maxAngle]}*100,
norm = {Cos[maxAngle + \[Pi]/2], Sin[maxAngle + \[Pi]/2]}*1/
Norm[brightestOffset/{h, w}]},
Show[noBorder, Graphics[{Red,
Table[
Line[{center - dir + i*norm, center + dir + i*norm}],
{i, -10, 10}]}]]] Which is the angle of the sine wave you would get if you filtered only this single frequency:

Image[Rescale@
Re[InverseFourier[
Fourier[imgTimesWnd]*
SparseArray[{brightestOffset + 1 -> 1}, {h, w}]]]] • Great answer! Notice how the frequency does not match the experimental image 100% because of some branched lines in the upper left corner.
– shrx
Sep 14, 2015 at 11:49
• @shrx: I noticed and haven't found out 100% why yet. I'm guessing the "real" peak isn't at an integer location in the fourier transform. But since that wasn't part of the original question, I didn't investigate further... Sep 14, 2015 at 13:11
• Thank you very much. @nikie, your guess was right, I just wanted the angle of the periodic waves. But there are some more Information I try to obtain from the image of the FFT, so I tried to write a code to get the angle out of that image. I created the black border of the original Image, i thought that the created light line in the FTT imgae could probably be useful to obtain the angle... Sep 15, 2015 at 7:33