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.
In general I start with a picture like this
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]]]
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}]]]]
Comments
Post a Comment