#include “opencv/opencv_application.h”
#include “public.h”
int main( int argc, const char* argv[])
{
// OpencvOperator opencvOperator;
// opencvOperator.FullfillHoughLinesPChange();
//初始化变量和随机值
Mat image(600, 600, CV_8UC3);
RNG & rng = theRNG();
while(1)
{
//参数初始化
char key; //键值
int count = (unsigned)rng%100 + 3; //随机生成点的数量,形成凸包至少要3各点
vector points; //点值
// cout << "The value of count is " << count << endl;
for(int i = 0; i < count; i++)
{
Point point;
point.x = rng.uniform(image.cols/4, image.cols3/4);
point.y = rng.uniform(image.cols/4, image.cols3/4);
// cout << "The value of point.x is " << point.x << endl;
points.push_back(point);
}
//检测凸包
vector<int> hull;
convexHull(Mat(points), hull, true);
//绘制出随机颜色的点
image = Scalar::all(0);
for(int i = 0; i < count; i++)
{
circle(image, points[i], 3, Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255)), FILLED, LINE_AA);
}
//准备参数
int hullcount = (int)hull.size(); //凸包的边数
Point point0 = points[hull[hullcount-1]]; //连接凸包边的坐标点
//绘制凸包的边
for(int i = 0; i < hullcount; i++)
{
Point point = points[hull[i]];
line(image, point0, point, Scalar(255, 255, 255), 2, LINE_AA);
point0 = point;
}
//显示效果图
imshow("凸包检测示例", image);
//按下ESC, Q, 或者q程序退出
key = (char)waitKey();
if(key == 27 || key == 'q' || key == 'Q') break;
}
return 0;
}