c - 为什么 opencv 中的 FindContours 函数会找到两个轮廓而不是图像中的一个轮廓,如下所示?

标签 c opencv

输入图像 -- http://i.imgur.com/sLoqh.png 绘制轮廓凸包的输出图像 -- http://i.imgur.com/AaNJY.png

还有任何关于如何获得一个轮廓的帮助将不胜感激?

最佳答案

将图像检索为一个轮廓 的技巧似乎是在执行cvFindContours 之前使用Canny 处理图像。

IplImage* src = cvLoadImage(argv[1], CV_LOAD_IMAGE_GRAYSCALE);

IplImage* cc_img = cvCreateImage( cvGetSize(src), src->depth, 3 );
cvSetZero(cc_img);
CvScalar(ext_color);

CvMemStorage *mem;
mem = cvCreateMemStorage(0);
CvSeq *contours = 0;

// edges returned by Canny might have small gaps between them, which causes some problems during contour detection
// Simplest way to solve this s to "dilate" the image.
cvCanny(src, src, 10, 50, 3); 
cvShowImage("Tutorial", src);
cvWaitKey(0);

int n = cvFindContours( src, mem, &contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));

CvSeq* ptr = 0;
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_img, ptr, ext_color, CV_RGB(0,0,0), -1, CV_FILLED, 8, cvPoint(0,0));
}   

cvNamedWindow("Tutorial");
cvShowImage("Tutorial", cc_img);
//cvSaveImage("out.png", cc_img);

cvWaitKey(0);

输出:

enter image description here

关于c - 为什么 opencv 中的 FindContours 函数会找到两个轮廓而不是图像中的一个轮廓,如下所示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7288672/

相关文章:

无法理解此代码堆栈存储函数调用 c 的输出

c - OpenCV 中的链接库

opencv - OpenCV 245首次构建错误

python - 如何在OpenCV中区分实心圆/轮廓和未实心圆/轮廓?

image - 同一图像中的多重裁剪

python - 将 KeyPoint 保存为字符串并转换回 KeyPoint

c - 简单的 tar 实现?

c - 文件读写问题

c - 结构字符串数组打印最后输入的元素

C 指针未正确分配