利用minAreaRect计算平面矩形的旋转角度

来源:互联网 发布:list转json后的格式 编辑:程序博客网 时间:2024/05/17 00:49
#include "opencv2/core/core.hpp"  #include "opencv2/highgui/highgui.hpp"  #include "opencv2/imgproc/imgproc.hpp"  #include "opencv2/features2d/features2d.hpp"    //需要添加该头文件  #include <iostream>  #include "Math.h"27using namespace cv;using namespace std;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;}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

参考:
http://stackoverflow.com/questions/15956124/minarearect-angles-unsure-about-the-angle-returned



原文地址:http://blog.csdn.net/yanxiaopan/article/details/53240098

0 0
原创粉丝点击