Ask Your Question
0

How to fill enclosed area

asked 2017-04-17 13:35:34 -0600

Habib gravatar image

updated 2017-04-18 03:13:00 -0600

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 );
edit retag flag offensive close merge delete

Comments

You can use fillcontour and hierarchy or search for connectedComponents with negative image

LBerger gravatar imageLBerger ( 2017-04-17 14:00:18 -0600 )edit

Thank you,but,i'm new to opencv.please explain more ...

Habib gravatar imageHabib ( 2017-04-18 00:33:51 -0600 )edit

What have you already try ?

LBerger gravatar imageLBerger ( 2017-04-18 01:53:00 -0600 )edit

i get image contours,now i want create a mask and crop contours

Habib gravatar imageHabib ( 2017-04-18 02:55:02 -0600 )edit

Have you got contour hierarchy ?

LBerger gravatar imageLBerger ( 2017-04-18 03:06:04 -0600 )edit

I update my question

Habib gravatar imageHabib ( 2017-04-18 03:13:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-04-18 04:07:21 -0600

LBerger gravatar image

updated 2017-04-18 04:08:09 -0600

I'm not sure but you can try with this code :

#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

using namespace cv;
using namespace std;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<char> fillCtr;
Mat c;

void DrawTree( int idx, int level)
{
    int i=idx;
    drawContours(c, contours, i, Scalar(i),CV_FILLED);
    fillCtr[i]=1;
    while (hierarchy[i][0]!=-1)
    {
        int j=hierarchy[i][0];
        if (fillCtr[j] == 0)
        {
            drawContours(c, contours, j, Scalar(j),CV_FILLED);
            fillCtr[j]=1;
            DrawTree(j,level);
        }
        i=hierarchy[i][0];

    }
    if (hierarchy[idx][2]!=-1)
        DrawTree(hierarchy[idx][2],level+1);
}

int main (int argc,char **argv)
{
    {
        Mat x = imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/14415468805620458.jpg",CV_LOAD_IMAGE_GRAYSCALE);
        //Mat x = imread("f:/IMAGES/pic10.png",CV_LOAD_IMAGE_GRAYSCALE);
    imshow("original",x);
    Mat y;
    threshold(x,y,50,255,THRESH_BINARY);
    imshow("threshold",y);
    Mat yc;
    findContours(y,contours,hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
    c =Mat::zeros(x.size(),CV_16U);
   fillCtr.resize(contours.size());
   for( int i=0;i <contours.size();i++)
    {
       if (hierarchy[i][3] == -1 && fillCtr[i]==0)
       {
           DrawTree(i,0);
       }
    }

    imshow("contour",c);
    imwrite("contour.jpg",c);
    imwrite("contour.tif",c);
    waitKey(0);
    }

}
edit flag offensive delete link more

Comments

This code is helpfull but if we have a big table shape with several rows and cols in image ,as you know table is a big contour and this code make all of table filled and contents of table is unreachable.

Habib gravatar imageHabib ( 2017-04-18 09:58:05 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-04-17 13:35:34 -0600

Seen: 14,956 times

Last updated: Apr 18 '17