OpenCV:将矩阵作为可选参数传递 [英] OpenCV: Passing a matrix as an optional argument

查看:119
本文介绍了OpenCV:将矩阵作为可选参数传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将矩阵作为函数中的可选参数传递.如果未提供参数,则将其设置为一个单位矩阵.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆