从MinAreaRect的函数中,它是否返回0-360度范围内的角度?我不确定,因为我有一个对象,方向是90度左右,但我总是得到-1或-15度。这可能是openCV错误吗?
任何指导都非常感谢。
谢谢
发布于 2013-04-17 00:55:38
我假设您使用的是C++,但如果您使用的是C或Python语言,答案应该是相同的。
函数minAreaRect似乎给出的角度范围从-90度到0度,不包括零,因此间隔[-90,0)。
如果输出的矩形没有旋转,该函数会给出-90度,也就是说,矩形的两条边完全水平,两条边完全垂直。当矩形顺时针旋转时,角度增加(接近于零)。当达到零时,函数给出的角度再次回到-90度。
因此,如果你有一个来自minAreaRect的长方形,并且它是平躺的,minAreaRect会称其角度为-90度。如果您旋转图像,直到minAreaRect给出的矩形完全直立,那么角度将再次显示为-90度。
我实际上对此一无所知(我从我的OpenCV项目中延迟了下来,以了解它是如何工作的:/)。不管怎样,如果我还没有解释清楚的话,这里有一个演示minAreaRect的OpenCV程序:
#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解释的比较。
发布于 2014-01-29 18:09:29
在@Adam Goodwin的回答的基础上,我想添加我的小代码,稍微改变一下行为:
我想要的是较长边和垂直边之间的角度(对我来说,这是思考旋转矩形的最自然的方式):

如果您需要相同的代码,只需使用以下代码:
void printAngle(RotatedRect calculatedRect){
if(calculatedRect.size.width < calculatedRect.size.height){
printf("Angle along longer side: %7.2f\n", calculatedRect.angle+180);
}else{
printf("Angle along longer side: %7.2f\n", calculatedRect.angle+90);
}
}要查看它的实际效果,只需将其插入Adam Goodwin代码中:
printf("Angle given by minAreaRect: %7.2f\n", calculatedRect.angle);
printAngle(calculatedRect);
printf("---\n");发布于 2016-05-05 17:26:53
通过实验,我发现如果长边在底点的左边,角度值在长边和Y+轴之间,而长边在底点的右边,角度值在长边和X+轴之间。所以我使用这样的代码(Java):
rRect = Imgproc.minAreaRect(mop2f);
if(rRect.size.width<rRect.size.height){
angle = 90 -rRect.angle;
}else{
angle = -rRect.angle;
}角度从0到180。
https://stackoverflow.com/questions/15956124
复制相似问题