如何从与轴对齐的边界框中获得矩形倾斜?

问题描述 投票:1回答:2

我的二进制图像上有已知大小的矩形旋转对象。我想使用MATLAB的regionprops返回的与轴对齐的边界框来获得对象的倾斜度。我有什么建议:

使边界框的宽度为W,矩形的边为C且倾斜度为alpha

然后

enter image description here

使用Weierstrass替代

enter image description here

经过一些简化:

enter image description here

enter image description here求解tan(alpha / 2)的方程

enter image description here

对于任何非零倾角判别为正。

enter image description here

逻辑似乎可以,就像数学一样。您能指出我做错了什么吗,或者有什么更好的方法来获得成功?

这里是对应的MATLAB代码:

img = false(25,25);
img(5:16,5:16) = true;
rot_img = imrotate(img, 30, 'crop');
props = regionprops(bwlabel(rot_img),'BoundingBox');
bbox = cat(1,props.BoundingBox);
w = bbox(3);
h = 12;
a = -1*(1+w/h); b = 2; c = 1 - w/h;
D = b^2 - 4*a*c;
alpha = 2*atand((-b + sqrt(D))/(2*a));
%alpha = 25.5288

编辑感谢您的三角学提示。它们大大简化了计算,但给出了错误的答案。据我了解,这个问题是用错误的方式提出的。我真正需要的是找到具有高精度(+/- 0.5度)的短线(10-50像素)的倾斜度,这些线的位置不重要。

问答中使用的方法对于长行显示了更好的精度,因为c = 100误差小于0.1度。这意味着我们这里陷入光栅化误差,并且需要亚像素精度。目前,我只有一种算法可以解决该问题-Radon变换,但希望您能推荐其他方法。

p = bwperim(rot_img);
theta=0:0.1:179.9;
[R,xp] = radon(p,theta); %Radon transform of contours
a=imregionalmax(R,true(3,3)); %Regional maxima of the transform
[r,c]=find(a); idx=sub2ind(size(a),r,c); maxvals=R(idx);
[val,midx]=sort(maxvals,'descend'); %Choose 4 highest maxima
mean(rem(theta(c(midx(1:4))),90)) %And average corresponding angles
%29.85

我的二进制图像上有已知大小的矩形旋转对象。我想使用MATLAB的regionprops返回的与轴对齐的边界框来获得对象的倾斜度。我的建议是:...

matlab image-processing transform transformation
2个回答
1
投票

如果矩形是正方形:


0
投票

可能与带'Orientation'选项的regionprops函数一起使用...

© www.soinside.com 2019 - 2024. All rights reserved.