1

我在OpenCV中的代码运行良好,直到我想找到轮廓

findContours(src, contours,hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE);

然后我不断收到以下错误:

“Contour.exe 中 0x773e3e28 处未处理的异常:Microsoft C++ 异常:cv::Exception at memory location 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);
4

2 回答 2

1

引用关于findContours的 OpenCV 文档

image – 源,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)。

于 2015-09-14T00:25:48.760 回答
0

你的问题是当它需要一个 CV_8UC1 图像时,你给“findContours”一个 CV_64F 图像。您通常将 findContours 传递给边缘检测器的输出。(例如康尼)。

如果您将代码修改为以下内容,您可以在通过 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);
于 2015-09-14T00:28:26.767 回答