How to plot a line with angle of 'Orientation' and length of 'MajorAxisLength' through 'Centroid'?

21 views (last 30 days)
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
  1 Comment
Nici Me
Nici Me on 29 May 2017
st = regionprops(Image,'Centroid','Orientation','MajorAxisLength', 'Extrema');
line([st.Extrema(7),st.Extrema(4)],[st.Extrema(15),st.Extrema(12)], 'Color', 'red', 'Linestyle', '--');
plot(st.Extrema(4),st.Extrema(12), 'r*')
plot(st.Extrema(7),st.Extrema(15), 'g*')

Sign in to comment.

Accepted Answer

David Young
David Young on 22 Jan 2015
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);
  3 Comments
Maria Pardo Garcia
Maria Pardo Garcia on 5 May 2017
I'm having some problems implementing this code, because it tells me:
Error using /
Too many input arguments.
How can I fix it? I want to do this because I will like to understand the values that ' Orientation' gives
Thank you in advanced
María
Image Analyst
Image Analyst on 5 May 2017
Maria: The code was meant for the case where you had only one blob. You have multiple blobs so you'll have to index st and do it for every blob that st has information on. If you have no idea how to do it, post your image and your code to a new question (not here).

Sign in to comment.

More Answers (1)

Sid
Sid on 21 Jan 2015
Edited: Sid on 21 Jan 2015
Hi Ivan,
Would this example from Steve be helpful?
Essentially, I grabbed what Steve had done before, and tinkered it a little bit with the direction you had taken. I am using 'rice.png' as my source image.
I simply divided the Major/Minor Axis Lengths by 2, and then also changed the angle for Minor Axis orientation.
Hope this helps!
Cheers,
Sid
_____________________________________
clc; clear all; close all;
I = imread('rice.png');
bw = im2bw(I,graythresh(I));
bw = bwareaopen(bw,10);
s = regionprops(bw, 'Orientation', 'MajorAxisLength', ...
'MinorAxisLength', 'Eccentricity', 'Centroid');
imshow(bw)
hold on
phi = linspace(0,2*pi,50);
cosphi = cos(phi);
sinphi = sin(phi);
for k = 1:length(s)
% k = 10;
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);
hold on
% Major Axis Values
xMajor1 = xbar + (((s(k).MajorAxisLength)./2) * cosd(s(k).Orientation));
yMajor1 = ybar - (((s(k).MajorAxisLength)./2) * sind(s(k).Orientation));
xMajor2 = xbar - (((s(k).MajorAxisLength)./2) * cosd(s(k).Orientation));
yMajor2 = ybar + (((s(k).MajorAxisLength)./2) * sind(s(k).Orientation));
line([xMajor1 xMajor2],[yMajor1 yMajor2],'color','g')
% Minor Axis Values
xMinor1 = xbar + (((s(k).MinorAxisLength)./2) * sind((180-s(k).Orientation)));
yMinor1 = ybar - (((s(k).MinorAxisLength)./2) * cosd((180-s(k).Orientation)));
xMinor2 = xbar - (((s(k).MinorAxisLength)./2) * sind((180-s(k).Orientation)));
yMinor2 = ybar + (((s(k).MinorAxisLength)./2) * cosd((180-s(k).Orientation)));
line([xMinor1 xMinor2],[yMinor1 yMinor2],'color','c')
end
hold off
  2 Comments
Ivan Shorokhov
Ivan Shorokhov on 22 Jan 2015
Hi Sid,
Thank you for your interest and willing to help. I already saw this example and tried to use it,but unfortunately it didn't help a lot...
David Young
David Young on 22 Jan 2015
Looks a bit more complex than is needed, Sid. It's most help if you give simple solutions with the minimum of code.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!