OpenCV 中检测到的轮廓数量不匹配

发布于 2024-11-08 11:14:10 字数 1155 浏览 0 评论 0原文

根据文档,函数 cvFindContours() 返回从二进制图像输入中检索到的轮廓数。然而,当我在所有轮廓上运行循环时,检测到的轮廓数量大大减少。

一个可能的原因是父级的子轮廓以及孔被计入函数的返回值中。即便如此,这个数字与我所使用的图片的合理目测估计并不相符。

本例中的返回值为 92,而在遍历所有轮廓时,有 15 个不同的轮廓。

这是代码:

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);

图像是:

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

随机彩色轮廓:http://imageshack.us/photo/my-images/819/cccolors.jpg/

As per the documentation, the function cvFindContours() 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/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

顾铮苏瑾 2024-11-15 11:14:10

您可能需要查看文档此处。关键部分如下。

指针firstContour填充为
的功能。它将包含指针
到第一个最外轮廓或
如果没有检测到轮廓则为 NULL(如果
图像是全黑的)。其他
轮廓可以从
使用 h_next 和 v_next 的firstContour
链接。

可能您需要检查每个 h_next 的 v_next 数量(反之亦然)。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文