OpenCV Q&A Forum - RSS feedhttp://answers.opencv.org/questions/OpenCV answersenCopyright <a href="http://www.opencv.org">OpenCV foundation</a>, 2012-2018.Wed, 22 Oct 2014 21:46:01 -0500Mat per-element operation: vector-matrix multiplicationhttp://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/**I** is an *mxn* matrix and each element of **I** is a *1x3* vector (**I** is a 3-channel Mat image actually).
**M** is a *3x3* matrix.
**J** is an matrix having the same dimension as **I** and is computed as follows: each element of **J** is the vector-matrix product of the corresponding (i.e. having the same coordinates) element of **I** and **M**.
I.e. if *v1(r1,g1,b1)* is an element of **I** and *v2(r2,g2,b2)* is its corresponding element of **J**, then *v2* = *v1* * **M** (this is a vector-matrix product, not a per-element product).
Question: How to compute **J** efficiently (in terms of speed)?
Thank you for your help. Wed, 03 Apr 2013 03:50:48 -0500http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/Answer by StevenPuttemans for <p><strong>I</strong> is an <em>mxn</em> matrix and each element of <strong>I</strong> is a <em>1x3</em> vector (<strong>I</strong> is a 3-channel Mat image actually).</p>
<p><strong>M</strong> is a <em>3x3</em> matrix.</p>
<p><strong>J</strong> is an matrix having the same dimension as <strong>I</strong> and is computed as follows: each element of <strong>J</strong> is the vector-matrix product of the corresponding (i.e. having the same coordinates) element of <strong>I</strong> and <strong>M</strong>. </p>
<p>I.e. if <em>v1(r1,g1,b1)</em> is an element of <strong>I</strong> and <em>v2(r2,g2,b2)</em> is its corresponding element of <strong>J</strong>, then <em>v2</em> = <em>v1</em> * <strong>M</strong> (this is a vector-matrix product, not a per-element product).</p>
<p>Question: How to compute <strong>J</strong> efficiently (in terms of speed)?</p>
<p>Thank you for your help. </p>
http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?answer=10620#post-id-10620Matrix multiplication is where two matrices are multiplied directly. This operation multiplies matrix A of size [a x b] with matrix B of size [b x c] to produce matrix C of size [a x c]. In OpenCV it is achieved using the simple * operator:
C = A * B;
Bitwise multiplication is where each pixel in the output matrix is formed by multiplying that pixel in matrix A by its corresponding entry in matrix B. The input matrices should be the same size, and the output will be the same size as well. This is achieved using the mul() function:
output = A.mul(B);
I guess this answers your problem? The * operator on Mat is defined to do what you want.
***source** : [http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv](http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv)*
Wed, 03 Apr 2013 04:03:03 -0500http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?answer=10620#post-id-10620Comment by StevenPuttemans for <p>Matrix multiplication is where two matrices are multiplied directly. This operation multiplies matrix A of size [a x b] with matrix B of size [b x c] to produce matrix C of size [a x c]. In OpenCV it is achieved using the simple * operator:</p>
<pre><code>C = A * B;
</code></pre>
<p>Bitwise multiplication is where each pixel in the output matrix is formed by multiplying that pixel in matrix A by its corresponding entry in matrix B. The input matrices should be the same size, and the output will be the same size as well. This is achieved using the mul() function:</p>
<pre><code>output = A.mul(B);
</code></pre>
<p>I guess this answers your problem? The * operator on Mat is defined to do what you want.</p>
<p><strong><em>source</em></strong><em> : <a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv"></a><a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv">http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv</a></em></p>
http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=10628#post-id-10628Then you need a for loop, looping over the elements of the first matrix, thaWed, 03 Apr 2013 05:04:54 -0500http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=10628#post-id-10628Comment by Nesbit for <p>Matrix multiplication is where two matrices are multiplied directly. This operation multiplies matrix A of size [a x b] with matrix B of size [b x c] to produce matrix C of size [a x c]. In OpenCV it is achieved using the simple * operator:</p>
<pre><code>C = A * B;
</code></pre>
<p>Bitwise multiplication is where each pixel in the output matrix is formed by multiplying that pixel in matrix A by its corresponding entry in matrix B. The input matrices should be the same size, and the output will be the same size as well. This is achieved using the mul() function:</p>
<pre><code>output = A.mul(B);
</code></pre>
<p>I guess this answers your problem? The * operator on Mat is defined to do what you want.</p>
<p><strong><em>source</em></strong><em> : <a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv"></a><a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv">http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv</a></em></p>
http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=10637#post-id-10637I would like just to remind that my question is not how to do that, but how to do that **efficiently** (in terms of speed).Wed, 03 Apr 2013 07:04:32 -0500http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=10637#post-id-10637Comment by StevenPuttemans for <p>Matrix multiplication is where two matrices are multiplied directly. This operation multiplies matrix A of size [a x b] with matrix B of size [b x c] to produce matrix C of size [a x c]. In OpenCV it is achieved using the simple * operator:</p>
<pre><code>C = A * B;
</code></pre>
<p>Bitwise multiplication is where each pixel in the output matrix is formed by multiplying that pixel in matrix A by its corresponding entry in matrix B. The input matrices should be the same size, and the output will be the same size as well. This is achieved using the mul() function:</p>
<pre><code>output = A.mul(B);
</code></pre>
<p>I guess this answers your problem? The * operator on Mat is defined to do what you want.</p>
<p><strong><em>source</em></strong><em> : <a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv"></a><a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv">http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv</a></em></p>
http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=10641#post-id-10641I know, but I have searched multiple times at how to traverse the Mat element efficiently, havent found any solution yet, so interested to hear if you do.Wed, 03 Apr 2013 07:18:20 -0500http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=10641#post-id-10641Comment by Nesbit for <p>Matrix multiplication is where two matrices are multiplied directly. This operation multiplies matrix A of size [a x b] with matrix B of size [b x c] to produce matrix C of size [a x c]. In OpenCV it is achieved using the simple * operator:</p>
<pre><code>C = A * B;
</code></pre>
<p>Bitwise multiplication is where each pixel in the output matrix is formed by multiplying that pixel in matrix A by its corresponding entry in matrix B. The input matrices should be the same size, and the output will be the same size as well. This is achieved using the mul() function:</p>
<pre><code>output = A.mul(B);
</code></pre>
<p>I guess this answers your problem? The * operator on Mat is defined to do what you want.</p>
<p><strong><em>source</em></strong><em> : <a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv"></a><a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv">http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv</a></em></p>
http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=10627#post-id-10627Thanks. But that's not what I was searching for. Maybe I was not clear enough.
What I want to do is to multiply *every* element (that is a 3-component vector) of a matrix with a given 3x3 matrix.
for(int i=0;i<I.cols;i++)
{
for(int j=0;j<I.rows;j++)
{
I(j,i) = I(j,i)*M;
}
}Wed, 03 Apr 2013 05:00:45 -0500http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=10627#post-id-10627Comment by aledalgrande for <p>Matrix multiplication is where two matrices are multiplied directly. This operation multiplies matrix A of size [a x b] with matrix B of size [b x c] to produce matrix C of size [a x c]. In OpenCV it is achieved using the simple * operator:</p>
<pre><code>C = A * B;
</code></pre>
<p>Bitwise multiplication is where each pixel in the output matrix is formed by multiplying that pixel in matrix A by its corresponding entry in matrix B. The input matrices should be the same size, and the output will be the same size as well. This is achieved using the mul() function:</p>
<pre><code>output = A.mul(B);
</code></pre>
<p>I guess this answers your problem? The * operator on Mat is defined to do what you want.</p>
<p><strong><em>source</em></strong><em> : <a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv"></a><a href="http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv">http://stackoverflow.com/questions/10936099/matrix-multiplication-in-opencv</a></em></p>
http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=45262#post-id-45262That is called the Kronecker product and it's not supported by OpenCV, so you have to write your own.Wed, 22 Oct 2014 21:46:01 -0500http://answers.opencv.org/question/10619/mat-per-element-operation-vector-matrix-multiplication/?comment=45262#post-id-45262