Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV findcontours returns 2 contours for each circle

I am trying to use findcontours() function in OpenCV on the image below.

findContours(img, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_NONE, cvPoint(0,0) );

When I do this query: contours.size() it returns 18, so that seems 2 contour for each circle. The circles are as you can see 1-pixel thick, how is it 2 contours? Is it one for outer and one for inner, if so, how can I force this function to detect just one contour for each circle? I thought a contour was defined as a connected sequence of pixels, 1 pixel thick.

Says there are 18 contours!!

like image 907
dr_rk Avatar asked Oct 08 '22 08:10

dr_rk


1 Answers

I can't confirm this, but i think the algorithm used by this function does something equivalent to computing the gradient for each function. Which means that there will be a contour found on the outer edge and one on the inner edge, just like you suggested. To confirm this, You can try to use an input image where the circles have been filled with white (elimininating the inner contour)

you can also test with different parameters on the findContours function

For example, try using CV_RETR_EXTERNAL instead of CV_RETR_TREE I assume the inner circle is nested within the outer one, so this should force it to return only the outer ones

like image 119
Naps62 Avatar answered Oct 13 '22 09:10

Naps62