我想知道如何将矩阵作为可选参数传递给函数。如果未给出参数,则将其设置为单位矩阵。
如果我做类似的事情
Mat function(const Mat &I, Mat &matrix=Mat::eye(2, 3, CV_32F))
{
/// some code
return matrix;
}
然后我得到以下错误:
error: could not convert ‘cv::Mat::eye(int, int, int)(3, 5)’ from ‘cv::MatExpr’ to ‘cv::Mat&’
提前感谢您的任何建议。
最佳答案
你遇到这个问题是因为 C++ does not allow a temporary (the default value in this case) to be bound to non-const reference. 您有三个(至少)选择:
Mat function(const Mat &I, const Mat & matrix = Mat::eye(2, 3, CV_32F))
或
Mat function(const Mat &I, Mat const & matrix = Mat::eye(2, 3, CV_32F))
或
Mat function(const Mat &I, Mat matrix = Mat::eye(2, 3, CV_32F))
或者如 berak 所说,您可以将默认值设置为空垫,并使用 Mat::empty()
对其进行测试:
Mat function(const Mat &I, Mat & matrix = Mat())
关于c++ - OpenCV:将矩阵作为可选参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25785038/