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

Posted lx17746071609

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[opencv]利用minAreaRect计算平面矩形的旋转角度相关的知识,希望对你有一定的参考价值。

 

#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的cv2.minAreaRect解析

OpenCV-最小包围旋转矩形边框cv::minAreaRect

OpenCV-最小包围旋转矩形边框cv::minAreaRect

opencv图片倾斜度检测

利用OpenCV的函数warpPerspective()作图像的透视变换

利用OpenCV的函数warpPerspective()作图像的透视变换