Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cvFindContours没有产生我期望的输出_C_Opencv_Image Processing - Fatal编程技术网

cvFindContours没有产生我期望的输出

cvFindContours没有产生我期望的输出,c,opencv,image-processing,C,Opencv,Image Processing,我有一个简单的、精心设计的例子,使用CVFindOntours和OpenCV C API在二进制图像上查找连接的组件。(这是C中编写的一大段软件的一部分,所以使用C++ API不是一种选择。) 样本输入图像由黑色背景上的两个填充的白色正方形组成,它们不重叠。我希望从中得到两个轮廓,表示构成每个正方形周长的连接组件。相反,我得到了四个组件。为什么?详情如下 首先,我在“self”中设置输入图像的阈值,这是一个CV_32FC1图像,背景像素为0.0,白色像素(两个正方形)值为1.0: cvThres

我有一个简单的、精心设计的例子,使用CVFindOntours和OpenCV C API在二进制图像上查找连接的组件。(这是C中编写的一大段软件的一部分,所以使用C++ API不是一种选择。) 样本输入图像由黑色背景上的两个填充的白色正方形组成,它们不重叠。我希望从中得到两个轮廓,表示构成每个正方形周长的连接组件。相反,我得到了四个组件。为什么?详情如下

首先,我在“self”中设置输入图像的阈值,这是一个CV_32FC1图像,背景像素为0.0,白色像素(两个正方形)值为1.0:

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

在黑暗中拍摄,但我知道轮廓方法标志控制结果集结构。也许可以尝试将CV_RETR_CCOMP标志与所描述的flasg之一交换

这稍微解释了标志CV_RETR_CCOMP的工作原理


虽然这很奇怪,但我也不期望您得到结果。

您能将示例图像添加到该图像和中间图像中吗?
===============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