OpenCV "findContours"方法错误
OpenCV "findContours" method errors
我在使用OpenCV 2.4.8的"findContours"方法时遇到了问题。具体如下错误:
OpenCV Error: Unsupported format or combination of formats ([Start]FindContours support only 8uC1 and 32sC1 images) in cvStartFindContours, file ........opencvmodulesimgprocsrccontours.cpp, line 196
从消息的内容来看,我似乎使用了不适当的图像格式,但是我很确定我的代码指定了一个 8uC1(8 位 1 通道)矩阵。
/* 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 FilesMicrosoft Visual Studio 10.0Common7Toolsvsvars32.bat"
我正在重写你的代码,我认为你可以试试。
//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));
看起来问题最终与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));
相关文章:
- C++:正在检查LinkedList中的回文-递归方法-错误
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 方法内部但循环仍得到预期的不合格id错误C++
- 重载方法的方式会在使用临时调用时生成编译器错误
- 在C++中样板"冷/never_inline"错误处理技术的最佳方法是什么?
- 在C++中使用内联方法时出现未定义的符号错误
- 使用类模板的方法链接错误
- 尝试了解在导入的静态方法上使用删除方法时的错误
- 我的模板类方法返回错误类型?
- QtQuick - qml:28:错误:未知方法返回类型:自定义类型
- C++数组队列实现方法错误
- 方法错误"not all control paths return a value"和方法不返回值
- 为什么我的 setter 方法会产生错误的访问错误
- 派生类调用父类的方法,该方法调用重写的虚拟方法调用错误的方法
- 为什么调用没有正文的纯虚拟方法不会导致链接器错误?
- Android JNI 未定义对错误 c++ 方法的引用
- 这是哨兵线性搜索的错误方法吗?
- 这是重定向fork和execvp派生的子级的输出的错误方法吗
- flickcurl错误-方法flickr.oauth.access_token失败了
- 错误:方法未在此范围内声明(但已包含)