2011-05-22 02:32:34 +08:00
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2010-11-30 09:35:54 +08:00
|
|
|
#include <fstream>
|
|
|
|
#include <iostream>
|
2010-11-28 07:15:16 +08:00
|
|
|
|
|
|
|
using namespace cv;
|
2010-11-30 09:35:54 +08:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
void help()
|
|
|
|
{
|
2010-12-04 16:29:27 +08:00
|
|
|
cout << "\nThis sample program demonstrates the use of the convexHull() function\n"
|
2010-11-30 09:35:54 +08:00
|
|
|
<< "Call:\n"
|
|
|
|
<< "./convexhull\n" << endl;
|
|
|
|
}
|
2010-11-28 07:15:16 +08:00
|
|
|
|
2011-02-15 18:54:49 +08:00
|
|
|
int main( int /*argc*/, char** /*argv*/ )
|
2010-11-28 07:15:16 +08:00
|
|
|
{
|
|
|
|
Mat img(500, 500, CV_8UC3);
|
|
|
|
RNG& rng = theRNG();
|
|
|
|
|
2010-11-30 09:35:54 +08:00
|
|
|
help();
|
|
|
|
|
2010-11-28 07:15:16 +08:00
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
char key;
|
|
|
|
int i, count = (unsigned)rng%100 + 1;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
points.push_back(pt);
|
|
|
|
}
|
|
|
|
|
|
|
|
vector<int> hull;
|
2011-05-30 17:02:17 +08:00
|
|
|
convexHull(Mat(points), hull, true);
|
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), CV_FILLED, CV_AA);
|
|
|
|
|
|
|
|
int hullcount = (int)hull.size();
|
|
|
|
Point pt0 = points[hull[hullcount-1]];
|
|
|
|
|
|
|
|
for( i = 0; i < hullcount; i++ )
|
|
|
|
{
|
|
|
|
Point pt = points[hull[i]];
|
|
|
|
line(img, pt0, pt, Scalar(0, 255, 0), 1, CV_AA);
|
|
|
|
pt0 = pt;
|
|
|
|
}
|
|
|
|
|
|
|
|
imshow("hull", img);
|
|
|
|
|
|
|
|
key = (char)waitKey();
|
|
|
|
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|