Tag Archives: angle

OpenCV : Determine orientation of ellipse/RotatedRect in FitEllipse()

In my other post, I examined the RotatedRect of minAreaRect() function. But later i found out that the ellipse/RotatedRect of minFitEllipse() function doesn’t work the same way as that of minAreaRect(). (Looks like the two developers didn’t talk to each other before developing these functions !)

So, i again examined the RotatedRect extraacted from ellipse of the minFitEllipse() function and here are the results:

  1. The angle property is the angle between vertical and the edge of 0th and 1st vertices of the rectangle.
  2. If the angle is less than 90 degrees the 0th vertex if the left most (or left-bottom if angle is 0 degrees) vertex. 1st, 2nd, 3rd vertices follow in clockwise manner.
  3. If the angle is greater than or equal to 90 degrees (but < 180), 0th vertex is the top most vertex (or top left vertex if angle == 90). 1st, 2nd, 3rd vertices follow in clockwise manner.
  4. Height is always greater than (or equal to) Width.

 

This is a code snippet i used to get and draw RotatedRect of fitellipse() function.

RotatedRect box = fitEllipse(contour);
Point2f vtx[4];
box.points(vtx);
for( int j = 0; j < 4; j++ )
    line(drawing, vtx[j], vtx[(j+1)%4], Scalar(0,255,0),2);

 

Below are test cases for various test objects (in white). Green rectangle is the RotatedRect of fitEllipse() function.

Cases :  0 <= Angle < 90

result_brickright_fitellipseresult_barright_fitellipseresult_barSqrVer_fitelliipse

 

Cases :  90 <=Angle < 180

result_brickleft_fitellipseresult_barleft_fitellipseresult_barSqrHor_fitelliipseresult_barHor_fitelliipseresult_barVer_fitellipse

 

 

 

Open CV : Determine angle of RotatedRect / minAreaRect

I was working on an OpenCV project which needed to determine Angle of a RotatedRect extracted from a contour using minAreaRect() function. Unknown of the weird and intelligent logic behind the “angle” property of the RotatedRect, my results weren’t as expected. It took me a while to understand whats going on with the determined RotatedRect properties.

Unable to finding quick info about the properties of RotatedRect in web search results. I decided to inspect it myself … and here are the results.

  1. The lowest point in a rectangle is 0th vertex, and 1st, 2nd, 3rd vertices follow clockwise.
  2. Height is distance between 0th & 1st  (or 2nd & 3rd) vertices. And width is distance between 1st  & 2nd (or 0th & 3rd) vertices.
  3. Angle is calculated from the horizontal to the first edge of rectangle, counter clockwise.
  4. Angle varies between -0 to -90 (I am unsure, what is the decisive factor of -0 or -90)

 

result_barleft result_barright result_brickrightresult_brickleftresult_barSqrHorresult_barSqrVerresult_barHorresult_barVer

For OpenCV a contour is just a pattern, it doesn’t understand how much it is rotated in real life. OpenCV will get the minAreaRect and see it only as a rectangle which rotated from the horizontal (upto 90 degrees). if your object is rotated more than (-)90 degrees, next edge of the rectangle becomes the leading edge and is used to calculate the angle from the horizontal (between -0 to -90). So you have to take care of this in your code to figure out how much the contour has rotated in real life i.e. from 0 to 359 degrees.

Tip: if your contour is of fixed shape, rotating it will make the Height & Width of the RotatedRect flip on every 90 degrees boundary.

The below code snippet can give you the rotation angle from 0 to -180.

double myContourAngle = myRotatedRect.angle;
if (myRotatedRect.size.width < myRotatedRect.size.height) {
  myContourAngle = myContourAngle - 90;
}