OpenCV findContour:没有匹配函数

OpenCV findContour : no matching function

本文关键字:函数 findContour OpenCV      更新时间:2023-10-16

我正在尝试在特定场景中使用Hough线,并且findContours方法没有出现匹配函数错误

代码

...
Mat bw, hsvdst;
...
bw = Mat::zeros(hsvdst.rows, hsvdst.cols, CV_8UC1);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
findContours(bw.clone(), contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

错误

error: no matching function for call to ‘findContours(cv::Mat, st
d::vector<std::vector<cv::Point_<int> > >&, std::vector<cv::Vec<int, 4> >&, cv::<anonymous enum>, cv::<anonymous enum>)
note: candidates are:
void cv::findContours(cv::InputOutputArray, cv::OutputArrayOfArr
ays, cv::OutputArray, int, int, cv::Point)
note:   no known conversion for argument 1 from ‘cv::Mat’ to ‘cv::Inpu
tOutputArray {aka const cv::_OutputArray&}’

请帮忙,我不确定我在这里遗漏了什么。

环境:OpenCV 2.4.6.1;Eclipse CDT,Ubuntu 12.04.2

我通过更换

findContours(bw.clone(), contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

带有

Mat m = bw.clone(); findContours(m, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

不过,这有点奇怪,因为findContour的定义的第一个参数是InputOutputArray类型,它映射到Map类型,而clone方法也返回类型Mat.

我认为这里的问题是Mat::clone()返回一个临时的,而您无法获得对临时的引用。_OutputArray的构造函数采用Mat&。首先将其分配给一个变量会起作用(如您在回答中所示)。