opencv - MinAreaRect angles - 不确定返回的角度

标签 opencv angle

从 MinAreaRect 的函数来看,它是否返回 0-360 度范围内的角度? 我不确定,因为我有一个方向为 90 度左右的物体,但我一直得到 -1 或 -15 度。这可能是 openCV 错误吗?

非常感谢任何指导。

谢谢

最佳答案

我假设您使用的是 C++,但如果您使用的是 C 或 Python,答案应该相同。

函数 minAreaRect 似乎给出了从 -90 度到 0 度的角度,不包括零,所以间隔为 [-90, 0)。

如果它输出的矩形未旋转,则该函数给出 -90 度,即矩形的两条边完全水平,两条边完全垂直。随着矩形顺时针旋转,角度增加(趋向于零)。当达到零时,函数给出的角度再次回到 -90 度。

因此,如果您从 minAreaRect 中得到一个长矩形,并且它是平放的,则 minAreaRect 会将角度称为 -90 度。如果您旋转图像直到 minAreaRect 给出的矩形完全直立,那么角度将再次显示为 -90 度。

我实际上并不知道这些(我拖延了我的 OpenCV 项目以了解它是如何工作的:/)。不管怎样,这里有一个 OpenCV 程序演示了 minAreaRect 如果我还没有解释清楚的话:

#include <stdio.h>

#include <opencv\cv.h>
#include <opencv\highgui.h>

using namespace cv;

int main() {
    float angle = 0;
    Mat image(200, 400, CV_8UC3, Scalar(0));
    RotatedRect originalRect;
    Point2f vertices[4];
    vector<Point2f> vertVect;
    RotatedRect calculatedRect;

    while (waitKey(5000) != 27) {
        // Create a rectangle, rotating it by 10 degrees more each time.
        originalRect = RotatedRect(Point2f(100,100), Size2f(100,50), angle);

        // Convert the rectangle to a vector of points for minAreaRect to use.
        // Also move the points to the right, so that the two rectangles aren't
        // in the same place.
        originalRect.points(vertices);
        for (int i = 0; i < 4; i++) {
            vertVect.push_back(vertices[i] + Point2f(200, 0));
        }

        // Get minAreaRect to find a rectangle that encloses the points. This
        // should have the exact same orientation as our original rectangle.
        calculatedRect = minAreaRect(vertVect);

        // Draw the original rectangle, and the one given by minAreaRect.
        for (int i = 0; i < 4; i++) {
            line(image, vertices[i], vertices[(i+1)%4], Scalar(0, 255, 0));
            line(image, vertVect[i], vertVect[(i+1)%4], Scalar(255, 0, 0));
        }
        imshow("rectangles", image);

        // Print the angle values.
        printf("---\n");
        printf("Original angle:             %7.2f\n", angle);
        printf("Angle given by minAreaRect: %7.2f\n", calculatedRect.angle);
        printf("---\n");

        // Reset everything for the next frame.
        image = Mat(200, 400, CV_8UC3, Scalar(0));
        vertVect.clear();
        angle+=10;
    }

    return 0;
}

这使您可以轻松地看到手动绘制的矩形的角度和形状与同一矩形的 minAreaRect 解释相比如何。

关于opencv - MinAreaRect angles - 不确定返回的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15956124/

相关文章:

python - 检测红色和绿色圆圈

java - 如何发现两个物体之间的角度?

javascript - 使用横滚,俯仰,偏航和长度确定新的GPS位置

c++ - 从 DXF 文件中解析不完整的椭圆

matlab - 如何在 MATLAB 中显示位于特定角度的箭头?

android - Android:如何将图库中的图像加载到OpenCv Mat变量中?

OpenCV PCA 不可初始化

c++ - OpenCV - 未定义的引用 : SurfFeatureDetector and BruteForceMatcher

python - 如何在Python中将视频分成4个相等的部分?

python - 在不知道第二个坐标的情况下获取直线上的点/像素