【问题标题】:OpenCV "findContours" method errorsOpenCV“findContours”方法错误
【发布时间】:2014-01-20 23:20:19
【问题描述】:

我遇到了 OpenCV 2.4.8 的“findContours”方法的问题。具体如下错误:

OpenCV Error: Unsupported format or combination of formats ([Start]FindContours support only 8uC1 and 32sC1 images) in cvStartFindContours, file ..\..\..\..\opencv\modules\imgproc\src\contours.cpp, line 196

从消息的内容来看,我似乎使用了不合适的图像格式,但我非常确定我的代码指定了一个 8uC1(8 位 1 通道)矩阵。 p>

/* Threshold source image (src1 which is a grayscale image) */
Mat threshImg(src1.rows, src1.cols, CV_8UC1);
threshold(src1, threshImg, thresh, 255, CV_THRESH_BINARY);

/* Get contours */
Mat threshCopy = threshImg; // Copying image because findContours method edits image data
std::vector<std::vector<Point>> contours;
findContours(threshCopy, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0,0));

我在命令行使用cl编译代码,链接如下:

$: cl NameOfCode.cpp -W2 -EHsc -c -I OpenCVIncludeDirectory
$: link NameOfCode.obj -LIBPATH:OpenCVLibraryDirectory opencv_core248.lib opencv_highgui248.lib opencv_imgproc248.lib

为了启用 cl 和链接,我从 Visual Studio 2010 运行 vsvars32.bat:

$: "C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.bat"

【问题讨论】:

  • 什么是 src1 ?如果 that 不是 CV_8UC1,但 CV_8UC3 你的阈值 img 也不会。 (不,你不必初始化它)
  • 另外,如果您想要阈值图像的 副本,请使用: Mat threshCopy = threshImg.clone(); // 你的版本只做一个“浅”拷贝
  • @berak 感谢您对使用 clone() 方法的见解。我也可能不正确地加载了 src1。现在我正在使用'Mat src1 = imread("PathToFile.png", CV_LOAD_IMAGE_GRAYSCALE);'我的代码不再给我一条错误消息,但它仍然崩溃并显示一般消息 programName.exe 已停止工作

标签: c++ opencv


【解决方案1】:

我正在重写你的代码,我觉得很好,你可以试试。

//Copy the src1 image to threshImg
Mat threshImg = src1.clone(); 
//Covert the threshImg from 8-channel to 1-channel and threshold it to binary image.
cvtColor(threshImg,threshImg, CV_RGB2GRAY); 
threshold(src1, threshImg, thresh, 255, CV_THRESH_BINARY);
//Finnaly you can get a contours.
Mat threshCopy = threshImg.clone;
std::vector<std::vector<Point>> contours;
findContours(threshCopy, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0,0));

【讨论】:

    【解决方案2】:

    看起来问题最终与 Visual Studio 相关。在安装 SP1 http://www.microsoft.com/en-us/download/details.aspx?id=23691 并按照 berak 和 vasan 的建议对代码进行调整之后。最终代码如下:

    /* NOTE: Using namespace cv & std */
    
    /* Get input image */
    Mat origImg = imread("C:\\PathToImage\\Image.png", CV_LOAD_IMAGE_GRAYSCALE);
    
    /* Threshold input image */
    Mat threshImg;
    threshold(origImg, threshImg, 150, 255.0, THRESH_BINARY);
    
    /* Get contours from threshold image */
    Mat copyImage = threshImg.clone();
    vector<vector<Point>> contours;
    findContours(copyImage, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0,0));
    

    【讨论】:

      猜你喜欢
      • 2018-03-04
      • 1970-01-01
      • 2020-12-21
      • 2016-05-30
      • 2016-06-25
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多