开发者

Mismatch in number of detected contours in OpenCV

As per the documentation, the function cvFindContour开发者_如何学Gos() returns the number of retrieved contours from the binary image input. However when I'm running a loop over all the contours the number of detected contours is drastically less.

A possible reason would be that child contours of a parent, as well as holes are being counted in the return value of the function. Even so this number is not matching a reasonable eye-estimate of the pictures used by me.

The return value in this case is 92, whereas in traversing all the contours there are 15 distinct contours.

Here's the code:

int n = cvFindContours(img_canny_mop, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
printf("No of retrieved contours = %d",n);
for (ptr = contours; ptr != NULL; ptr = ptr->h_next) 
{
        ext_color = CV_RGB( rand()&255, rand()&255, rand()&255 ); //randomly coloring different contours
        cvDrawContours(cc_color, ptr, ext_color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));          
        //display( cc_color, "CCs");
        n_cont++;
}
cvSaveImage("CC_colors.jpg",cc_color);
printf("\nNo of contours = %d",n_cont);

The images are:

Input: http://imageshack.us/photo/my-images/811/cannymop.jpg/

Randomly colored contours: http://imageshack.us/photo/my-images/819/cccolors.jpg/


You may want to look at the documentation here. The key part being the following.

The pointer firstContour is filled by the function. It will contain pointer to the first most outer contour or NULL if no contours is detected (if the image is completely black). Other contours may be reached from firstContour using h_next and v_next links.

May be you need to check the number of v_next for each h_next (or vice versa).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜