title: OpenCV: Conversion of vector<vector<Point>> to vector<Mat>, problems with call by reference
body: <p>I was trying to extract contour lines from an image, using OpenCV 2.4.9.<br>
  The <code>findContours</code> function does most of the work, but it returns the contour lines<br>
  as type <code>vector &lt; vector &lt; Point &gt; &gt;</code>.
  I need to convert them to type <code>vector &lt; Mat &gt;</code> for later uses.<br>
  I used a constructor of the <code>Mat</code> class to do the job and everything works fine until I pass the result from one function to the other via call by reference. 
  The following code reproduces the error:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;opencv2/opencv.hpp&gt;
  using namespace std;
  using namespace cv;
  
  void getCont(Mat&amp; img, vector&lt;Mat&gt;&amp; cLines, int thresh)
  {
      //binarize the image
      Mat imgBin;
      threshold(img, imgBin, thresh, 255, THRESH_BINARY_INV);
  
      //find contour lines
      vector&lt;vector&lt;Point&gt;&gt; contours;
      findContours(imgBin, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
  
      //convert vector&lt;vector&lt;Point&gt;&gt; to vector&lt;Mat&gt;
      for (int i = 0; i &lt; contours.size(); i++)
          cLines.push_back(Mat(contours[i])); 
  
      cerr &lt;&lt; "cLines[0] in getCont:\n";
      cerr &lt;&lt; "cLines[0].rows: " &lt;&lt; cLines[0].rows &lt;&lt; "\n";
      cerr &lt;&lt; "cLines[0].cols: " &lt;&lt; cLines[0].cols &lt;&lt; "\n";
      cerr &lt;&lt; "cLines[0].channels(): " &lt;&lt; cLines[0].channels() &lt;&lt; "\n";
      cerr &lt;&lt; "cLines[0].type(): " &lt;&lt; cLines[0].type() &lt;&lt; "\n";
      cerr &lt;&lt; "cLines[0].row(0): " &lt;&lt; cLines[0].row(0) &lt;&lt; "\n";
      cerr &lt;&lt; endl &lt;&lt; endl;
  }
  
  int main()
  {
      Mat img = imread("leaf.jpg", 0);
      int thresh = 124;
  
      vector&lt;Mat&gt; cLines;
      getCont(img, cLines, thresh);
  
      cerr &lt;&lt; "cLines[0] in main:\n";
      cerr &lt;&lt; "cLines[0].rows: " &lt;&lt; cLines[0].rows &lt;&lt; "\n";
      cerr &lt;&lt; "cLines[0].cols: " &lt;&lt; cLines[0].cols &lt;&lt; "\n";
      cerr &lt;&lt; "cLines[0].channels(): " &lt;&lt; cLines[0].channels() &lt;&lt; "\n";
      cerr &lt;&lt; "cLines[0].type(): " &lt;&lt; cLines[0].type() &lt;&lt; "\n";
      cerr &lt;&lt; "cLines[0].row(0): " &lt;&lt; cLines[0].row(0) &lt;&lt; "\n";
  
      return 0;
  }
  </code></pre>
  
  <p>The error occurs in main, in the line preceding the return statement, when I try to print out the first row of the first element of <code>cLines</code>. For different input images, I either get a message, which tells me, that the .exe is not working properly and has to quit or the values are actually printed out, but they differ from the output of the <code>getCont</code> function (in main, I get negative values, so it looks like there is some overflow). I'm using Visual Studio 2013 Express on Windows 8, 64 bit machine (but I'm using the x86 DLL libraries of OpenCV). Can anyone reproduce the error on a different system?</p>
  
  <p>I thought there was some implicit type cast, so I printed out the size and type of <code>cLines</code> in <code>getCont</code> and <code>main</code>, but the results are the same. The error does not occur, when I put the code of the <code>getCont</code> function into <code>main</code>, so that I avoid the additional function call.
  Also, everything works fine, when I replace the loop</p>
  
  <pre><code>for (int i = 0; i &lt; contours.size(); i++)
      cLines.push_back(Mat(contours[i])); 
  </code></pre>
  
  <p>by the following:</p>
  
  <pre><code>for (int i = 0; i &lt; contours.size(); i++)
  {
      vector&lt;Point&gt; currPts = contours.at(i);
      Mat currLine(currPts.size(), 1, CV_32SC2);
      for (int j = 0; j &lt; currPts.size(); j++)
      {
          currLine.at&lt;Vec2i&gt;(j, 0).val[0] = currPts.at(j).x;
          currLine.at&lt;Vec2i&gt;(j, 0).val[1] = currPts.at(j).y;
      }
      cLines.push_back(currLine);
  }
  </code></pre>
  
  <p>Does anyone know what's going on?</p>