Abdullah Al Mubarok
Abdullah Al Mubarok

Reputation: 91

Draw Major Axis and Minor Axis of contour

Contour

So I have the above contour and I want to plot a Major Axis and Minor Axis Line of the contour, what I do is exactly like this

s=regionprops(image,'Centroid','Orientation','MajorAxisLength','MinorAxisLength');

xMajor=s.Centroid(1) + [-1 1]*(s.MajorAxisLength/2)*cosd(s.Orientation);
yMajor=s.Centroid(2) + [-1 1]*(s.MajorAxisLength/2)*sind(s.Orientation);

xMinor=s.Centroid(1) + [-1 1]*(s.MinorAxisLength/2)*sind(s.Orientation);
yMinor=s.Centroid(2) + [-1 1]*(s.MinorAxisLength/2)*cosd(s.Orientation);

line(xMajor,yMajor);
line(xMinor,yMinor);

but what I get so far is the picture below

Result

am I doing it wrong? Thanks in advance.

Upvotes: 2

Views: 1851

Answers (1)

LowPolyCorgi
LowPolyCorgi

Reputation: 5188

For the minor axis, the orientation is s.Orientation+90, so cosd(s.Orientation+90) = sind(s.Orientation) but sind(s.Orientation+90) = -cosd(s.Orientation).

So you have to use:

xMinor=s.Centroid(1) + [-1 1]*(s.MinorAxisLength/2)*sind(s.Orientation);
yMinor=s.Centroid(2) - [-1 1]*(s.MinorAxisLength/2)*cosd(s.Orientation);

Upvotes: 3

Related Questions