First time here? Check out the FAQ!

Ask Your Question
2

mutiply scalar to a vector opencv

asked Nov 23 '13

zulfiqar gravatar image

updated Sep 7 '15

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

Preview: (hide)

2 answers

Sort by » oldest newest most voted
2

answered Nov 23 '13

Guanta gravatar image

updated Nov 23 '13

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]
Preview: (hide)
3

answered Nov 23 '13

Moster gravatar image

updated Nov 23 '13

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]
Preview: (hide)

Comments

1

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

Guanta gravatar imageGuanta (Nov 23 '13)edit

Question Tools

Stats

Asked: Nov 23 '13

Seen: 3,174 times

Last updated: Nov 23 '13