有关minAreaRect的坑

有一些帖子只是详细介绍一下,并没有说其中的坑,minAreaRect的坑就是角度,我发现老外讨论如下:

From 

http://stackoverflow.com/questions/24073127/opencvs-rotatedrect-angle-does-not-provide-enough-information, I think this can be solved this way:

@RotatedRect rotated_rect = minAreaRect(contour);
float blob_angle_deg = rotated_rect.angle;
if (rotated_rect.size.width < rotated_rect.size.height) {
  blob_angle_deg = 90 + blob_angle_deg;
}
Mat mapMatrix = getRotationMatrix2D(center, blob_angle_deg, 1.0);@

So, in fact, RotatedRect's angle does not provide enough information for knowing an object's angle, you must also use RotatedRect's size.width and size.height.

Due to the number of questions about this issue in the Internet, maybe this should be clarified. Even if only in the documentation.

Contrarily to most of the other functions in OpenCV's API this feature seems difficult to understand. For instance, see http://shiyuzhao1.wordpress.com/2013/10/08/how-is-the-anlge-in-roatedrect-defined/ where the author writes "I bet you can never guess how the angle of a RoatedRect is defined."

What would be the disadvantages of making RotatedRect's angle consist in an value such that passing the symmetric to e.g. getRotationMatrix2D would allow to deskew the object enclosed by the RotatedRect?

验证了一下,下面的似乎是正解!!!!有的时候theta角会出现-0的情况,实验了几次,猜测-0代表很小的角度,对应下图90°的情况,也就是长边抬起了一个很小很小的角(近似为-0),此时根据x轴逆时针旋转碰到的第一个边是width的原则,此时有width>height的,则最终角度为-0+90°


猜你喜欢

转载自blog.csdn.net/insanegtp/article/details/80105228