OpenCV 3.0 findContours 崩溃

Dan*_*Dan 5 c++ opencv

我需要检测图像中对象的轮廓。为此,我使用 OpenCV 库的 function findContours。我正在使用OpenCV 3.0 (x86)Windows 10 (x64)contrib modules.

问题

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

在此输入图像描述

我测试过什么

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

我检查了图像的类型,是0,和CV_8Uvalue一样。

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

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.

The code

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);
}
Run Code Online (Sandbox Code Playgroud)

Dan*_*Dan 6

我已经找到问题了。显然发生上述错误是因为我将 Visual Studio 2013 的调试配置与 OpenCV 的发布库(*.libs 没有“d”)一起使用。我已经使用发布配置测试了该程序并且它可以工作。我什至绘制了检测到的轮廓,并且该功能工作正常。

  • 您必须小心不要在调试应用程序中使用Release dll或在release中使用Debug dll(或混合编译器版本)。其中任何一个都会导致您拥有多个不兼容的堆,当一个试图从另一个尝试释放分配的内存时,这会导致堆损坏。堆损坏并不总是会导致立即崩溃,因此可能很难确定原因。 (2认同)