包围轮廓的矩形边界 opencv

 1 #include<opencv2/opencv.hpp>
 2 #include<iostream>
 3 
 4 using namespace std;
 5 using namespace cv;
 6 
 7 int main()
 8 {
 9     Mat image(600, 600, CV_8UC3);  //创建一个600*600 8位无符号字符型的3通道图像
10     RNG& rng = theRNG();     ////用其引用来接收theRNG函数返回的随机数生成器
11 
12     while (1)
13     {   
14         char key; // 键值
15         int count = rng.uniform(3,103);//随机生成点的数量3-103
16         vector<Point>points;  //二维点集存在这个向量里面
17 
18         for (int i = 0; i < count; i++)   //点的坐标
19         {
20             Point point;
21             point.x = rng.uniform(image.cols / 4, image.cols * 3 / 4);
22             point.y = rng.uniform(image.rows / 4, image.cols * 3 / 4);
23             points.push_back(point);  //生成的点放进points这个向量里面
24         }
25         //寻找最小面积的包围矩形
26         RotatedRect box = minAreaRect(Mat(points));
27         Point2f vertex[4];
28         box.points(vertex);
29 
30         image = Scalar::all(0);//初始化图像为全黑色
31         //随机化点的颜色 并画出
32         for (int i = 0; i < count; i++)
33         {
34             circle(image, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), FILLED, LINE_AA);
35         }
36         for (int i = 0; i < 4; i++)
37         {
38             line(image, vertex[i],vertex[(i+1)%4], Scalar(255, 255, 255), 2, LINE_AA);
39         }
40         //显示效果图
41         imshow("矩形包围示例", image);
42         //按下ESC退出程序
43         key = (char)waitKey();
44         if (key == 27)
45             break;
46     }
47 
48     return 0;
49 }

原文地址:https://www.cnblogs.com/carlber/p/9720384.html