OpenCV Python中旋转矩形的角落顺序

4

我正在进行一个项目,使用Python 3上的OpenCV,在实时摄像头中检测形状和一些属性。其中一个形状是矩形,所以我使用旋转矩形(或最小面积矩形)来提取有用的信息:

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

该函数能够自动检测矩形的四个角,并按照它们的坐标进行排序。例如,当矩形被旋转时,角的顺序也会改变,如下面的gif所示: enter image description here 有没有一种方法可以使角的顺序在旋转时不改变?矩形的宽度和高度取决于角的顺序,因此稍微倾斜一下角度就可以使高度变为宽度,反之亦然。

两个连续帧之间的最大旋转角度是否有限制?如果有,当您找到第(i+1)帧的角点时,请根据它们之间的距离将所有先前的角点与当前角点进行匹配。如果没有限制,那么两个连续帧可以具有任意旋转角度,除非有额外的信息,否则无法完成此操作。 - unlut
看起来第一个角落(编号0)是右下角顶点。 - fmw42
1个回答

2
我将避免使用gif。相反,我会分享一些图像然后解释。rotWidth和rotHeight显示从旋转矩形中获得的值。而Correct Width,Correct Height则显示它们根据角度的修正值。

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

如下所示,MinAreaRect 获得的角度变化在 (0, -10,...., -80, -90] 之间。然而,这并不足以找出对象的真实宽度和高度。当我们改变对象的角度时,观察到 rotWidth 和 rotHeight 发生变化。因此,我计算了一个“新角度”,它在 (0, 10,....., 170, 180] 之间变化。现在根据 rotWidth 是否大于 rotHeight,我计算正确的宽度和高度。以下是代码片段:
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;
            }

上面的代码是使用C#中的EmguCV编写的。现在宽度和高度不会改变,这会有帮助吗?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接