Regionprops does not return the correct angle of a line - what am I doing wrong?
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I filmed a pendulum swinging. I've taken a frame out of the video, and the image looks like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160356/image.jpeg)
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/160357/image.jpeg)
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?
0 个评论
采纳的回答
Walter Roberson
2017-2-2
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 个评论
Image Analyst
2017-2-3
Either
stats = regionprops(bwlabel(~BW), 'Area', 'Orientation','Centroid','BoundingBox');
or
BW = S_plane > thresh;
stats = regionprops(bwlabel(BW), 'Area', 'Orientation','Centroid','BoundingBox');
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!