opencv/samples/cpp/convexhull.cpp

58 lines
1.3 KiB
C++
Raw Normal View History

2016-02-15 21:37:29 +08:00
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
2010-11-30 09:35:54 +08:00
#include <iostream>
2010-11-28 07:15:16 +08:00
using namespace cv;
2010-11-30 09:35:54 +08:00
using namespace std;
static void help(char** argv)
2010-11-30 09:35:54 +08:00
{
2012-06-08 01:21:29 +08:00
cout << "\nThis sample program demonstrates the use of the convexHull() function\n"
<< "Call:\n"
<< argv[0] << endl;
2010-11-30 09:35:54 +08:00
}
2010-11-28 07:15:16 +08:00
2015-08-01 23:24:23 +08:00
int main( int argc, char** argv )
2010-11-28 07:15:16 +08:00
{
2015-08-01 23:24:23 +08:00
CommandLineParser parser(argc, argv, "{help h||}");
if (parser.has("help"))
{
help(argv);
2015-08-01 23:24:23 +08:00
return 0;
}
2010-11-28 07:15:16 +08:00
Mat img(500, 500, CV_8UC3);
RNG& rng = theRNG();
for(;;)
{
int i, count = (unsigned)rng%100 + 1;
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
vector<Point> points;
for( i = 0; i < count; i++ )
{
Point pt;
pt.x = rng.uniform(img.cols/4, img.cols*3/4);
pt.y = rng.uniform(img.rows/4, img.rows*3/4);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
points.push_back(pt);
}
2012-06-08 01:21:29 +08:00
2021-09-12 03:58:45 +08:00
vector<Point> hull;
convexHull(points, hull, true);
2012-06-08 01:21:29 +08:00
2010-11-28 07:15:16 +08:00
img = Scalar::all(0);
for( i = 0; i < count; i++ )
circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);
2012-06-08 01:21:29 +08:00
2021-09-12 03:58:45 +08:00
polylines(img, hull, true, Scalar(0, 255, 0), 1, LINE_AA);
2010-11-28 07:15:16 +08:00
imshow("hull", img);
char key = (char)waitKey();
2010-11-28 07:15:16 +08:00
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
break;
}
return 0;
}