c++ - OpenCV:将矩阵作为可选参数传递

标签 c++ opencv

我想知道如何将矩阵作为可选参数传递给函数。如果未给出参数,则将其设置为单位矩阵。

如果我做类似的事情

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/

相关文章:

c++ - boost::lock_guard 分配、构造函数和析构函数开销

c++ - 为什么我的类(class)规模为零?如何确保不同的对象具有不同的地址?

c++ - 当返回值是class或class<class>或class<class, class>等时如何使用enable_if?

c++ - 将两个 uint16_t 数字相乘得到一个 int

opencv - Raspberry Pi 4中的mp4编解码器:未将帧写入视频

c++ - 使用 OpenCV 进行相机校准 - findChessboardCorners 返回 false

c# - 写入时图像不更新..奇怪的事情发生了

c++ - 相同的键,std::unordered_map 的多个条目?

python - 使用 opencv imread 读取时图像高度和宽度被交换

c++ - 合并两个显示亮度的图像