OpenCV 3.0 findContours崩溃 [英] OpenCV 3.0 findContours crashes

查看:335
本文介绍了OpenCV 3.0 findContours崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要检测图像的对象轮廓.为此,我使用的是OpenCV库的函数findContours.我正在使用contrib modules编译的Windows 10 (x64)上的OpenCV 3.0 (x86).

I need to detect the object's contour of an image. For that purpose, I'm using the OpenCV library's function findContours. I'm using OpenCV 3.0 (x86) on Windows 10 (x64) compiled by me with the contrib modules.

问题是,当我尝试使用此功能时,应用程序崩溃.该错误不是异常或断言失败,我只能看到一个窗口告诉我该应用程序已崩溃:

The problem is that, when I try to use this function, the application crashes. The error is not an exception or an assertion failure, I can only see a window saying to me that the application has crashed:

我已经检查过要传递给findContours的图像是二进制图像:

I have checked that the image I'm passing to findContours is a binary image:

我检查了图像的类型,它是0,与CV_8U值相同.

I have checked the type of the image, which is 0, the same as CV_8U value.

我什至检查了直方图,只有像素的值为0和1.

I have even checked the histogram, and there are only pixels with values 0 and 1.

我还从OpenCV教程和论坛中搜索了示例,并且尝试执行与示例中完全相同的操作,程序再次崩溃.

I have also searched for examples from OpenCV tutorials and forums, and I have tried to do exactly the same than in the example, and the program crashes again.

这是我正在执行的代码:

Here is the code that I'm executing:

// This is the main function:
int test_findContours(const std::string &path){
    Mat img = imread(path, IMREAD_GRAYSCALE);
    if (!img.data){
        cout << "ERROR" << endl;
        return -1;
    }
    Mat mask;
    getRemBackgroundMask(img, mask);

    vector< vector<Point> > contours;

    // Here the program crashes:
    findContours(mask, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);
    return 0;
}

// Get the mask to remove the background
void getRemBackgroundMask(const Mat &img, Mat &mask) {
    threshold(img, mask, 70, 1, THRESH_BINARY_INV);

    Mat kernel = getStructuringElement(MORPH_RECT, Size(3, 3));
    openning(mask, mask, kernel);
}

void openning(const Mat &binary, Mat &result, const Mat &kernel){
    erode(binary, result, kernel);
    dilate(binary, result, kernel);
}

推荐答案

我发现了问题.由于我在将Visual Studio 2013的Debug配置与OpenCV的Release库(* .lib没有'd')一起使用时,显然发生了所提到的错误.我已经使用Release配置测试了该程序,并且可以正常工作.我什至绘制了检测到的轮廓,该功能正常.

I have found the problem. The mentioned error apparently occurs because I was using the Debug configuration of Visual Studio 2013 with the OpenCV's Release libraries (*.libs which don't have the 'd'). I have tested the program with the Release configuration and it works. I have even drawed the detected contours and the function works ok.

这篇关于OpenCV 3.0 findContours崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆