minarearect 源码_MinAreaRect角度-不确定返回的角度

OpenCV的minAreaRect函数返回的角度范围从-90到0度,不包括0度。-90度表示矩形未旋转,随着矩形顺时针旋转,角度增加。当角度达到0度时,函数会再次返回-90度。提供的代码示例展示了minAreaRect如何处理旋转矩形并显示其角度变化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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?

Any guidance much appreciated.

Thanks

解决方案

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

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

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.

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.

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

#include

#include

using namespace cv;

int main() {

float angle = 0;

Mat image(200, 400, CV_8UC3, Scalar(0));

RotatedRect originalRect;

Point2f vertices[4];

vector 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;

}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值