MinAreaRect 角度 - 不确定返回的角度 [英] MinAreaRect angles - Unsure about the angle returned

查看:33
本文介绍了MinAreaRect 角度 - 不确定返回的角度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

From the functions for MinAreaRect, does it return angles in the range of 0-360 degrees? I am unsure as i have an object that is oriented at 90 degrees or so but I keep getting either -1 or -15 degrees. Could this be an openCV error?

非常感谢任何指导.

谢谢

推荐答案

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

I'm going to assume you're using C++, but the answer should be the same if you're using C or Python.

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

The function minAreaRect seems to give angles ranging from -90 to 0 degrees, not including zero, so an interval of [-90, 0).

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

The function gives -90 degrees if the rectangle it outputs isn't rotated, i.e. the rectangle has two sides exactly horizontal and two sides exactly vertical. As the rectangle rotates clockwise, the angle increases (goes towards zero). When zero is reached, the angle given by the function ticks back over to -90 degrees again.

因此,如果您有一个来自 minAreaRect 的长矩形,并且它是平躺的,minAreaRect 将调用 -90 度角.如果旋转图像直到 minAreaRect 给出的矩形完全直立,那么角度将再次显示 -90 度.

So if you have a long rectangle from minAreaRect, and it's lying down flat, minAreaRect will call the angle -90 degrees. If you rotate the image until the rectangle given by minAreaRect is perfectly upright, then the angle will say -90 degrees again.

我实际上一无所知(我拖延了我的 OpenCV 项目以了解它是如何工作的:/).无论如何,这是一个演示 minAreaRect 的 OpenCV 程序,如果我还没有解释清楚的话:

I didn't actually know any of this (I procrastinated from my OpenCV project to find out how it works :/). Anyway, here's an OpenCV program that demonstrates minAreaRect if I haven't explained it clear enough already:

#include <stdio.h>

#include <opencvcv.h>
#include <opencvhighgui.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("---
");
        printf("Original angle:             %7.2f
", angle);
        printf("Angle given by minAreaRect: %7.2f
", calculatedRect.angle);
        printf("---
");

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

    return 0;
}

这让您可以轻松查看手动绘制的矩形的角度和形状与相同矩形的 minAreaRect 解释的比较.

This lets you easily see how the angle, and shape, of a manually drawn rectangle compares to the minAreaRect interpretation of the same rectangle.

这篇关于MinAreaRect 角度 - 不确定返回的角度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆