can any one correct me this code please??

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 commentaires

Azzi Abdelmalek
Azzi Abdelmalek le 18 Sep 2012
what is J on line2 ?
I'm sorry
bw = bwlabel(bw);
Azzi Abdelmalek
Azzi Abdelmalek le 18 Sep 2012
what is k? have you read errors messages?
Pamela Paolo
Pamela Paolo le 18 Sep 2012
I'm sorry I corrected it

Connectez-vous pour commenter.

Réponses (4)

Thomas
Thomas le 18 Sep 2012
Modifié(e) : Thomas le 18 Sep 2012
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 commentaires

Pamela Paolo
Pamela Paolo le 18 Sep 2012
13.jpg is a binary image, i want to draw the 4 axis for the region of interest (white)
Thomas
Thomas le 18 Sep 2012
Modifié(e) : Thomas le 18 Sep 2012
See edit for major and minor axes..
Pamela Paolo
Pamela Paolo le 18 Sep 2012
i can't understand what do you mean :(
Image Analyst
Image Analyst le 19 Sep 2012
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.

Connectez-vous pour commenter.

Azzi Abdelmalek
Azzi Abdelmalek le 18 Sep 2012
Modifié(e) : Azzi Abdelmalek le 18 Sep 2012
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

1 commentaire

Pamela Paolo
Pamela Paolo le 18 Sep 2012
This code also draw the ellipse not the 4 axis (major axe, minor axe and the 2 diagonal and ortogonal axes)

Connectez-vous pour commenter.

Pamela Paolo
Pamela Paolo le 18 Sep 2012

0 votes

It seems that my question is not clear I will try to explain more. This code allows you to trace the ellipse while I do not need this ellipse, I only need to draw 4 axes (major axis, minor axis, and the two diagonal orthogonal axes)

2 commentaires

Thomas
Thomas le 19 Sep 2012
I have edited my code .. Please try it and see if it works..
Image Analyst
Image Analyst le 19 Sep 2012
Pamela, people are giving you what you asked for which I don't think is what you want. See my answer.

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 19 Sep 2012
Modifié(e) : Image Analyst le 19 Sep 2012

0 votes

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 commentaires

Pamela Paolo
Pamela Paolo le 19 Sep 2012
Good morning, Thanks but the use of the Feret Diameter is not the best solution for my case because: First, I need the 2 principals axes (major and minor) to study the asymmetry of the region of interest. So the region of interest should be traversed by two axes at 90° passing through the center of gravity and which were positioned to produce the lowest possible score of asymmetry.
Image Analyst
Image Analyst le 19 Sep 2012
Then why isn't the elliptical fit working for you? Can you upload your image somewhere so we can see your objects?
Pamela Paolo
Pamela Paolo le 19 Sep 2012
Modifié(e) : Pamela Paolo le 3 Jan 2013
The code in the fisrt message can draw an ellipse: http://s8.postimage.org/d9z0477b9/ellipse.jpg but first i want to draw the 2 axes to study the asymmetry of the region of interest in colors and differential structures. So the region of interest should be traversed by two axes at 90° passing through the center of gravity and which were positioned to produce the lowest possible score of asymmetry: a something like that (the images that i will use them are color images where the region of interest have differents colors and the bakground is black. I'll finish the segmentation later) : http://s16.postimage.org/lkdn7dx/axe2.jpg . The aim is to have a result: asymmetric along the axis 1, asymmetric along the axis 2 or symmetric Then i should draw the 4 axes: http://s8.postimage.org/rqvga9k5/axe.jpg
Image Analyst
Image Analyst le 19 Sep 2012
I didn't run any of their code. Does they not draw lines along the major and minor directions? If they don't, I could help. If they do, then what's the problem? Do you not know how to chop your region up into quadrants? If you do, then what do you do with the pixels? Why don't you look at image moments: http://en.wikipedia.org/wiki/Image_moments, http://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/OWENS/LECT2/node3.html to determine symmetry?
Pamela Paolo
Pamela Paolo le 19 Sep 2012
Modifié(e) : Pamela Paolo le 19 Sep 2012
The code in my first message can draw an ellipse and it can't draw the axis. So I should correct it. The segmentation had a good result (color image) but i uploaded just the binary masque because i can't use the regionprops with a color image. I will try later to use these axes with the color image. To study the asymmetry can I use the PCA?? I thik it's a good solution to study the asymmetry in a color image but i can't understand it very well. Have you please a matlab code because i couldn't implement it??
Image Analyst
Image Analyst le 19 Sep 2012
PCA would probably be good. I don't have any PCA code to share with you, sorry.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by