Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

How to fill enclosed area

Hello , How can I fill enclosed areas through openCv.like example image : image description

Thank you...

How to fill enclosed area

Hello , How can I fill enclosed areas through openCv.like example image : image description

Thank you...

I use this code :

Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

 /// Convert image to gray and blur it
     cvtColor( src, src_gray, CV_BGR2GRAY );
    blur( src_gray, src_gray, Size(3,3) );


  Mat canny_output;
   vector<vector<Point> > contours;
   vector<Vec4i> hierarchy;

 /// Detect edges using canny
 Canny( src_gray, canny_output, thresh, thresh*2, 3 );
 /// Find contours
 //CV_RETR_TREE
  findContours( canny_output, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

   /// Draw contours
   Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
     for( int i = 0; i< contours.size(); i++ )
    {
   Scalar color = Scalar( 255 ,255,255 );
   drawContours( drawing, contours, i ,color, 1, 8, hierarchy, INT_MAX, Point(-1 ,-1) );
  }

   fillPoly(drawing, contours, cv::Scalar::all(255),8);
  // cv::floodFill(edgesNeg, cv::Point(0,0), CV_RGB(255,255,255));
   /// Show in a window
      namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
       imshow( "Contours", drawing );