OpenCV:将矩阵作为可选参数传递 [英] OpenCV: Passing a matrix as an optional argument
问题描述
我想知道如何将矩阵作为函数中的可选参数传递.如果未提供参数,则将其设置为一个单位矩阵.
I would like to know how to pass a matrix as an optional argument in a function. If the argument is not given, then it is set to be an identity matrix.
如果我做类似的事情
Mat function(const Mat &I, Mat &matrix=Mat::eye(2, 3, CV_32F))
{
/// some code
return matrix;
}
然后出现以下错误:
错误:无法从以下位置转换"cv :: Mat :: eye(int,int,int)(3,5)" "cv :: MatExpr"到"cv :: Mat&"
error: could not convert ‘cv::Mat::eye(int, int, int)(3, 5)’ from ‘cv::MatExpr’ to ‘cv::Mat&’
提前感谢您的任何建议.
Thanks in advance for any suggestions.
推荐答案
You are getting this problem because C++ does not allow a temporary (the default value in this case) to be bound to non-const reference. You have three (at least) choices:
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,然后使用Mat::empty()
对其进行测试:
Or as berak said, you could make the default an empty Mat, and test for it with Mat::empty()
:
Mat function(const Mat &I, Mat & matrix = Mat())
这篇关于OpenCV:将矩阵作为可选参数传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!