Ask Your Question
0

cv::Mat element matrix multiplication

asked 2015-06-01 10:36:14 -0600

trucna gravatar image

updated 2015-06-01 11:04:32 -0600

I need to take concrete element from three channel matrix:

a.row(0).col(0)

and use this element for multiplication:

multiply(a.row(0).col(0), b, c);

Something like this:

Mat a(5, 5, CV_32FC3);  
Mat b(5, 5, CV_32FC3); 
Mat c;

multiply(a.row(0).col(0), b, c);

But it is not working. Do you have some tip how to do it?

P.S. : it is really easy for one channel image using

a.at<float>(0,0)

Edit

Ok, let me give an example.

Mat a:

[ 91,   2,  79, 179,  52, 205, 236,   8, 181, 239,  26, 248, 207, 218,  45;
 183, 158, 101, 102,  18, 118,  68, 210, 139, 198, 207, 211, 181, 162, 197;
 191, 196,  40,   7, 243, 230,  45,   6,  48, 173, 242, 125, 175,  90,  63;
  90,  22, 112, 221, 167, 224, 113, 208, 123, 214,  35, 229,   6, 143, 138;
  98,  81, 118, 187, 167, 140, 218, 178,  23,  43, 133, 154, 150,  76, 101]

Mat b:

[  8,  38, 238,  84,  47,   7, 117, 246, 163, 237,  69, 129,  60, 101,  41;
 190,  50,  90,  72, 168, 109, 121, 220, 114, 248,  99, 202, 199, 212,  79;
 128, 198,  90, 168,  76, 145, 181, 118,   8,  63, 114, 217, 164, 158, 217;
 237, 147,  44, 207,  54, 182,  65, 197, 191, 239,  72, 166, 236, 240,   3;
 151,  91, 246, 116, 238,  94,  63, 252, 232,  25,  17,   8, 166, 116,  81]

I want to take element [0; 0] from the a which is (91, 2, 79) and use it for per-element multiplication to achieve Mat c:

[  728, 76, 18802, 7644, 94, 553, 10647, 492, 12877, 21567, 138, 10191, 5460, 202, 3239;
 17290, 100, 7110, 6552, 336, 8611, 11011, 440, 9006, 22568, 198, 15958, 18109, 424, 6241;
 11648, 396, 7110, 15288, 152, 11455, 16471, 236, 632, 5733, 228, 17143, 14924, 316, 17143;
 21567, 294, 3476, 18837, 108, 14378, 5915, 394, 15089, 21749, 144, 13114, 21476, 480, 237;
 13741, 182, 19434, 10556, 476, 7426, 5733, 504, 18328, 2275, 34, 632, 15106, 232, 6399]
edit retag flag offensive close merge delete

Comments

it's unclear, what you're trying to achieve in the end. could you concentrate more on that, and less on your faulted attempts ?

also, multiply does a per-element multiplication, not a matrix one (that's cv::gemm).

berak gravatar imageberak ( 2015-06-01 10:43:38 -0600 )edit
1

I have added an example.

trucna gravatar imagetrucna ( 2015-06-01 11:05:37 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-06-01 17:35:33 -0600

theodore gravatar image

updated 2015-06-01 17:47:01 -0600

@trucna why not split the original multi-channel matrices into individual matrices within a vector<Mat>, then multiply the individual correspondent channels by using the mul() and finally re-merge the final outcome into a multi-channel matrix again. Example, pseudo code:

Mat a;
Mat b;
Mat c;

vector<Mat> a_channels;
vector<Mat> b_channels;
vector<Mat> c_channels;

split(a, a_channels);
split(b, b_channels);

for(i = 0; i < a_channels.size() || b_channels.size(); i++)
{
    c_channels.push_back(a_channels[i].mul(b_channels[i]));
}

merge(c_channels, c);

and then you have your result.

edit flag offensive delete link more

Comments

Thank you, but this is not a solution. You still need to somehow choose concrete element from Mat a. The choosing part is the one not working. This:

a.row(0).col(0)

can not be applied in multiply and here is the problem.

Btw, I am focused on performance in this implementation, so I would like to not doing such a stuff from your advice, unless absolutely necessary.

trucna gravatar imagetrucna ( 2015-06-02 03:59:42 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-06-01 10:36:14 -0600

Seen: 2,545 times

Last updated: Jun 01 '15