OpenCV——凸包

 

 

 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 }

 

 

转载于:https://www.cnblogs.com/long5683/p/9692423.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值