【发布时间】:2016-03-21 18:33:08
【问题描述】:
我需要检测图像的对象轮廓。为此,我使用了 OpenCV 库的函数 findContours。我在Windows 10 (x64) 上使用OpenCV 3.0 (x86),由我用contrib modules 编译。
问题
问题是,当我尝试使用此功能时,应用程序崩溃了。该错误不是异常或断言失败,我只能看到一个窗口告诉我应用程序已崩溃:
我测试过的内容
我已检查我传递给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);
}
【问题讨论】: