Home | History | Annotate | Download | only in ShapeDescriptors
      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 
      7 #include "opencv2/imgcodecs.hpp"
      8 #include "opencv2/highgui/highgui.hpp"
      9 #include "opencv2/imgproc/imgproc.hpp"
     10 #include <iostream>
     11 #include <stdio.h>
     12 #include <stdlib.h>
     13 
     14 using namespace cv;
     15 using namespace std;
     16 
     17 Mat src; Mat src_gray;
     18 int thresh = 100;
     19 int max_thresh = 255;
     20 RNG rng(12345);
     21 
     22 /// Function header
     23 void thresh_callback(int, void* );
     24 
     25 /**
     26  * @function main
     27  */
     28 int main( int, char** argv )
     29 {
     30   /// Load source image and convert it to gray
     31   src = imread( argv[1], 1 );
     32 
     33   /// Convert image to gray and blur it
     34   cvtColor( src, src_gray, COLOR_BGR2GRAY );
     35   blur( src_gray, src_gray, Size(3,3) );
     36 
     37   /// Create Window
     38   const char* source_window = "Source";
     39   namedWindow( source_window, WINDOW_AUTOSIZE );
     40   imshow( source_window, src );
     41 
     42   createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
     43   thresh_callback( 0, 0 );
     44 
     45   waitKey(0);
     46   return(0);
     47 }
     48 
     49 /**
     50  * @function thresh_callback
     51  */
     52 void thresh_callback(int, void* )
     53 {
     54   Mat threshold_output;
     55   vector<vector<Point> > contours;
     56   vector<Vec4i> hierarchy;
     57 
     58   /// Detect edges using Threshold
     59   threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
     60   /// Find contours
     61   findContours( threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
     62 
     63   /// Find the rotated rectangles and ellipses for each contour
     64   vector<RotatedRect> minRect( contours.size() );
     65   vector<RotatedRect> minEllipse( contours.size() );
     66 
     67   for( size_t i = 0; i < contours.size(); i++ )
     68      { minRect[i] = minAreaRect( Mat(contours[i]) );
     69        if( contours[i].size() > 5 )
     70          { minEllipse[i] = fitEllipse( Mat(contours[i]) ); }
     71      }
     72 
     73   /// Draw contours + rotated rects + ellipses
     74   Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
     75   for( size_t i = 0; i< contours.size(); i++ )
     76      {
     77        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
     78        // contour
     79        drawContours( drawing, contours, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
     80        // ellipse
     81        ellipse( drawing, minEllipse[i], color, 2, 8 );
     82        // rotated rectangle
     83        Point2f rect_points[4]; minRect[i].points( rect_points );
     84        for( int j = 0; j < 4; j++ )
     85           line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
     86      }
     87 
     88   /// Show in a window
     89   namedWindow( "Contours", WINDOW_AUTOSIZE );
     90   imshow( "Contours", drawing );
     91 }
     92