Skip to main content

fourier analysis - 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.


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



Answer



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


Comments

Popular posts from this blog

plotting - How to draw lines between specified dots on ListPlot?

I would like to create a plot where I have unconnected dots and some connected. So far, I have figured out how to draw the dots. My code is the following: ListPlot[{{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4,13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full] I have thought using ListLinePlot command, but I don't know how to specify to the command to draw only selected lines between the dots. Do have any suggestions/hints on how to do that? Thank you. Answer One possibility would be to use Epilog with Line : ListPlot[ {{1, 1}, {2, 2}, {3, 3}, {4, 4}, {1, 4}, {2, 5}, {3, 6}, {4, 7}, {1, 7}, {2, 8}, {3, 9}, {4, 10}, {1, 10}, {2, 11}, {3, 12}, {4, 13}, {2.5, 7}}, Ticks -> {{1, 2, 3, 4}, None}, AxesStyle -> Thin, TicksStyle -> Directive[Black, Bold, 12], Mesh -> Full, Epilog -> { Line[ ...

equation solving - Invert and fit implicitly defined curve

I need to fit an implicitly defined curve. I thought I could get some data out of Solve , and then using FindFit . Therefore, I would like to find the relation the parametric curve defined by $F(x,y)=0$: Solve[-(1/2) + 1/2 (0.41202 BesselK[0, 0.1 Sqrt[x^2 + y^2]] + (0.101483 x BesselK[1, 0.1 Sqrt[x^2 + y^2]])/Sqrt[x^2 + y^2]) == 0, y] But I can't get an output: Solve was unable to solve the system with inexact coefficients or the system obtained by direct rationalization of inexact numbers present in the system. Since many of the methods used by Solve require exact input, providing Solve with an exact version of the system may help. >> Edit: In particular, I would like to fit the data coming from the curve with the expression of another curve, and not with a function $f(x)$. In particular, since this clearly looks like a cardioid , I would like it to fit to something like it. What other strategies could I try?

dynamic - How can I make a clickable ArrayPlot that returns input?

I would like to create a dynamic ArrayPlot so that the rectangles, when clicked, provide the input. Can I use ArrayPlot for this? Or is there something else I should have to use? Answer ArrayPlot is much more than just a simple array like Grid : it represents a ranged 2D dataset, and its visualization can be finetuned by options like DataReversed and DataRange . These features make it quite complicated to reproduce the same layout and order with Grid . Here I offer AnnotatedArrayPlot which comes in handy when your dataset is more than just a flat 2D array. The dynamic interface allows highlighting individual cells and possibly interacting with them. AnnotatedArrayPlot works the same way as ArrayPlot and accepts the same options plus Enabled , HighlightCoordinates , HighlightStyle and HighlightElementFunction . data = {{Missing["HasSomeMoreData"], GrayLevel[ 1], {RGBColor[0, 1, 1], RGBColor[0, 0, 1], GrayLevel[1]}, RGBColor[0, 1, 0]}, {GrayLevel[0], GrayLevel...