opencv3检测凸包convexHull函数-使用方法一-滚动条

来源:互联网 发布:淘宝卖家数量 编辑:程序博客网 时间:2024/04/29 23:42
#include<iostream>#include<opencv2/opencv.hpp>#include<vector>using namespace cv;using namespace std;int g_nColorThick = 0;int g_nLineThick = 0;int g_nBackBlue = 0;int g_nBackGreen = 0;int g_nBackRed = 0;int g_bPointWay = 0;int g_bLineWay = 0;int g_nPointBlue = 0;int g_nPointGreen = 0;int g_nPointRed = 0;int g_nLineBlue = 0;int g_nLineGreen = 0;int g_nLineRed = 0;int g_nShowWay = 0;int main(){//先初始化变量Mat srcImage(Size(600, 600), CV_8UC3, Scalar(0));Mat saveImage;srcImage.copyTo(saveImage);RNG &rng = theRNG();namedWindow("【滚动条窗口】", 0);createTrackbar("circlethick", "【滚动条窗口】", &g_nColorThick, 100, 0);createTrackbar("linethick", "【滚动条窗口】", &g_nLineThick, 100, 0);createTrackbar("backblue", "【滚动条窗口】", &g_nBackBlue, 255, 0);createTrackbar("backgreen", "【滚动条窗口】", &g_nBackGreen, 255, 0);createTrackbar("backred", "【滚动条窗口】", &g_nBackRed, 255, 0);createTrackbar("pointblue", "【滚动条窗口】", &g_nPointBlue, 255, 0);createTrackbar("pointgreen", "【滚动条窗口】", &g_nPointGreen, 255, 0);createTrackbar("pointred", "【滚动条窗口】", &g_nPointRed, 255, 0);createTrackbar("lineblue", "【滚动条窗口】", &g_nLineBlue, 255, 0);createTrackbar("linegreen", "【滚动条窗口】", &g_nLineGreen, 255, 0);createTrackbar("linered", "【滚动条窗口】", &g_nLineRed, 255, 0);createTrackbar("pointway", "【滚动条窗口】", &g_bPointWay, 1, 0);createTrackbar("lineway", "【滚动条窗口】", &g_bLineWay, 1, 0);createTrackbar("showway", "【滚动条窗口】", &g_nShowWay, 1000, 0);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);}//绘制随机点double pointColorBlue, pointColorGreen, pointColorRed;for (int i = 0; i < (int)points.size(); i++){if (g_bPointWay == 0){pointColorBlue = rng.uniform(0, 255);pointColorGreen = rng.uniform(0, 255);pointColorRed = rng.uniform(0, 255);}else{pointColorBlue = g_nPointBlue;pointColorGreen = g_nPointGreen;pointColorRed = g_nPointRed;}circle(srcImage, points[i], 0, Scalar(pointColorBlue, pointColorGreen, pointColorRed), g_nColorThick + 1, 8);}//该用法结束后,hull将外围点的序号存放起来convexHull(Mat(points), hull, true);//绘制凸包Point point0 = points[hull[hull.size() - 1]];double lineColorBlue, lineColorGreen, lineColorRed;for (int i = 0; i < (int)hull.size(); i++){Point curPoint = points[hull[i]];if (g_bLineWay == 0){lineColorBlue = rng.uniform(0, 255);lineColorGreen = rng.uniform(0, 255);lineColorRed = rng.uniform(0, 255);}else{lineColorBlue = g_nLineBlue;lineColorGreen = g_nLineGreen;lineColorRed = g_nLineRed;}line(srcImage, point0, curPoint, Scalar(lineColorBlue, lineColorGreen, lineColorRed), g_nLineThick, 8);point0 = curPoint;}imshow("【显示窗口】", srcImage);//停止在没有按键事件发生时if (g_nShowWay == 0)key = waitKey();elsekey = waitKey(g_nShowWay);if (key == 27)break;elsesrcImage = Scalar(g_nBackBlue, g_nBackGreen, g_nBackRed);/*saveImage.copyTo(srcImage);*/}return 0;}

0 0
原创粉丝点击