2016-03-21 76 views
3

我需要检测对象的图像轮廓。为此,我使用OpenCV库的功能findContours。我在编辑的Windows 10 (x64)上使用OpenCV 3.0 (x86)OpenCV 3.0 findContours崩溃

问题

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

enter image description here

我已经测试

我已经检查了我的形象“M传递到findContours是二值图像:

我已经检查了图像,该图像是0,同为CV_8U值的类型。

我甚至查直方图,并有只值0和1

像素我也搜索了从OpenCV的教程和论坛的例子,我试图做同样的比例如,程序又崩溃了。

代码

下面是我执行的代码:

// 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); 
} 

回答

4

我已经找到了问题。所提到的错误显然是因为我使用Visual Studio 2013的Debug配置和OpenCV的发布库(* .libs,它们没有'd')。我已经使用Release配置测试了该程序并且它可以正常工作。我甚至画出了检测到的轮廓,功能正常。

+2

您必须小心,不要在发行版(或混合编译器版本)中的调试应用程序或调试dll中使用发行版dll。其中任何一种情况都会导致您有超过1个不兼容的堆,这会在尝试从另一个堆释放分配的内存时导致堆损坏。堆损坏并不总是会导致即时崩溃,因此可能难以确定原因。 – drescherjm