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