generalContours_demo1.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @function generalContours_demo1.cpp
  3. * @brief Demo code to find contours in an image
  4. * @author OpenCV team
  5. */
  6. #include "opencv2/imgcodecs.hpp"
  7. #include "opencv2/highgui.hpp"
  8. #include "opencv2/imgproc.hpp"
  9. #include <iostream>
  10. using namespace cv;
  11. using namespace std;
  12. Mat src_gray;
  13. int thresh = 100;
  14. RNG rng(12345);
  15. /// Function header
  16. void thresh_callback(int, void* );
  17. /**
  18. * @function main
  19. */
  20. int main( int argc, char** argv )
  21. {
  22. //! [setup]
  23. /// Load source image
  24. CommandLineParser parser( argc, argv, "{@input | stuff.jpg | input image}" );
  25. Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ) );
  26. if( src.empty() )
  27. {
  28. cout << "Could not open or find the image!\n" << endl;
  29. cout << "usage: " << argv[0] << " <Input image>" << endl;
  30. return -1;
  31. }
  32. /// Convert image to gray and blur it
  33. cvtColor( src, src_gray, COLOR_BGR2GRAY );
  34. blur( src_gray, src_gray, Size(3,3) );
  35. //! [setup]
  36. //! [createWindow]
  37. /// Create Window
  38. const char* source_window = "Source";
  39. namedWindow( source_window );
  40. imshow( source_window, src );
  41. //! [createWindow]
  42. //! [trackbar]
  43. const int max_thresh = 255;
  44. createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
  45. thresh_callback( 0, 0 );
  46. //! [trackbar]
  47. waitKey();
  48. return 0;
  49. }
  50. /**
  51. * @function thresh_callback
  52. */
  53. void thresh_callback(int, void* )
  54. {
  55. //! [Canny]
  56. /// Detect edges using Canny
  57. Mat canny_output;
  58. Canny( src_gray, canny_output, thresh, thresh*2 );
  59. //! [Canny]
  60. //! [findContours]
  61. /// Find contours
  62. vector<vector<Point> > contours;
  63. findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE );
  64. //! [findContours]
  65. //! [allthework]
  66. /// Approximate contours to polygons + get bounding rects and circles
  67. vector<vector<Point> > contours_poly( contours.size() );
  68. vector<Rect> boundRect( contours.size() );
  69. vector<Point2f>centers( contours.size() );
  70. vector<float>radius( contours.size() );
  71. for( size_t i = 0; i < contours.size(); i++ )
  72. {
  73. approxPolyDP( contours[i], contours_poly[i], 3, true );
  74. boundRect[i] = boundingRect( contours_poly[i] );
  75. minEnclosingCircle( contours_poly[i], centers[i], radius[i] );
  76. }
  77. //! [allthework]
  78. //! [zeroMat]
  79. Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
  80. //! [zeroMat]
  81. //! [forContour]
  82. /// Draw polygonal contour + bonding rects + circles
  83. for( size_t i = 0; i< contours.size(); i++ )
  84. {
  85. Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
  86. drawContours( drawing, contours_poly, (int)i, color );
  87. rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2 );
  88. circle( drawing, centers[i], (int)radius[i], color, 2 );
  89. }
  90. //! [forContour]
  91. //! [showDrawings]
  92. /// Show in a window
  93. imshow( "Contours", drawing );
  94. //! [showDrawings]
  95. }