为什么在opencv中使用FindContours函数查找两个轮廓,而不是在下面的图像中找到一个轮廓? [英] Why does FindContours function in opencv find two contours instead of one in an image as below?

查看:791
本文介绍了为什么在opencv中使用FindContours函数查找两个轮廓,而不是在下面的图像中找到一个轮廓?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Input Image -- http://i.imgur.com/sLoqh.png Output Image with Convex Hull of contours drawn -- http://i.imgur.com/AaNJY.png

对如何获得一个轮廓的任何帮助将不胜感激?

Also any help on how to get one contour will be much appreciated?

推荐答案

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

The trick to retrieve the image as one contour seems to be processing the image with Canny before executing cvFindContours.

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

输出:

这篇关于为什么在opencv中使用FindContours函数查找两个轮廓,而不是在下面的图像中找到一个轮廓?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆