# Filter a specific area of an image

I try to obtain the angle of the periodic waves that you can see below.

I am using the FourierTransformation to get it:

direin = StringJoin[NotebookDirectory[], "Eingang\\"];
diraus = StringJoin[NotebookDirectory[], "Ausgang\\"];
SetDirectory[direin]; files = FileNames[{"*.png"}, FileNameJoin[{Directory[]}]]
img = Import /@ FileNames[files[[1]]];
img = img[[1]];
img = ColorConvert[img, "Grayscale"];
(*sauberer FIltern*)
{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;
(*Fourier*)
ft = Fourier[imgTimesWnd];
center = Floor[Dimensions[ft]/2];
ft = RotateRight[ft, center];

(*Winkel*)
brightestOffset = First[Position[Abs[ft], Max[Abs[ft]]]] - center;
maxAngle = ArcTan @@ N[brightestOffset/{h, w}];
getDeg = 180 / \[Pi]*maxAngle


It worked very well for a picture you can see here: Angle between two areas of an image of a 2D FFT

But for the picture you can see above i receive an image like this:

As you can see there are too many periodic reflections, so the code can't work.

My idea is to use a filter to get the interesting areas, marked as the area between the red circles in the image below.

Actually I tried a bandpass Filter and a Mask function... not very successful, do you have any other ideas?

Version 1 Using GradientOrientationFilter[ ]

i = Import["http://i.stack.imgur.com/HuyCnm.png"];
tvf = TotalVariationFilter[i, 1, Method -> "Laplacian", MaxIterations -> 100];
op = Closing[ImageSubtract[i, tvf] // Binarize, DiskMatrix[1]];
gof1 = ArcTan[Sin[gof], -Cos[gof]];
angle = Flatten@gof1 // Mean;
Print["Angle in Degrees = ", angle/Degree];
tan = Tan@angle;
Show[i, Graphics[{Orange, Thick,
Line[Last@ImageDimensions[i] {{0, 1}, {1/tan, 0}}]}]]


Version 2 Using Radon[ ]

Another way, using the Radon Transform:

r = Radon[i];
lpl = LaplacianGaussianFilter[r, {5, 1}];
idr = ImageDimensions@gf;
cc = Ordering[Total@ImageData@gf, -5];
angles = cc Pi/First@idr;
t = angles // Mean // Tan;
Show[i, Graphics[{Orange, Thick,
Line[Last@ImageDimensions[i] {{0, 1}, {-1/t, 0}}]}]]


And here you have your code, adapted. Not much care taken, though.

img = Import["http://i.stack.imgur.com/HuyCnm.png"];
img = ColorConvert[img, "Grayscale"];

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

rawPixels = ImageData[img];
imgTimesWnd = (rawPixels - Mean[Flatten[rawPixels]])*wnd;
(*Fourier*)
ft = Fourier[imgTimesWnd];
center = Floor[Dimensions[ft]/2];
fti = Image@Abs@RotateRight[ft, center];
ft1 = ImageData@ ColorConvert[
ImageMultiply[ fti, Rasterize[Graphics[Rectangle[]], ImageSize -> 20]],
"Grayscale"];

brightestOffset = First[Position[ft1, Max[ft1]]] - center;
t = ArcTan @@ N[brightestOffset/{h, w}]
Show[img, Graphics[{Orange, Thick,
Line[Last@ImageDimensions[img] {{0, 1}, {1/t, 0}}]}]]


• Nice illustration of image-processing features (+1). By the way, gof1 is just gof - π/2. Nov 25 '15 at 1:26
• @ybeltukov Thanks :) There is always a sign in there that I can't get right Nov 25 '15 at 1:48
• @belisarius has settled: Thx for your quick answer. Version 1 seems to have the best fit with the original waves. But I receive an Angle of 1,66221 rad, what is 95°. Actually I measured an Angle of 122° of the orange line. Is there something missing in the code? Nov 26 '15 at 8:18
• @Felicitas I don't understand your question. What do you mean by "I measured an Angle of 122° of the orange line."? Nov 26 '15 at 13:36
• BTW I modified the code to print the angle in degrees Nov 26 '15 at 13:37