public static BufferedImage bufferedImage(Mat m) { int type = BufferedImage.TYPE_BYTE_GRAY; if (m.channels() > 1) { type = BufferedImage.TYPE_3BYTE_BGR; } BufferedImage image = new BufferedImage(m.cols(), m.rows(), type); m.get( 0, 0, ((DataBufferByte) image.getRaster().getDataBuffer()).getData()); // get all the pixels return image; }