Order of corners in a rotating rectangle in OpenCV Python

Viewed 734

I am working on a project where I'm using OpenCV on Python 3 to detect shapes and some properties using a camera in real-time. Ones of the shapes is a rectangle so I used Rotating Rectangle (or minimum area Rectangle) to extract useful information about it:

                #Rotating Rectangle
                rect = cv2.minAreaRect(approx)
                box = cv2.boxPoints(rect)
                box = np.int0(box)

This function detects the corners of the rectangle automatically and orders them depending to their coordinates. i.e the order of the corners changes when the rectangle is rotated, like the gif below:

enter image description here

Is there a way to have the order not change with rotation? The width and height of the rectangle depend on the order, so the height can become the width with a sleight angle change and vise versa.

1 Answers

I will avoid putting a gif. Rather i will share a few images and then explain. the rotWidth and rotHeight shows the value obtained from the rotated rectangle. And Correct Width , Correct Height shows the corrected values of them depending on the angle.

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

As seen the angle obtained after MinAreaRect varies between (0, -10,...., -80, -90]. however this is not sufficient to find out the true width and height of the object. this variation is observed in rotWidth and rotHeight as we change the angle of object. hence i calculate a "NEW angle" which varies between (0, 10,....., 170, 180]. now depending on whether rotWidth is greater or less than rotHeight, I compute the Correct Width , Correct Height. here is the code snippet :

RotatedRect rotatedRect = CvInvoke.MinAreaRect(singleContour);
            vertices = rotatedRect.GetVertices();

            angle = rotatedRect.Angle;
            double newAngle = 0.0; 

            rotWidth = rotatedRect.Size.Width;
            rotHeight = rotatedRect.Size.Height;

            if (rotHeight > rotWidth)
            {
                newAngle = (-1) * angle;
            }
            else
            {
                newAngle = 90 - angle;
            }

            if (newAngle >= 0 && newAngle < 90)
            {
                correctWidth = rotWidth;
                correctHeight = rotHeight;
            }
            if (newAngle == 90)
            {
                correctWidth = rotWidth;
                correctHeight = rotHeight;
            }
            if (newAngle > 90 && newAngle < 180)
            {
                correctWidth = rotHeight;
                correctHeight = rotWidth;
            }
            if (newAngle == 180)
            {
                correctWidth = rotHeight;
                correctHeight = rotWidth;
            }

the code above is written using EmguCV in C#. now that width and height does not change, will this help you?

Related