title: Convert OpenCV Mat object to BufferedImage
body: <p>I am trying to create a helper function using OpenCV Java API that would process an input image and return the output byte array. The input image is a jpg file saved in the computer. The input and output image are displayed in the Java UI using Swing.</p>
  
  <pre><code>System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  // Load image from file
  Mat rgba = Highgui.imread(filePath);
  
  Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0);
  
  // Convert back to byte[] and return
  byte[] return_buff = new byte[(int) (rgba.total() * rgba.channels())];
  rgba.get(0, 0, return_buff);
  return return_buff;
  </code></pre>
  
  <p>When the <code>return_buff</code> is returned and converted to BufferedImage I get NULL back. When I comment out the <code>Imgproc.cvtColor</code> function, the <code>return_buff</code> is properly converted to a BufferedImage that I can display. It seems like the <code>Imgproc.cvtColor</code> is returning a Mat object that I couldn't display in Java.</p>
  
  <p>Here's my code to convert from byte[] to BufferedImage:</p>
  
  <pre><code>InputStream in = new ByteArrayInputStream(inputByteArray);
  BufferedImage outputImage = ImageIO.read(in);
  </code></pre>
  
  <p>In above code, outputImage is NULL</p>
  
  <p>Does anybody have any suggestions or ideas?</p>