title: interface OpenCV's Mat containers with blas for matrix multiplication
body: <p>I am processing UHD (2160 x 3840) images.
  One of the processing I do consist to process a Sobel filtering on X and Y axis then I have to multiply every output matrix by it's transpose and then I process the gradient image as the square root of the sum of the gradient.</p>
  
  <p>So : S = sqrt( S_x * S_x^t + S_y * S_y^t).</p>
  
  <p>Due to dimension of the image OpenCV take up to twenty seconds to process that without multithreading and ten with multithreading.</p>
  
  <p>I know there OpenCV call OpenCL in order to speed up the filtering operations so I think it can take a long time in order to try to gain performance from the filtering step.</p>
  
  <p>For the matrix multiplication I experience a kind of unstability from the OpenCV's OpenCL gemm kernel implementation.</p>
  
  <p>So I would like to try to use OpenBLAS insted.</p>
  
  <p>My questions are : </p>
  
  <p>1.) </p>
  
  <p>I wrote the following code but I face some issue for interface OpenCV's Mat objects :</p>
  
  <pre><code>template&lt;class _Ty&gt;
  void mm(cv::Mat&amp; A,cv::Mat&amp; B,cv::Mat&amp; C)
  {
      static_assert(true,"support matrix_multiply is only defined for floating precision numbers.");
  }
  
  template&lt;&gt;
  inline void mm&lt;float&gt;(cv::Mat&amp; A,cv::Mat&amp; B,cv::Mat&amp; C)
  {
      const int M = A.rows;
      const int N = B.cols;
      const int K = A.cols;
  
      cblas_sgemm( CblasRowMajor ,// 1
                   CblasNoTrans, // 2 TRANSA
                   CblasNoTrans, // 3 TRANSB
                   M,       // 4 M
                   N,       // 5 N
                   K,       // 6 K
                   1.,           // 7 ALPHA
                   A.ptr&lt;float&gt;(),//8 A
                   A.rows,        //9 LDA
                   B.ptr&lt;float&gt;(),//10 B
                   B.rows,        //11 LDB
                   0.,            //12 BETA
                   C.ptr&lt;float&gt;(),//13 C
                   C.rows);       //14 LDC
  
  }
  
  template&lt;&gt;
  inline void mm&lt;double&gt;(cv::Mat&amp; A,cv::Mat&amp; B,cv::Mat&amp; C)
  {
      cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,A.rows,B.cols,A.cols,1.,A.ptr&lt;double&gt;(),A.rows,B.ptr&lt;double&gt;(),B.cols,0.,C.ptr&lt;double&gt;(),C.rows);
  }
      void matrix_multiply(cv::InputArray _src1, cv::InputArray _src2, cv::OutputArray _dst)
      {
  
          CV_DbgAssert(  (_src1.isMat() || _src1.isUMat()) &amp;&amp; (_src1.kind() == _src2.kind()) &amp;&amp;
                        (_src1.depth() == _src2.depth()) &amp;&amp; (_src1.depth() == CV_32F) &amp;&amp; (_src1.depth() == _src1.type()) &amp;&amp;
                        (_src1.rows() == _src2.cols())
                         );
  
  
          cv::Mat src1 = _src1.getMat();
          cv::Mat src2 = _src2.getMat();
          cv::Mat dst;
  
          bool cpy(false);
  
          if(_dst.rows() == _src1.rows() &amp;&amp; _dst.cols() == _src2.cols() &amp;&amp; _dst.type() == _src1.type())
              dst = _dst.getMat();
          else
          {
              dst = cv::Mat::zeros(src1.rows,src2.cols,src1.type());
              cpy = true;
          }
  
          if(cpy)
              dst.copyTo(_dst);
      }
  </code></pre>
  
  <p>I tried to organize the datas as specified here :
  <a href="http://www.netlib.org/lapack/explore-html/db/dc9/group__single__blas__level3.html#gafe51bacb54592ff5de056acabd83c260" rel="nofollow">http://www.netlib.org/lapack/explore-html/db/dc9/group__single__blas__level3.html#gafe51bacb54592ff5de056acabd83c260</a></p>
  
  <p>without succes.
   This is my main issue</p>
  
  <p>2.) 
      I was thinking in order to try to speed up a little my implementation to apply the divide and conquer approach illustrated here :</p>
  
  <p><a href="https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm" rel="nofollow">https://en.wikipedia.org/wiki/Matrix_multiplication_algorithm</a></p>
  
  <p>But for only four submatrix.
  Does any one tried some similar approach or got a better way to gain performance in matrix multiplication (without using GPU) ?</p>
  
  <p>Thank you in advance for any help.</p>