6
$\begingroup$

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.

Image of the sample after 2D FFT

Usually the area surrounded by the red circle is lighter than the rest of the image.

In general I start with a picture like this

Image of the sample

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

$\endgroup$
  • 1
    $\begingroup$ 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. $\endgroup$ – dionys Sep 14 '15 at 10:28
15
$\begingroup$

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"];    
noBorder = ImagePad[img, -BorderDimensions[img]];

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]]]

enter image description here

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}]}]]]

enter image description here

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}]]]]

enter image description here

$\endgroup$
  • 1
    $\begingroup$ Great answer! Notice how the frequency does not match the experimental image 100% because of some branched lines in the upper left corner. $\endgroup$ – shrx Sep 14 '15 at 11:49
  • 2
    $\begingroup$ @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... $\endgroup$ – Niki Estner Sep 14 '15 at 13:11
  • $\begingroup$ 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... $\endgroup$ – Felicitas Sep 15 '15 at 7:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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