- #include<iostream>
- #include<opencv2/opencv.hpp>
- #include<vector>
- using namespace cv;
- using namespace std;
- int main()
- {
- //先初始化变量
- Mat srcImage(Size(600, 600), CV_8UC3, Scalar(0));
- Mat saveImage;
- srcImage.copyTo(saveImage);
- RNG &rng = theRNG();
- char key;
- Point midPoint;
- //hull 变量用来存储点的序号
- vector<int> hull;
- while (1)
- {
- //因为下面是入栈操作,所以,需要在循环内为变量分配空间,否则,栈内的数据无法出栈
- vector<Point> points;
- //首先确定随机点的坐标,以及随机点落定的范围
- int count = (unsigned int)rng % 100 + 3;
- for (int i = 0; i < count; i++)
- {
- //确定随机点的范围
- midPoint.x = rng.uniform(srcImage.cols / 4, srcImage.cols * 3 / 4);
- midPoint.y = rng.uniform(srcImage.rows / 4, srcImage.rows * 3 / 4);
- points.push_back(midPoint);
- }
- //该用法结束后,hull将外围点的序号存放起来
- convexHull(Mat(points), hull, true);
- //绘制随机点
- for (int i = 0; i < points.size(); i++)
- {
- circle(srcImage, points[i], 0, Scalar(255, 255, 0), 3, 8);
- }
- //绘制凸包
- Point point0 = points[hull[hull.size() - 1]];
- for (int i = 0; i < hull.size(); i++)
- {
- Point curPoint = points[hull[i]];
- line(srcImage, point0, curPoint, Scalar(255, 255, 0), 3, 8);
- point0 = curPoint;
- }
- imshow("【显示窗口】", srcImage);
- //停止在没有按键事件发生时
- key = waitKey();
- if (key == 27)
- break;
- else
- srcImage = Scalar(0);
- /*saveImage.copyTo(srcImage);*/
- }
- return 0;
- }
按下空格键进行切换