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

I mean something like this:

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

not like this one:

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

edit retag close merge delete

Sort by » oldest newest most voted

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);
cvtColor(src,tmp,CV_BGR2GRAY);
threshold(tmp,thr,200,255,THRESH_BINARY_INV);

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);
else
rectangle(src,Point(r.x,r.y), Point(r.x+r.width,r.y+r.height), Scalar(0,0,255),2,8,0);

}
imshow("src",src);
waitKey();


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

more

1

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

( 2014-04-01 01:37:30 -0500 )edit

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

( 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?

( 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 ?

( 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.

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

Official site

GitHub

Wiki

Documentation