# mutiply scalar to a vector opencv

I want to mutiply 2 with each element of vec3 in opencv as we do in Matlab simplt by ".*". I searched alot but didn't find any command is their any command for this or not in opencv? thanks in advance for any help

edit retag close merge delete

Sort by ยป oldest newest most voted

You need to use the .mul() method. Note that afaik mixing of the types (e.g. Scalar.mul(Vec3i)) won't work.

Examples:

std::cerr << "test opencv: " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;
cv::Vec3i aa, bb;
aa[0] = aa[1] = aa[2] = 2;
bb[0] = bb[1] = bb[2] = 3;
// try multiplication of vectors
cv::Vec3i ab = aa.mul(bb);
std::cerr << "1. " << aa << " .* " << bb << " = " <<  ab << std::endl;
// try multiplication of scalars
cv::Scalar cc = cv::Scalar(aa).mul(cv::Scalar(bb));
std::cerr << "2. " << cv::Scalar(aa) << " .* " << cv::Scalar(bb) << " = " <<  cc << std::endl;
// try multiplication of matrices
cv::Mat ccc  = cv::Mat(aa).mul( cv::Mat(bb) );
std::cerr << "3. " << cv::Mat(aa) << " .* " << cv::Mat(bb) << " = " <<  ccc << std::endl;


Output:

test opencv: 2.4.7
1. [2, 2, 2] .* [3, 3, 3] = [6, 6, 6]
2. [2, 2, 2, 0] .* [3, 3, 3, 0] = [6, 6, 6, 0]
3. [2; 2; 2] .* [3; 3; 3] = [6; 6; 6]

more

Its pretty simple actually. Check the example

cv::Vec3i a;
a[0] = a[1] = a[2] = 2;

cout << a << endl;

a = 2 * a;

cout << a << endl;


Output:

[2, 2, 2]
[4, 4, 4]

more

1

ah, I thought he'd like to multiply two vectors, misread it...

( 2013-11-23 09:23:45 -0500 )edit

Official site

GitHub

Wiki

Documentation