码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-09-24 11:59:21      阅读:110      评论:0      收藏:0      [点我收藏+]

标签:++   opencv   平面   original   sam   next   pre   添加   core   

 

#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"27
using 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;
}

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

标签:++   opencv   平面   original   sam   next   pre   添加   core   

原文地址:https://www.cnblogs.com/lx17746071609/p/11577382.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!