title: OpenCV - Convert vector of vector to Mat
body: <p>I want to convert a <code>vector&lt;vector&lt;double&gt; &gt;</code> to <code>Mat</code> because I need to apply a custom smooth filter to this values.</p>
  
  <p>The image below shows the CORRECT values</p>
  
  <p><img src="https://i.stack.imgur.com/LHwXz.png" alt="This image shows the CORRECT values"></p>
  
  <p>I tried this.</p>
  
  <pre><code>std::vector&lt;std::vector&lt;double&gt; &gt; angles;
  calculateAngles(angles);
  Mat matAngles(angles.size(), angles.at(0).size(), CV_64FC1, angles.data());
  </code></pre>
  
  <p>But the values in the first columns are wrong converted, with value 2.12566e-314.</p>
  
  <p>The resulting image</p>
  
  <p><img src="https://i.stack.imgur.com/fNCPR.png" alt="result image"></p>
  
  <p>I also tried put the values directly in the <code>Mat</code>.</p>
  
  <pre><code>void calculateAngles(cv::Mat&amp; im, cv::Mat&amp; angles, int blockSize, int(*f)(int x, int y), int(*g)(int x, int y)){
  
  static int ySobel[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
  static int xSobel[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
  
  angles.create(cv::Size(im.cols/blockSize+1, im.rows/blockSize+1), CV_64FC1);
  
  int nominator;
  int denominator;
  int GX, GY;
  double angle;
  for(int i = 1; i &lt; im.cols; i += blockSize){
      for(int j = 1; j &lt; im.rows; j += blockSize){
          nominator = 0;
          denominator = 0;
          for(int k = i; k &lt; std::min(i + blockSize, im.cols-1); k++){
              for(int l = j; l &lt; std::min(j + blockSize, im.rows-1); l++){
                  GX = applyKernelAt(im, xSobel, k, l);
                  GY = applyKernelAt(im, ySobel, k, l);
  
                  nominator += f(GX, GY);
                  denominator += g(GX, GY);
              }
          }
  
          angle = (PI + std::atan2(nominator, denominator)) / 2;
          angles.at&lt;double&gt;((i-1)/blockSize, (j-1)/blockSize) = angle;
      }
  }
  }
  </code></pre>
  
  <p>But the values are repeated after a certain point.</p>
  
  <p><img src="https://i.stack.imgur.com/RqiH3.png" alt="enter image description here"></p>
  
  <p>This is how the values are printed</p>
  
  <pre><code>void drawLines(cv::Mat&amp; src, cv::Mat&amp; dst, cv::Mat&amp; angles, int blockSize){
  cv::Size size = src.size();
  dst.create(src.size(), src.type());
  
  // for a white background
  dst += 255;
  
  cv::cvtColor(dst, dst, CV_GRAY2BGR);
  
  int x1,y1,x2,y2;
  
  for(int i = 1; i &lt; size.width; i += blockSize){
      for(int j = 1; j &lt; size.height; j += blockSize){
          double tang = std::tan(angles.at&lt;double&gt;((i-1)/blockSize,(j-1)/blockSize));
  
          if( tang &gt;= -1 &amp;&amp; tang &lt;= 1 ){
              x1 = i;
              y1 = (-blockSize/2) * tang + j + blockSize/2;
              x2 = i + blockSize;
              y2 = (blockSize/2) * tang + j + blockSize/2;
          }else{
              x1 = i + blockSize/2 + blockSize/(2*tang);
              y1 = j + blockSize/2;
              x2 = i + blockSize/2 - blockSize/(2*tang);
              y2 = j -blockSize/2;
          }
  
          cv::line(dst, cv::Point(x1,y1), cv::Point(x2,y2), cv::Scalar(0,0,255));
      }
  }
  }
  </code></pre>