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

标签 image-processing 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/

最佳答案

您可能需要查看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/

相关文章:

opencv - 如何使用 catkin 将 OpenCV 3.4.5 dnn 模块与自定义 cv_bridge 链接?错误:未定义的引用 cv::dnn:experimental_dnn_v2::Net::Net()

python - OpenCV VideoWriter ffmpeg 一次又一次

c# - 调整图像的亮度对比度和 Gamma

ios - 从点数组中获取矩形

android - android中的高斯模糊非常慢

openCV图像拼接广角160度

algorithm - cv::undistortPoints() - 迭代算法解释

opencv - 如何仅放大图像的几个点,图像处理

c++ - 在 OpenCV 3.0 中计算密集 SIFT 特征

opencv - 级联分类器无法训练。检查使用的训练参数