Clear Filters
Clear Filters

Regionprops does not return the correct angle of a line - what am I doing wrong?

1 view (last 30 days)
Hi,
I filmed a pendulum swinging. I've taken a frame out of the video, and the image looks like this:
Now, I used the following code to get a nice black line:
thresh = 0.7701;
%HSV Color space
I = imread('frame4.jpg');
H = rgb2hsv(I);
H_plane=H(:,:,1);
S_plane=H(:,:,2);
V_plane=H(:,:,3);
imshow(S_plane);
BW = S_plane < thresh;
imshow(BW)
So far so good.
However, when I run regionprops to get an angle, as follows:
% Carrying out regionprops to calculate the ellipse which will give an
% angle.
stats = regionprops(BW, {'Area', 'Orientation','Centroid','BoundingBox'});
[~,I] = max([stats(:).Area]);
props = stats(I);
qR = -999;
% lastAngle = 0;
if(isempty(props) == 0)
qR = props.BoundingBox(1) + props.BoundingBox(3)/2 < props.Centroid(1);
end
% Using these values, a vertical line returns an angle of 180, while a
% horizontal line returns an angle of 270.
if( qR == 1)
theta = 270-props.Orientation; %%(was 270)
elseif (qR == 0)
theta = 90-props.Orientation; %%(was 90)
% else
% theta = lastAngle;
end
theta
I get a value of 90.0188.
Other stills taken at different angles return a very similar value, or a value very close to 270.
What is going wrong here?
Edit: Thanks to Walter. In fact, I realised that around the same time that you posted. However, now I'm analysing 20 frames at once and I'd really like when the line is in the vertical position to be zero: Anything to the left would then be negative, anything to the right would be positive.
I've been trying various methods of achieving this, none of which are working.
Any ideas?

Accepted Answer

Walter Roberson
Walter Roberson on 2 Feb 2017
regionprops on binary looks at 1 values to select the locations to be measured. Your image is mostly 1's, all of the white. You are measuring the properties of the white. You should be using regionprops on ~BW
  3 Comments
Image Analyst
Image Analyst on 3 Feb 2017
Either
stats = regionprops(bwlabel(~BW), 'Area', 'Orientation','Centroid','BoundingBox');
or
BW = S_plane > thresh;
stats = regionprops(bwlabel(BW), 'Area', 'Orientation','Centroid','BoundingBox');

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!