Minor and major axis inside elipse

3 views (last 30 days)
Damian
Damian on 24 Jan 2018
Commented: Monica Vazquez on 17 Jan 2019
Hi. I want to plot axis inside elipse in front of my 7 figures, for checking the Orientation angle like that:
Code:
A = imread('4.png');
level = graythresh(A);
if level < 1
level = level +0.20
end
BW = im2bw(A,level);
figure(1), imshow(BW)
BW2 = imcomplement(BW);
figure(2), imshow(BW2)
s = regionprops(BW2, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid', 'Area');
imshow(BW2)
hold on
minArea=5000;
maxArea=40000;
list = find([s.Area] > minArea & [s.Area] < maxArea);
assignin('base','list',list);
display('Index list: ');
display(list);
for k = 1:numel(list)
k_iter = list(k);
c = s(k_iter).Centroid;
text(c(1), c(2)+20, sprintf('%d', k_iter),'HorizontalAlignment', 'center','VerticalAlignment', 'middle');
plot(c(1),c(2),'b*');
end
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
for k = 1:length(s)
xbar = s(k).Centroid(1);
ybar = s(k).Centroid(2);
a = s(k).MajorAxisLength/2;
b = s(k).MinorAxisLength/2;
theta = pi*s(k).Orientation/180;
R = [ cos(theta) sin(theta)
-sin(theta) cos(theta)];
xy = [a*cosphi; b*sinphi];
xy = R*xy;
x = xy(1,:) + xbar;
y = xy(2,:) + ybar;
plot(x,y,'r','LineWidth',2);
end
hold off
Results:

Answers (1)

Image Analyst
Image Analyst on 24 Jan 2018
It's not so easy to find the largest ellipse than can fit inside the shapes. Why do you think you need this?
  5 Comments
Image Analyst
Image Analyst on 24 Jan 2018
5 points are needed to define an ellipse. If you have less than that you'll have to determine the missing ones. You can perhaps take the "missing" ones as the ends of the major axis with the same major axis length as regionprops gives you. So take the convex hull with convhull(). Then if there are less than 5, use the orientation and major axis length to define points out on the tip of the long axis of the ellipse. Then you'll have 5 points and you can determine the ellipse. The attached paper may help.
Monica Vazquez
Monica Vazquez on 17 Jan 2019
Thank you for share with us the paper

Sign in to comment.

Categories

Find more on Polar Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!