Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opencv multiply scalar and matrix

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;

After which sample should just be:

[20 24 30; 20 10 25; 5 20 10]

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!

like image 211
Arpan Shah Avatar asked Jul 27 '13 00:07

Arpan Shah


1 Answers

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

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
like image 53
Aurelius Avatar answered Sep 22 '22 19:09

Aurelius