0

我正在继续我的 openCV 工作,这是我在这里描述的第一次冒险。它进行得很好,但我偶然发现了另一件棘手的事情。我想在已应用自适应阈值的图像上找到轮廓:

在此处输入图像描述

所以这cvFindContours似乎工作得很好,结果如下:

在此处输入图像描述

问题是,当我尝试遍历找到的计数时,它说只有一个轮廓(contours->total在下面的代码中等于 1)。这是代码:

IplImage* img;
if((img = cvLoadImage( "photos/img-000012.ppm", 1)) == 0 )
{
    perror("cvLoadImage");
    return 1;
}
cvNamedWindow( "Image view", 1 );
cvShowImage( "Image view", img );

IplImage* gray = cvCreateImage( cvGetSize(img), 8, 1 ); // allocate a 1 channel byte image
cvCvtColor( img, gray, CV_BGR2GRAY );
cvShowImage( "Image view", gray );
cvWaitKey(0);

cvAdaptiveThreshold(gray, gray,
        255,    //  Non-zero value assigned to the pixels for which the condition is satisfied
        CV_ADAPTIVE_THRESH_MEAN_C, // adaptiveMethod
        CV_THRESH_BINARY_INV,   // thresholdType
        11, // blockSize
        5); // Constant subtracted from the mean or weighted mean
cvShowImage( "Image view", gray );
cvWaitKey(0);

IplConvKernel *se = cvCreateStructuringElementEx(3, 3, 1,  1,  CV_SHAPE_RECT, NULL);
cvErode(gray, gray, se, 1);
cvShowImage( "Image view", gray );
cvWaitKey(0);

IplImage *canny_out = cvCreateImage(cvGetSize(gray), 8, 1);
cvCanny(gray, canny_out, 50, 100, 3);
cvShowImage( "Image view", canny_out );
cvWaitKey(0);

CvMemStorage *storage = cvCreateMemStorage(0);
CvSeq *contours = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvPoint), storage);
cvFindContours(gray, storage, &contours, sizeof(CvContour), CV_RETR_LIST,
        CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0));
printf("contours->total = %d\n", contours->total);

cvDrawContours(img, contours, CV_RGB(0,255,0), CV_RGB(0,0,255),
        2, 1, 8, cvPoint(0, 0));
cvShowImage( "Image view", img );
cvWaitKey(0);

应该是这样,只有一个轮廓吗?也许我不明白openCV对轮廓的定义?我会很感激你的帮助。

4

2 回答 2

2

实际上cvFindContours返回已建立轮廓的数量(我测试了你的图片,它返回>1)。请参阅文档。但我不知道为什么total等于一。

于 2012-08-07T19:13:17.233 回答
0
  1. 你不需要cvCreateSeq()之前cvFindContours()
  2. 而不是轮廓->总计,遍历它们。

    for (; contours != 0; contours = contours->h_next)

于 2013-10-30T06:33:16.470 回答