OpenCV rotation angle does not provide enough information

From my experiments, the angle returned by the RotatedRect angle variable varies from -90 to 0 degrees, which is not enough to determine if the object is tilted left or right.

For example, if the angle is -45 degrees, we cannot say whether we need to rotate +45 or -45 degrees to align it.

Excerpt from the code I use:

RotatedRect rotated_rect = minAreaRect(contour); float blob_angle_deg = rotated_rect.angle; Mat mapMatrix = getRotationMatrix2D(center, blob_angle_deg, 1.0); 

Leaning on an object in one direction, I get angles from 0 to -90 degrees, and when you tilt the object to another direction, I get angles from -90 to 0 degrees.

How can I find the angle at which I should rotate the image on the table?

+7
opencv rotation
source share
3 answers

Having received answers from Sebastian Schmitz and Michael Burdinov, I realized how I decided:

 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); 

Thus, in fact, the RotatedRect angle does not provide enough information to know the angle of the object, you should also use RotatedRect size.width and size.height .

+13
source share

Switching the width and height of the rectangle is the same as turning it 90 degrees. Therefore, if the range of angles was equal to 180 degrees instead of 90, then the same rectangle would have 2 representations (width, height, angle) and (height, width, angle + 90). Having a range of 90 degrees, you can represent each rectangle, and you can do it in only one way.

+3
source share

I explained how you can convert the angle of the rectangle in [0-180] to this stream .

The angle is always calculated along the longer side.

+3
source share

All Articles