C++ OpenCV";findContours“;方法错误

C++ OpenCV";findContours“;方法错误,c++,opencv,C++,Opencv,我对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

我对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通道)矩阵

要启用cl和link的使用,我从Visual studio 2010运行vsvars32.bat:

$: "C:\Program Files\Microsoft Visual Studio 10.0\Common7\Tools\vsvars32.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));
//将src1映像复制到threshImg
Mat threshImg=src1.clone();
//将阈值从8通道转换为1通道,并将阈值转换为二值图像。
CVT颜色(阈值、阈值、CV_rgb2灰色);
阈值(src1,threshImg,thresh,255,CV_thresh_二进制);
//最后你可以得到一个轮廓。
Mat threshCopy=threshImg.clone;
矢量轮廓;
findContours(阈值复制、等高线、等高线外部、等高线链近似、点(0,0));

看起来问题最终与Visual Studio有关。在安装SP1并按照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));
/*注意:使用名称空间cv&std*/
/*获取输入图像*/
Mat origImg=imread(“C:\\PathToImage\\Image.png”,CV\u LOAD\u Image\u GRAYSCALE);
/*阈值输入图像*/
垫脱粒;
阈值(原始、阈值、150、255.0、阈值二进制);
/*从阈值图像中提取轮廓线*/
Mat copyImage=threshImg.clone();
矢量等值线;
findContours(复制图像、轮廓、CV_RETR_外部、CV_链_近似、点(0,0));

什么是src1?如果这不是CV_8UC1,而是CV_8UC3,那么您的阈值img也不会是。(不,您不必初始化它)另外,如果您想要阈值图像的副本,请使用:Mat thresholdcopy=thresholimg.clone();//你的版本只做了一个“肤浅的”copy@berak感谢您对使用clone()方法的深入了解。另外,我可能没有正确加载src1。现在我使用的是'Mat src1=imread(“PathToFile.png”,CV\u LOAD\u IMAGE\u GRAYSCALE);'我的代码不再向我提供错误消息,但它仍然会崩溃,并显示一般消息programName.exe已停止工作
//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));
/* 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));