OpenCV乘法标量和矩阵 [英] Opencv multiply scalar and matrix
问题描述
我一直在努力实现一些应该是微不足道的事情,并且在 Matlab 中是不重要的.
I have been trying to achieve something which should pretty trivial and is trivial in Matlab.
我想简单地实现以下目标:
I want to simply achieve something such as:
cv::Mat sample = [4 5 6; 4 2 5; 1 4 2];
sample = 5*sample;
之后应该是以下示例:
[20 24 30; 20 10 25; 5 20 10]
我尝试了scaleAdd
,Mul
,Multiply
,它们都不允许标量乘法器,也不需要相同的大小和类型"矩阵.在这种情况下,我可以创建一个1的矩阵,然后使用scale参数,但这看起来非常多余
I have tried scaleAdd
, Mul
, Multiply
and neither allow a scalar multiplier and require a matrix of the same "size and type". In this scenario I could create a Matrix of Ones and then use the scale parameter but that seems so very extraneous
任何直接的简单方法都很棒!
Any direct simple method would be great!
推荐答案
实际上,OpenCV确实支持标量值与operator*
重载相乘.不过,您可能需要正确初始化矩阵.
OpenCV does in fact support multiplication by a scalar value with overloaded operator*
. You might need to initialize the matrix correctly, though.
float data[] = {1 ,2, 3,
4, 5, 6,
7, 8, 9};
cv::Mat m(3, 3, CV_32FC1, data);
m = 3*m; // This works just fine
如果您主要对数学运算感兴趣,则cv::Matx
会更易于使用:
If you are mainly interested in mathematical operations, cv::Matx
is a little easier to work with:
cv::Matx33f mx(1,2,3,
4,5,6,
7,8,9);
mx *= 4; // This works too
这篇关于OpenCV乘法标量和矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!