OpenCV 中检测到的轮廓数量不匹配
根据文档,函数 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能需要查看文档此处。关键部分如下。
可能您需要检查每个 h_next 的 v_next 数量(反之亦然)。
You may want to look at the documentation here. The key part being the following.
May be you need to check the number of v_next for each h_next (or vice versa).