cvFindContours没有生成我期望的输出

问题描述:

我有一个简单的,人为的例子,使用cvFindContours和OpenCV C API在二进制图像上查找连接组件。 (这是用C语言编写的较大软件的一部分,因此不能使用C++ API。)cvFindContours没有生成我期望的输出

示例输入图像由黑色背景上两个填充的白色方块组成,它们不重叠。我希望得到两个等值线,代表构成每个正方形周长的连接组件。相反,我得到四个组件。为什么?下面的细节。

首先我阈值“自我”我的输入图像,其是具有值1.0的0.0背景像素和白色像素(两个正方形)一个CV_32FC1图像:

cvThreshold(self, self, 0.8, 1.0, CV_THRESH_BINARY); 

我然后复制我的输入图像相同大小与cvScale一个CV_32SC1图像:

CvMat * temp = cvCreateMat(height, width, CV_32SC1); 
cvConvertScale(self, temp, 1, 0); 

我然后做cvFindContours获得连接的部件:

CvSeq * components = 0; 
CvMemStorage * mem = cvCreateMemStorage(0); 
cvFindContours(temp, mem, &components, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0)); 

然后,我执行以下操作来输出组成轮廓的点并将它们绘制到窗口中。

for(; components != 0; components = components->h_next) 
    { 
     printf("===============NEW COMPONENT!============\n"); 
     if (components->v_next) printf("This has a child.\n"); 
     CvPoint * pt_array = (CvPoint *)malloc(components->total*sizeof(CvPoint)); 

     cvCvtSeqToArray(components, pt_array, CV_WHOLE_SEQ); 
     for (int i = 0; i < components->total; i++) 
     { 
      printf("Point %i: %i, %i\n", i, pt_array[i].x, pt_array[i].y); 
    } 
    free(pt_array); 
    outer_color = CV_RGB(rand()%255, rand()%255, rand()%255); 
    cvDrawContours(dst, components, outer_color, inner_color, -3, 1, 8, cvPoint(0,0)); 
} 

我希望这会产生两个轮廓,代表我的二进制图像中的两个白色方块的周长。取而代之的是,我得到了四个顶级轮廓,它们大致位于彼此之上。是什么赋予了?

在四个轮廓点如下:

===============NEW COMPONENT!============ 
Point 0: 200, 150 
Point 1: 200, 199 
Point 2: 199, 200 
Point 3: 150, 200 
Point 4: 149, 199 
Point 5: 149, 150 
Point 6: 150, 149 
Point 7: 199, 149 
===============NEW COMPONENT!============ 
Point 0: 150, 150 
Point 1: 150, 199 
Point 2: 199, 199 
Point 3: 199, 150 
===============NEW COMPONENT!============ 
Point 0: 50, 10 
Point 1: 50, 49 
Point 2: 49, 50 
Point 3: 10, 50 
Point 4: 9, 49 
Point 5: 9, 10 
Point 6: 10, 9 
Point 7: 49, 9 
===============NEW COMPONENT!============ 
Point 0: 10, 10 
Point 1: 10, 49 
Point 2: 49, 49 
Point 3: 49, 10 
+0

您可以将样本图像添加到该图像和中间图像吗? – 2013-02-27 19:55:18

在黑暗中拍摄,但我知道,轮廓方法标志控制的结果集结构。也许试着用一个描述here的flasg交换CV_RETR_CCOMP标志。

这个link解释了标志CV_RETR_CCOMP是如何工作的。

这很奇怪,我不希望你的结果得到。