# Orientation of the pattern in an image

Following the topic Find the center of a circular specimen, I have trouble determining the principal orientations of the pattern in an image: Image to analyze

The main goal was to find the principal direction of the pattern in an image and to orient the image accordingly. So far, I have tried to use the code from Mathematica Image Processing tutorial (Analyze Orientations in an Image) and an SE answer bellow the following link: Image Processing: Finding Orientation and Position of Symmetry Axes

However, both methods are not working as they should and images have to be rotated manually with ImageRotate. In this example image would have to be rotated for approximately 45° in order to align the pattern principal orientation axes with the horizontal and vertical directions.

I think that the main problem is small size of white pockets without dominant dimension. Global orientation of the pattern can only be seen when looked upon an image from far away.

EDIT

It is important to add that I am looking for the orientation of white-spot pattern and not the orientation of the rectangular net. Nevertheless, your suggestions are really nice, because they deal with problem from different angles.

Before reading your answers I was trying to do something considering the comments from @nikie. What I found out was that the EdgeDetect is very sensitive to the r parameter definition. Besides, "RANSAC" method in ImageLines turned out to be a better predictor than the default "Hough" method.

With the code: Show[EdgeDetect[Binarize@imageC, 2], Graphics[{Orange, Line /@ ImageLines[EdgeDetect[Binarize@imageC, 2], Method -> "RANSAC"]}], ImageSize -> Large] following results were obtained:

And with the code: Show[Binarize@imageC, Graphics[{Orange, Line /@ ImageLines[Binarize@imageC, Method -> "RANSAC"]}], ImageSize -> Large] following lines were obtained:

As already mentioned, the change of method to "RANSAC" with default t and d parameters was crucial improvement in the quality of the results. In this case we have a lot of outliers in the data and "RANSAC" is more suitable in assigning no influence to them than the "Hough" method. I will try your suggestions, too, in order to get the best method for line-pattern recognition.

• – Niki Estner Jun 15 '17 at 7:31
• @nikie: Thanks for sharing. Despite the extensive search, I have not stumbled upon these before. I will try to use them and report results or further questions. – Mike Jun 15 '17 at 8:04

I see two possible solutions here. You can either use the orientation of the white spots to calculate the mean orientation of them all. The other possibility is to acknowledge that there is an underlying grid which is probably what you call pockets.

Let's first try to enhance them

img = Import["https://i.stack.imgur.com/KAJ7r.png"];
Manipulate[
Binarize[img, {t1, t2}],
{t1, 0, 1},
{t2, t1, 1}
]


Beside the large portion of white in the bottom part, the grid becomes visible. Let us use this image and kill the white portion

Manipulate[
ImageMultiply[Binarize[img, {0, t}],
Binarize[GaussianFilter[img, g]]],
{{t, 0.04}, 0, 0.1},
{{g, 20}, 3, 30}
]


That looks like a good start for ImageLines

lines = ImageLines[
ImageMultiply[Binarize[img, {0, 0.04}],
Binarize[GaussianFilter[img, 30]]]];
HighlightImage[img, {Red, Thickness[0.01], Line /@ lines}]


• Do I understand you correctly, that you have obtained the orientation of the lines in the image from the perpendicular pattern (net) and not from the white spots in the original image? – Mike Jun 20 '17 at 7:16

One approach is using the Radon transform:

img = Import["https://i.stack.imgur.com/KAJ7r.png"];


The column of the Radon matrix with the largest sum is the direction with the strongest line:

xaxis = Range[-89, 90];
ListPlot[Transpose[{xaxis, trans}]]
pos = First@First@Position[trans, Max[trans]];
xaxis[[pos]]

-50


which shows that the strongest sum is at about -50 degrees, the amount to rotate the image so that the stripes are vertical.

• Sorry,could I know what is your radDat? – yode Jun 15 '17 at 19:10
• Apologies -- I failed to copy that line. It's now been added. – bill s Jun 15 '17 at 19:11
• As I know,the position of white pixel in radon image is {orient,distance from corner}.But what the magnitude of the pixel mean for?As your answer,it mean the confidence of the orient? – yode Jun 15 '17 at 19:30
• The above sums over the columns of the Radon image -- so we're looking for the column (the orientation) with the largest amount of "white" stuff. – bill s Jun 15 '17 at 19:50
• I'm sorry I'm here again,as my this two examples,I think the abscissa ispresentive of the orient (0~180°) but not your vertical coordinates.. – yode Jun 16 '17 at 19:05

You can use the code from this question more or less without changes:

img = Import["https://i.stack.imgur.com/KAJ7r.png"];

pixels = ImageData[img];


The idea is basically: Treat the gradient (strength and direction) as complex numbers, square them, so gradient vectors pointing in opposite directions have the same phase, then sum them up:

sigma = 10; (*roughly the with of a line*)
GaussianFilter[pixels, sigma, {0, 1}] +
I GaussianFilter[pixels, sigma, {1, 0}];

{w, h} = ImageDimensions[img];
Graphics[{Red,
Table[Line[{{0, y}, {w, w*Cot[angle] + y}}], {y, 0, 2 h, 50}]}]]


ImageRotate[img, angle]


Since we have GradientOrientationFilter,the thing become easer.

img = Import["https://i.stack.imgur.com/KAJ7r.png"];

angles = Rescale[Flatten[ImageData[orient]], {-Pi/2, Pi/2}, {-90, 90}];