根据文档,函数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/
最佳答案
您可能需要查看here文档。关键部分如下。
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.
可能是您需要检查每个h_next的v_next数量(反之亦然)。
关于image-processing - OpenCV中检测到的轮廓数量不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6025539/