generalContours_demo2.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. * @function generalContours_demo2.cpp
  3. * @brief Demo code to obtain ellipses and rotated rectangles that contain detected contours
  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. /// Load source image and convert it to gray
  23. CommandLineParser parser( argc, argv, "{@input | stuff.jpg | input image}" );
  24. Mat src = imread( samples::findFile( parser.get<String>( "@input" ) ) );
  25. if( src.empty() )
  26. {
  27. cout << "Could not open or find the image!\n" << endl;
  28. cout << "Usage: " << argv[0] << " <Input image>" << endl;
  29. return -1;
  30. }
  31. /// Convert image to gray and blur it
  32. cvtColor( src, src_gray, COLOR_BGR2GRAY );
  33. blur( src_gray, src_gray, Size(3,3) );
  34. /// Create Window
  35. const char* source_window = "Source";
  36. namedWindow( source_window );
  37. imshow( source_window, src );
  38. const int max_thresh = 255;
  39. createTrackbar( "Canny thresh:", source_window, &thresh, max_thresh, thresh_callback );
  40. thresh_callback( 0, 0 );
  41. waitKey();
  42. return 0;
  43. }
  44. /**
  45. * @function thresh_callback
  46. */
  47. void thresh_callback(int, void* )
  48. {
  49. /// Detect edges using Canny
  50. Mat canny_output;
  51. Canny( src_gray, canny_output, thresh, thresh*2 );
  52. /// Find contours
  53. vector<vector<Point> > contours;
  54. findContours( canny_output, contours, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
  55. /// Find the rotated rectangles and ellipses for each contour
  56. vector<RotatedRect> minRect( contours.size() );
  57. vector<RotatedRect> minEllipse( contours.size() );
  58. for( size_t i = 0; i < contours.size(); i++ )
  59. {
  60. minRect[i] = minAreaRect( contours[i] );
  61. if( contours[i].size() > 5 )
  62. {
  63. minEllipse[i] = fitEllipse( contours[i] );
  64. }
  65. }
  66. /// Draw contours + rotated rects + ellipses
  67. Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
  68. for( size_t i = 0; i< contours.size(); i++ )
  69. {
  70. Scalar color = Scalar( rng.uniform(0, 256), rng.uniform(0,256), rng.uniform(0,256) );
  71. // contour
  72. drawContours( drawing, contours, (int)i, color );
  73. // ellipse
  74. ellipse( drawing, minEllipse[i], color, 2 );
  75. // rotated rectangle
  76. Point2f rect_points[4];
  77. minRect[i].points( rect_points );
  78. for ( int j = 0; j < 4; j++ )
  79. {
  80. line( drawing, rect_points[j], rect_points[(j+1)%4], color );
  81. }
  82. }
  83. /// Show in a window
  84. imshow( "Contours", drawing );
  85. }