Ask Your Question
2

mutiply scalar to a vector opencv

asked 2013-11-23 06:19:19 -0600

zulfiqar gravatar image

updated 2015-09-06 19:44:17 -0600

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 flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
2

answered 2013-11-23 07:42:54 -0600

Guanta gravatar image

updated 2013-11-23 07:49:30 -0600

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]
edit flag offensive delete link more
3

answered 2013-11-23 08:35:11 -0600

Moster gravatar image

updated 2013-11-23 08:36:16 -0600

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]
edit flag offensive delete link more

Comments

1

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

Guanta gravatar imageGuanta ( 2013-11-23 09:23:45 -0600 )edit

Question Tools

Stats

Asked: 2013-11-23 06:19:19 -0600

Seen: 3,135 times

Last updated: Nov 23 '13