visual-studio OpenCV findContours 函数问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3087720/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 12:08:43  来源:igfitidea点击:

OpenCV findContours function problem

c++visual-studioopencvcomputer-vision

提问by user123668

I am trying to use the findContours function in OpenCV, but VS 2008 gives an error saying:

我试图在 OpenCV 中使用 findContours 函数,但 VS 2008 给出了一个错误说:

OpenCV Error: Bad flag (parameter or structure field) (Unrecognized or unsupport ed array type) in unknown function, file ........\ocv\opencv\src\cxcore\cxarr ay.cpp, line 2476

OpenCV 错误:未知函数中的错误标志(参数或结构字段)(无法识别或不支持的数组类型),文件 ........\ocv\opencv\src\cxcore\cxarr ay.cpp,第 2476 行

This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Press any key to continue . . .

此应用程序已请求运行时以一种不寻常的方式终止它。请联系应用程序的支持团队了解更多信息。按任意键继续 。. .

Here's the code:

这是代码:

Mat_<Vec<float,3>> originalimage;

Mat_<Vec<float,3>> resultingimage;

vector<vector<cv::Point>> v;

originalimage = cv::imread("Original.ppm");

cv::findContours(originalimage,v,CV_RETR_LIST,CV_CHAIN_APPROX_NONE);

Thanks in advance

提前致谢

回答by dnul

FindContours only accepts binary image. That is , any image which is output of cvThreshold cvAdapiveThreshold cvCanny

FindContours 只接受二进制图像。也就是说,任何图像是 cvThreshold cvAdapiveThreshold cvCanny 的输出

try adding this statement before cv::findContours

尝试在 cv::findContours 之前添加此语句

cvThreshold(originalImage,resultingImage,100,100,CV_THRESH_BINARY) 

then call findcontours with resultingImage.

然后使用resultingImage调用findcontours。

if it works then you should input the correct parameters to cvThreshold ( 100 is just an example).Check the reference for that matter.

如果它有效,那么你应该向 cvThreshold 输入正确的参数(100 只是一个例子)。检查参考资料。

EDIT: resultingImage should be a single channel image!!

编辑:resultingImage 应该是单通道图像!!

回答by NotNamedDwayne

I have had the same problem (or at least a similar one) with that function. I was not able to fix it, so I used the old C style cvFindContours function instead. I have included an example function in wich I used the cvFindContours function to clean up a blob image. This might not be the fastest solution, but at leas it works.

我在该功能上遇到了同样的问题(或至少是类似的问题)。我无法修复它,所以我改用旧的 C 风格 cvFindContours 函数。我在其中包含了一个示例函数,我使用 cvFindContours 函数来清理 blob 图像。这可能不是最快的解决方案,但至少它有效。

void filtBproject(Mat& Bproject){

    Scalar          color       = CV_RGB(255,255,255); // text color
    IplImage*       BprojectIpl =  &IplImage(Bproject);
    CvMemStorage*   storage     = cvCreateMemStorage(0);
    CvSeq*          contours    = 0;
    int             numCont     = 0;
    int             contAthresh = 45;

    numCont= cvFindContours( BprojectIpl, storage, &contours, sizeof(CvContour),
                    CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0,0) );

    cvSet(BprojectIpl, cvScalar(0,0,0));
    for( ; contours != 0; contours = contours->h_next )
        {

            if ( (cvContourArea(contours, CV_WHOLE_SEQ) > contAthresh) ){
                cvDrawContours( BprojectIpl, contours, color, color, -1, CV_FILLED, 8 );
            }
        }
}

回答by Douglas

For your v vector, you need to add a space like so:

对于您的 v 向量,您需要像这样添加一个空格:

vector<vector<cv::Point> > v;

Very subtle and dumb but it works.

非常微妙和愚蠢,但它有效。