convexhull.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "opencv2/imgproc.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include <iostream>
  4. using namespace cv;
  5. using namespace std;
  6. static void help(char** argv)
  7. {
  8. cout << "\nThis sample program demonstrates the use of the convexHull() function\n"
  9. << "Call:\n"
  10. << argv[0] << endl;
  11. }
  12. int main( int argc, char** argv )
  13. {
  14. CommandLineParser parser(argc, argv, "{help h||}");
  15. if (parser.has("help"))
  16. {
  17. help(argv);
  18. return 0;
  19. }
  20. Mat img(500, 500, CV_8UC3);
  21. RNG& rng = theRNG();
  22. for(;;)
  23. {
  24. int i, count = (unsigned)rng%100 + 1;
  25. vector<Point> points;
  26. for( i = 0; i < count; i++ )
  27. {
  28. Point pt;
  29. pt.x = rng.uniform(img.cols/4, img.cols*3/4);
  30. pt.y = rng.uniform(img.rows/4, img.rows*3/4);
  31. points.push_back(pt);
  32. }
  33. vector<Point> hull;
  34. convexHull(points, hull, true);
  35. img = Scalar::all(0);
  36. for( i = 0; i < count; i++ )
  37. circle(img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA);
  38. polylines(img, hull, true, Scalar(0, 255, 0), 1, LINE_AA);
  39. imshow("hull", img);
  40. char key = (char)waitKey();
  41. if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
  42. break;
  43. }
  44. return 0;
  45. }