can any one correct me this code please??
1 回表示 (過去 30 日間)
古いコメントを表示
Can you correct me this code to draw the 4 axis (major axe, minor axe and the 2 diagonal and ortogonal axes) not the ellipse
bw = imread('13.jpg');
bw = bwlabel(bw);
imshow(bw)
s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid');
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
xbar = s.Centroid(1);
ybar = s.Centroid(2);
a = s.MajorAxisLength/2;
b = s.MinorAxisLength/2;
theta = pi*s.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);
hold off
thanks
4 件のコメント
回答 (4 件)
Thomas
2012 年 9 月 18 日
編集済み: Thomas
2012 年 9 月 18 日
bwlabel works on a 2D matrix, Your 13.jpg image is probably a color (rgb) image and hence is a 3 dimensional matrix. You could convert it to grayscale ( which is a 2d matrix)
bw2 = imread('13.jpg');
% bw2= rgb2gray(bw2); If image is binary
bw2 = bwlabel(bw2);
imshow(bw2)
s = regionprops(bw2, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid');
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
xbar = s.Centroid(1);
ybar = s.Centroid(2);
a = s.MajorAxisLength/2;
b = s.MinorAxisLength/2;
theta = pi*s.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);
hold off
% ADDED EDIT
plot(s.Centroid(1),s.Centroid(2),'*')
line(s.Centroid(1)-a:s.Centroid(1)+a,s.Centroid(2));
line(s.Centroid(1),s.Centroid(2)-b:s.Centroid(2)+b);
4 件のコメント
Image Analyst
2012 年 9 月 19 日
I think it's because you asked for major and minor axis when what you want does not match regionprops definition of those. See my answer.
Azzi Abdelmalek
2012 年 9 月 18 日
編集済み: Azzi Abdelmalek
2012 年 9 月 18 日
The corrected code
bw = imread('13.jpg');
bw = bwlabel(bw);
imshow(bw)
s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid');
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
xbar = s.Centroid(1);
ybar = s.Centroid(2);
a = s.MajorAxisLength/2;
b = s.MinorAxisLength/2;
theta = pi*s.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);
hold off
Pamela Paolo
2012 年 9 月 18 日
2 件のコメント
Image Analyst
2012 年 9 月 19 日
Pamela, people are giving you what you asked for which I don't think is what you want. See my answer.
Image Analyst
2012 年 9 月 19 日
編集済み: Image Analyst
2012 年 9 月 19 日
Pamela, My guess is that you really mean Feret Diameter ( http://www.sympatec.com/Science/Characterisation/05_ParticleShape.html) rather than the major and minor axis of an ellipse fitted to the shape. Am I correct? Feret diameter will touch at least two points on the perimeter of the object, like a caliper distance, while an ellipse's axes won't necessarily have the axis endpoints on the shape's perimeter.
MATLAB does not provide Feret diameters. You can calculate it yourself using bwboundaries. Let me know if you can't figure it out. I don't have any code all ready to give you though. However, you might take a look at this File Exchange submission: http://www.mathworks.com/matlabcentral/fileexchange/30402-feret-diameter-and-oriented-box
6 件のコメント
Image Analyst
2012 年 9 月 19 日
PCA would probably be good. I don't have any PCA code to share with you, sorry.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!