MATLAB: How to plot a line with angle of ‘Orientation’ and length of ‘MajorAxisLength’ through ‘Centroid’

centroidimageimage processingmajoraxislengthorientationregionprops

Dear All,
I would like to plot a line with angle of 'Orientation' and length of 'MajorAxisLength' through 'Centroid'.
Similar as shown in image bellow:
The code I'm using right now, is piloting the line with the right angle, but it starts from the center
st = regionprops(Image,'Centroid','Orientation','MajorAxisLength');
x = st.Centroid(1) + st.MajorAxisLength * cosd(st.Orientation);
y = st.Centroid(2) - st.MajorAxisLength * sind(st.Orientation);
line([c(1) x],[c(2) y]);
How can do it right, maybe I should use BoundingBox as well?
Thanks in advance for any help,
I

Best Answer

Your original code is almost right. You just need to think through the geometry a little more. This works:
st = regionprops(Image,'Centroid','Orientation','MajorAxisLength');
hlen = st.MajorAxisLength/2;
xCentre = st.Centroid(1);
yCentre = st.Centroid(2);
cosOrient = cosd(st.Orientation);
sinOrient = sind(st.Orientation);
xcoords = xCentre + hlen * [cosOrient -cosOrient];
ycoords = yCentre + hlen * [-sinOrient sinOrient];
line(xcoords, ycoords);