本文主要是介绍opencv rotatedrect 获取顶点,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
OpenCV对图片中的RotatedRect进行填充
class CV_EXPORTS RotatedRect
{
public://构造函数RotatedRect();RotatedRect(const Point2f& center, const Size2f& size, float angle);RotatedRect(const CvBox2D& box);void points(Point2f pts[]) const;//!返回矩形的4个顶点Rect boundingRect() const; //返回包含旋转矩形的最小矩形operator CvBox2D() const; //!转换到旧式的cvbox2d结构Point2f center; //矩形的质心Size2f size; //矩形的边长float angle; //旋转角度,当角度为0、90、180、270等时,矩形就成了一个直立的矩形
};
#include<opencv2/opencv.hpp>
#include<iostream>
using namespace std;
using namespace cv;int main()
{Mat img = imread("C:/1.jpg");Mat img_gray;cvtColor(img, img_gray, COLOR_RGB2GRAY);img_gray = img_gray > 30;vector<vector<Point>>contours;vector<Vec4i> hierarchy;vector<RotatedRect>rect;//【5】查找轮廓findContours(img_gray, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);for (int i = 0; i < contours.size(); i++){rect.push_back(minAreaRect(contours[i]));Point2f vertices[4]; //定义矩形的4个顶点rect[i].points(vertices); //计算矩形的4个顶点for (int i = 0; i < 4; i++)line(img, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0),1);cout <<"width的值:"<<rect[i].size.width << endl;cout << "height的值:" << rect[i].size.height << endl;//其实只有一个外接矩形}imshow("img", img);waitKey(0); }
}
Reference
1 https://blog.csdn.net/mailzst1/article/details/83141632
2 https://www.cnblogs.com/hsy1941/p/7923323.html
这篇关于opencv rotatedrect 获取顶点的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!