contours2.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #include "opencv2/imgproc.hpp"
  2. #include "opencv2/highgui.hpp"
  3. #include <math.h>
  4. #include <iostream>
  5. using namespace cv;
  6. using namespace std;
  7. static void help(char** argv)
  8. {
  9. cout
  10. << "\nThis program illustrates the use of findContours and drawContours\n"
  11. << "The original image is put up along with the image of drawn contours\n"
  12. << "Usage:\n";
  13. cout
  14. << argv[0]
  15. << "\nA trackbar is put up which controls the contour level from -3 to 3\n"
  16. << endl;
  17. }
  18. const int w = 500;
  19. int levels = 3;
  20. vector<vector<Point> > contours;
  21. vector<Vec4i> hierarchy;
  22. static void on_trackbar(int, void*)
  23. {
  24. Mat cnt_img = Mat::zeros(w, w, CV_8UC3);
  25. int _levels = levels - 3;
  26. drawContours( cnt_img, contours, _levels <= 0 ? 3 : -1, Scalar(128,255,255),
  27. 3, LINE_AA, hierarchy, std::abs(_levels) );
  28. imshow("contours", cnt_img);
  29. }
  30. int main( int argc, char** argv)
  31. {
  32. cv::CommandLineParser parser(argc, argv, "{help h||}");
  33. if (parser.has("help"))
  34. {
  35. help(argv);
  36. return 0;
  37. }
  38. Mat img = Mat::zeros(w, w, CV_8UC1);
  39. //Draw 6 faces
  40. for( int i = 0; i < 6; i++ )
  41. {
  42. int dx = (i%2)*250 - 30;
  43. int dy = (i/2)*150;
  44. const Scalar white = Scalar(255);
  45. const Scalar black = Scalar(0);
  46. if( i == 0 )
  47. {
  48. for( int j = 0; j <= 10; j++ )
  49. {
  50. double angle = (j+5)*CV_PI/21;
  51. line(img, Point(cvRound(dx+100+j*10-80*cos(angle)),
  52. cvRound(dy+100-90*sin(angle))),
  53. Point(cvRound(dx+100+j*10-30*cos(angle)),
  54. cvRound(dy+100-30*sin(angle))), white, 1, 8, 0);
  55. }
  56. }
  57. ellipse( img, Point(dx+150, dy+100), Size(100,70), 0, 0, 360, white, -1, 8, 0 );
  58. ellipse( img, Point(dx+115, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
  59. ellipse( img, Point(dx+185, dy+70), Size(30,20), 0, 0, 360, black, -1, 8, 0 );
  60. ellipse( img, Point(dx+115, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
  61. ellipse( img, Point(dx+185, dy+70), Size(15,15), 0, 0, 360, white, -1, 8, 0 );
  62. ellipse( img, Point(dx+115, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
  63. ellipse( img, Point(dx+185, dy+70), Size(5,5), 0, 0, 360, black, -1, 8, 0 );
  64. ellipse( img, Point(dx+150, dy+100), Size(10,5), 0, 0, 360, black, -1, 8, 0 );
  65. ellipse( img, Point(dx+150, dy+150), Size(40,10), 0, 0, 360, black, -1, 8, 0 );
  66. ellipse( img, Point(dx+27, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
  67. ellipse( img, Point(dx+273, dy+100), Size(20,35), 0, 0, 360, white, -1, 8, 0 );
  68. }
  69. //show the faces
  70. namedWindow( "image", 1 );
  71. imshow( "image", img );
  72. //Extract the contours so that
  73. vector<vector<Point> > contours0;
  74. findContours( img, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
  75. contours.resize(contours0.size());
  76. for( size_t k = 0; k < contours0.size(); k++ )
  77. approxPolyDP(Mat(contours0[k]), contours[k], 3, true);
  78. namedWindow( "contours", 1 );
  79. createTrackbar( "levels+3", "contours", &levels, 7, on_trackbar );
  80. on_trackbar(0,0);
  81. waitKey();
  82. return 0;
  83. }