Minor and major axis inside elipse
조회 수: 3 (최근 30일)
이전 댓글 표시
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:

댓글 수: 0
답변 (1개)
Image Analyst
2018년 1월 24일
It's not so easy to find the largest ellipse than can fit inside the shapes. Why do you think you need this?
댓글 수: 5
Image Analyst
2018년 1월 24일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Polar Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
