Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/cpp/convexhull.cpp
16337 views
1
#include "opencv2/imgproc.hpp"
2
#include "opencv2/highgui.hpp"
3
#include <iostream>
4
5
using namespace cv;
6
using namespace std;
7
8
static void help()
9
{
10
cout << "\nThis sample program demonstrates the use of the convexHull() function\n"
11
<< "Call:\n"
12
<< "./convexhull\n" << endl;
13
}
14
15
int main( int argc, char** argv )
16
{
17
CommandLineParser parser(argc, argv, "{help h||}");
18
if (parser.has("help"))
19
{
20
help();
21
return 0;
22
}
23
Mat img(500, 500, CV_8UC3);
24
RNG& rng = theRNG();
25
26
for(;;)
27
{
28
int i, count = (unsigned)rng%100 + 1;
29
30
vector<Point> points;
31
32
for( i = 0; i < count; i++ )
33
{
34
Point pt;
35
pt.x = rng.uniform(img.cols/4, img.cols*3/4);
36
pt.y = rng.uniform(img.rows/4, img.rows*3/4);
37
38
points.push_back(pt);
39
}
40
41
vector<int> hull;
42
convexHull(Mat(points), hull, true);
43
44
img = Scalar::all(0);
45
for( i = 0; i < count; i++ )
46
circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);
47
48
int hullcount = (int)hull.size();
49
Point pt0 = points[hull[hullcount-1]];
50
51
for( i = 0; i < hullcount; i++ )
52
{
53
Point pt = points[hull[i]];
54
line(img, pt0, pt, Scalar(0, 255, 0), 1,LINE_AA);
55
pt0 = pt;
56
}
57
58
imshow("hull", img);
59
60
char key = (char)waitKey();
61
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
62
break;
63
}
64
65
return 0;
66
}
67
68