title: 3D matrix multiplication in opencv for RGB color mixing
body: <p>I'm trying to perform an RGB Color mixing operation in opencv. I have the image contained in an MxNx3 Mat. I would like to multiple this with a 3x3 matrix. In Matlab I do the following:
  *Flatten the image from MxNx3 to a MNx3
  *multiply the MNx3 matrix by the 3x3 color mixing matrix 
  *reshape back to a MxNx3</p>
  
  <p>In Opencv I would like to do the following:</p>
  
  <pre><code>void RGBMixing::mixColors(Mat &amp;imData, Mat &amp;rgbMixData)
  {
     float rgbmix[] = {1.4237, -0.12364, -0.30003, -0.65221, 2.1936, -0.54141, -0.38854, -0.47458, 1.8631};
     Mat rgbMixMat(3, 3, CV_32F, rgbmix);
     // Scale the coefficents
     multiply(rgbMixMat, 1, rgbMixMat, 256);
     Mat temp = imData.reshape(0, 1);
     temp = temp.t();
     multiply(temp, rgbMixMat, rgbMixData);
  }
  </code></pre>
  
  <p>This compiles but generates an exception:</p>
  
  <blockquote>
    <p>OpenCV Error: Sizes of input arguments do not match (The operation is
    neither 'a rray op array' (where arrays have the same size and the
    same number of channels) , nor 'array op scalar', nor 'scalar op
    array') in arithm_op, file C:/slave/WinI
    nstallerMegaPack/src/opencv/modules/core/src/arithm.cpp, line 1253
    terminate called after throwing an instance of 'cv::Exception'<br>
    what(): 
    C:/slave/WinInstallerMegaPack/src/opencv/modules/core/src/arithm.cpp:
    1253: error: (-209) The operation is neither 'array op array' (where
    arrays have  the same size and the same number of channels), nor
    'array op scalar', nor 'sca lar op array' in function arithm_op</p>
    
    <p>This application has requested the Runtime to terminate it in an
    unusual way. Please contact the application's support team for more
    information.</p>
  </blockquote>
  
  <hr>
  
  <p>Update 1:</p>
  
  <p>This is code that appears to work:</p>
  
  <pre><code>void RGBMixing::mixColors(Mat &amp;imData, Mat&amp;rgbMixData)
  {
      Size tempSize;
      uint32_t channels;
  
      float rgbmix[] = {1.4237, -0.12364, -0.30003, -0.65221, 2.1936, -0.54141, -0.38854, -0.47458, 1.8631};
      Mat rgbMixMat(3, 3, CV_32F, rgbmix);
      Mat flatImage = imData.reshape(1, 3);
      tempSize = flatImage.size();
      channels = flatImage.channels();
      cout &lt;&lt; "temp channels: " &lt;&lt; channels &lt;&lt; " Size: " &lt;&lt; tempSize.width &lt;&lt; " x " &lt;&lt; tempSize.height &lt;&lt; endl;
      Mat flatFloatImage;
      flatImage.convertTo(flatFloatImage, CV_32F);
      Mat mixedImage = flatFloatImage.t() * rgbMixMat;
      mixedImage = mixedImage.t();
      rgbMixData = mixedImage.reshape(3, 1944);
      channels = rgbMixData.channels();
      tempSize = rgbMixData.size();
      cout &lt;&lt; "temp channels: " &lt;&lt; channels &lt;&lt; " Size: " &lt;&lt; tempSize.width &lt;&lt; " x " &lt;&lt; tempSize.height &lt;&lt; endl;
  }
  </code></pre>
  
  <p>But the resulting image is distorted. If I skip the multiplication of the two matrices and just assign </p>
  
  <pre><code>mixedImage = flatFloatImage
  </code></pre>
  
  <p>The resulting image looks fine (just not color mixed). So I must be doing something wrong, but am getting close.</p>