1 #include <opencv2/opencv.hpp>
 2 #include <iostream>
 3 #include <math.h>
 4 
 5 using namespace cv;
 6 using namespace std;
 7 
 8 
 9 int main(int argc, char** argv)
10 {
11     Mat image(600, 600, CV_8UC3);
12     RNG rng = theRNG();//生成随机数
13 
14     while (1)
15     {
16         char key;//键值
17         int count = (unsigned)rng % 100 + 3;//随机生成点的数量
18         vector<Point> points;//点值
19 
20         //随机生成点坐标
21         for (int i = 0; i < count; i++)
22         {
23             Point point;
24             point.x = rng.uniform(image.cols / 4, image.cols * 3 / 4);
25             point.y = rng.uniform(image.rows / 4, image.rows * 3 / 4);
26 
27             points.push_back(point);
28 
29         }
30     
31 
32         //检测凸包
33         vector<int> hull;
34         convexHull(Mat(points), hull, true);
35 
36         //绘制随机颜色的点
37         image = Scalar::all(0);
38         for (int i = 0; i < count; i++)
39             circle(image, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), rng.uniform(0, 255), FILLED, LINE_AA);
40 
41         //准备参数
42         int hullcount = (int)hull.size();//凸包的边数
43         Point point0 = points[hull[hullcount - 1]];//连接凸包边的坐标
44 
45         //绘制凸包的边
46         for (int i = 0; i < hullcount; i++)
47         {
48             Point point = points[hull[i]];
49             line(image, point0, point, Scalar(255, 255, 255), 2, LINE_AA);
50             point0 = point;
51         }
52 
53         imshow("效果图", image);
54 
55 
56 
57         key = (char)waitKey();
58         if (key == 27 | key == 'q' | key == 'Q')
59             break;
60     }
61     return 0;
62 
63 }

 

 

posted on 2018-09-23 12:50  一抹烟霞  阅读(344)  评论(0编辑  收藏  举报

Live2D