我在 OpenCV 中的代码运行良好,直到我想找到轮廓:
findContours(src, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
然后我不断收到以下错误:
“Contour.exe中的0x773e3e28处的未处理异常:Microsoft C ++ 异常:cv ::内存位置0x002ff3ac的异常。“
您对此错误有任何疑问吗?
我的完整代码如下。
由于
Mat src=Mat(100,200,CV_64F),newimg;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
for (int i=25;i<80;i++)
for(int j=25;j<80;j++)
src.at<double>(i,j)=1;
imshow("img",src);
waitKey(0);
findContours(src, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
答案 0 :(得分:1)
从OpenCV文档中引用findContours
图片 - 来源, 8位单通道图片。非零像素被视为1。零像素保持为0,因此图像被视为二进制。您可以使用compare(),inRange(),threshold(),adaptiveThreshold(),Canny()等来从灰度或彩色图像中创建二进制图像。该功能在提取轮廓时修改图像。如果mode等于CV_RETR_CCOMP或CV_RETR_FLOODFILL,则输入也可以是标签的32位整数图像(CV_32SC1)。
您可以调整代码,将CV_64FC1
图片转换为CV_8UC1
,如:
...
Mat1b img8u;
src.convertTo(img8u, CV_8U);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(img8u, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);
...
此外,从评论中可以看出您使用的是Visual Studio 2010,但是使用msvc11(Visual Studio 2012)构建了OpenCV链接。
您需要使用Visual Studio 2012,或使用msvc10(Visual Studio 2010)重新编译OpenCV。如果您决定升级VS,您可以直接转到VS2013(并链接到vc12)或VS2015(但您也需要重新编译OpenCV)。
答案 1 :(得分:0)
您的问题是,当您需要CV_8UC1图像时,您正在为“findContours”提供CV_64F图像。您通常会将findContours传递给边缘检测器的输出。 (E.G. Canny)。
如果您将代码修改为以下内容,则可以在通过Canny过滤后在图像中找到轮廓。
Mat src=Mat(100,200,CV_64F),newimg;
Mat tCannyMat;
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
for (int i=25;i<80;i++)
for(int j=25;j<80;j++)
src.at<double>(i,j)=1;
imshow("img",src);
waitKey(0);
int lowThreshold = 0x3f;//This is the single value threshold
int ratio = 3;//This is the ratio to apply for the entire pixel
int kernel_size = 3;//This is the canny kernel size
//You can use your own edge detector/a different one here if you wish.
//Canny merely serves to give a working example.
cv::Canny( src, tCannyMat, lowThreshold, lowThreshold*ratio, kernel_size );
findContours(tCannyMat, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);