title: Converting `BufferedImage` to `Mat` in OpenCV
body: <p>How can I convert a <code>BufferedImage</code> to a <code>Mat</code> in <code>OpenCV</code>? </p>
  
  <p>I'm using the JAVA wrapper for <code>OpenCV</code>(not <em>JavaCV</em>). As I am new to <code>OpenCV</code> I have some problems understanding how <code>Mat</code> works. </p>
  
  <p>I want to do something like this. (Based on Ted W. reply):</p>
  
  <pre class="lang-java prettyprint-override"><code>BufferedImage image = ImageIO.read(b.getClass().getResource("Lena.png"));
  
  int rows = image.getWidth();
  int cols = image.getHeight();
  int type = CvType.CV_16UC1;
  Mat newMat = new Mat(rows, cols, type);
  
  for (int r = 0; r &lt; rows; r++) {
      for (int c = 0; c &lt; cols; c++) {
          newMat.put(r, c, image.getRGB(r, c));
      }
  }
  
  Highgui.imwrite("Lena_copy.png", newMat);
  </code></pre>
  
  <p>This doesn't work. <code>Lena_copy.png</code> is just a black picture with the correct dimensions. </p>