Ask Your Question

Is there an openCV method/function to detect unsevered blobs?

asked 2013-12-09 11:44:22 -0500

mervelous gravatar image

updated 2020-10-28 02:35:25 -0500

I mean something like this:image description

At pic the b, d, a and e letters have blobs, by my poor definition, unsevered blobs,

not like this one:

image description

Is there any way to recognize these blobs, unsevered ones?

Thanks in advance

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2013-12-10 05:43:16 -0500

Haris gravatar image

updated 2013-12-10 05:57:37 -0500

You can solve these easily findContours() in your source image and detect whether the contour is unsevered or not by examining the hierarchy passed to the findContours() function. From the first figure it is clearer that no contour has child contour as compared to the second image, you will get this data from hierarchy parameter which is optional output vector, containing information about the image topology. It has as many elements as the number of contours.

Here we will use hierarchy as

vector< Vec4i > hierarchy

where for an i-th contour

  • hierarchy[i][0] = next contour at the same hierarchical level
  • hierarchy[i][1] = previous contour at the same hierarchical level
  • hierarchy[i][2] = denotes its first child contour
  • hierarchy[i][3] = denotes index of its parent contour

If for the contour i there are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative. See findContour() function for more details.

So by checking the value hierarchy[i][2] you can decide the contour belongs to unsevered or not, that is for a contour if the hierarchy[i][2] = -1 then it belongs to unsevered.

And one more thing is that in findContours() function you should use CV_RETR_CCOMP which retrieves all of the contours and organizes them into a two-level hierarchy.

Here is the C++ code how to implement this.

    Mat src=imread("src.jpg",1);

    vector< vector <Point> > contours; // Vector for storing contour
    vector< Vec4i > hierarchy;

    findContours( thr, contours, hierarchy,CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

    for( int i = 0; i< contours.size(); i=hierarchy[i][0] ) // iterate through each contour.
        Rect r= boundingRect(contours[i]);
        if(hierarchy[i][2]<0) //Check if there is a child contour
          rectangle(src,Point(r.x,r.y), Point(r.x+r.width,r.y+r.height), Scalar(0,255,0),2,8,0);
          rectangle(src,Point(r.x,r.y), Point(r.x+r.width,r.y+r.height), Scalar(0,0,255),2,8,0);


Result: Here the green rectangle represent the positive object and red represent the false object. image description

image description

edit flag offensive delete link more



@Haris, guides man! I hope you keep your solutions somewhere together. We should look into making them into a quick and easy solution platform!

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 01:37:30 -0500 )edit

@StevenPuttemans I was also thinking that, hope you can help me to doing this.

Haris gravatar imageHaris ( 2014-04-01 02:22:24 -0500 )edit

@Haris, I will contact you about this in some time. Have some upcomming deadlines for the moment, so we should look into the best way to distribute them. I was thinking of an open source platform. It could then be used to add guides using specific rules, but it should also be possible to integrate it into this forum OR to add it to the openCV documentation. Any suggestions?

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 02:37:24 -0500 )edit

@StevenPuttemans Both of your idea looks fine for me, I am ready to works with any of them, and in your opinion which will be better ?

Haris gravatar imageHaris ( 2014-04-01 02:47:55 -0500 )edit

I will have a look into the possibilities of this forum. Might contact Alexander (the creator of this forum) to ask for some guidelines.

StevenPuttemans gravatar imageStevenPuttemans ( 2014-04-01 03:08:15 -0500 )edit

Question Tools

1 follower


Asked: 2013-12-09 11:44:22 -0500

Seen: 3,002 times

Last updated: Dec 10 '13