MATLAB Answers


I have extracted an image object, now i need to determine the line of symmetry for that object

Asked by Optical_Stress on 25 Apr 2017
Latest activity Commented on by Image Analyst
on 26 Apr 2017
I have an object of which there is a symmetrical pattern, i want to plot a line of symmetry and then determine the array co-ordinates of the boundary point of the that line of symmetry. For example consider below:
How could i go about detecting the line of symmetry for the object?


Sign in to comment.

3 Answers

Answer by Image Analyst
on 26 Apr 2017
 Accepted Answer

Try the attached test.m file, below this image it creates.


Show 1 older comment
That's the angle of the line - nearly 90 degrees, straight up. The line is along the major axis of an ellipse fitted to the binary image. Note that that line may not exactly correspond to what you might think the axis should be when looking at the gray scale image. That could be because the outer "hull" is an ellipse with one orientation and the stripes inside seem a bit rotated with respect to the outer hull of the object. If you want the inner stripes, you might have to try to get those by getting the outer hull, like I did, then eroding it about 5 or 6 pixel layers with imerode(), then mask the image to blacken the outer layers and just give an inner stripes image. Then threshold and get multiple blobs representing the stripes. Maybe try to process further so that you have just the two main stripes. Then take the orientation of all the blobs and average them together.
Or maybe a Hu moment can give you the orientation of the gray image directly.
This images i produced were actually already ones with masked backgrounds, what i'd like to know from your results is how i can rotate the vertical line so i can check how well it passes through the original image.
I tried rotating the values by
slope = atand(orientation);
y = slope * (x - xCentroid) + yCentroid;
y=y*sind(slope); x=(x)*cosd(slope);
hold on;
plot(x, y, 'r-', 'LineWidth', 3);
but this did not work unfortunately, I have the equation of the circle, i want to use this to determine the intersection point with the straight line. I might just do it using imtool and hovering over the point if it doesn't work out.
Thanks though, you're input has been really helpful.
The line does not need to be rotated since it already goes through the main axis of the binary image. Like I said, if you don't like the overall binary image, you can get a different, smaller one to try to find just the stripes, and recompute the angles.

Sign in to comment.

Answer by Walter Roberson
on 26 Apr 2017

data = double(YourImage);
dv = data(:);
try_it = @(ang) sum((reshape(fliplr(imrotate(data,ang,'crop')),[],1) - dv).^2)
A = linspace(0,359.9,500);
fitr = arrayfun(try_it, A);
[~,idx] = min(fitr);
best_ang = A(idx);
Note: what you posted was an object in a white frame. The YourImage I indicate above should have that white frame cropped away (e.g., should be the original image.)
The code here will work for grayscale and color both.
What this does is rotates an image by an angle, with cropping, flips it left to right, and finds the euclidean distance between that and the original. The hypothesis being tested is that there is an axis of reflective symmetry running though the center of the image and that it is just necessary to find the correct angle for it.
This will probably not do exactly what you want, in that your hypothesis probably involves an axis of reflective symmetry to does not run through the center of the image. You should be able to extend the technique to two parameters and evaluating at a grid of value pairs.
fmincon() cannot really optimize this, as it is not a continuous problem: small differences in rotation angles lead to the same output.

  1 Comment

Hi, thanks for your response. Could you explain a bit more regarding the code?
What is the Linspace part for?
I have just run the code, from what i've seen it seems to have just reflected the image about the center....?

Sign in to comment.

Answer by Image Analyst
on 26 Apr 2017

What if you threshold, then use bwconvhull() to get the convex hull of all blobs, then use regionprops to get the centroid and orientation angle?


Sign in to comment.