MinAreaRect の関数から、0 ~ 360 度の範囲の角度が返されますか? オブジェクトの向きが 90 度かそこらであるため、確信が持てませんが、-1 度または -15 度のいずれかになり続けます。これはopenCVエラーでしょうか?
どんなガイダンスでも大歓迎です。
ありがとう
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 <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;
}
This lets you easily see how the angle, and shape, of a manually drawn rectangle compares to the minAreaRect
interpretation of the same rectangle.
@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 Goodwins のコードに挿入します。
printf("Angle given by minAreaRect: %7.2f\n", calculatedRect.angle);
printAngle(calculatedRect);
printf("---\n");
実験の結果、長辺が下の Point の左側にある場合、角度の値は長辺と Y+ 軸の間であることがわかりましたが、長辺が下の Point の右側にある場合、角度の値は長い間です側面と X+ 軸。だから私はこのようなコードを使用します(Java):
rRect = Imgproc.minAreaRect(mop2f);
if(rRect.size.width<rRect.size.height){
angle = 90 -rRect.angle;
}else{
angle = -rRect.angle;
}
角度は 0 ~ 180 です。