Convert OpenCV Mat object to BufferedImage

I am trying to create a helper function using the Java OpenCV API that will process the input image and return an output byte array. The input image is a jpg file saved on a computer. An image of input and output is displayed in the Java user interface using Swing.

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; 

When return_buff returns and converts to BufferedImage, I return NULL. When I comment on the function Imgproc.cvtColor , return_buff correctly converted to a BufferedImage, which I can display. It seems that Imgproc.cvtColor returns a Mat object that I could not display in Java.

Here is my code to convert from byte [] to BufferedImage:

 InputStream in = new ByteArrayInputStream(inputByteArray); BufferedImage outputImage = ImageIO.read(in); 

In the above code, outputImage is NULL

Does anyone have any suggestions or ideas?

+5
source share
2 answers

ImageIO.read(...) (and the javax.imageio package as a whole) is intended for reading / writing images from / to file formats. You have an array containing raw pixels. ImageIO can determine the file format from this byte array. Because of this, it will return null .

Instead, you should create a BufferedImage from bytes directly. I do not know OpenCV very well, but I assume that the result of Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0) will be a grayscale image (8 bits / sample, 1 sample / pixel). This is the same format as BufferedImage.TYPE_BYTE_GRAY . If this assumption is correct, you should be able to do:

 // Read image to Mat as before Mat rgba = ...; Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0); // Create an empty image in matching format BufferedImage gray = new BufferedImage(rgba.width(), rgba.height(), BufferedImage.TYPE_BYTE_GRAY); // Get the BufferedImage backing array and copy the pixels directly into it byte[] data = ((DataBufferByte) gray.getRaster().getDataBuffer()).getData(); rgba.get(0, 0, data); 

By doing this this way, you save one allocation of a large byte array and one copy of the byte array as a bonus. :-)

+11
source

I used this code to convert a Mat object to a Buffered Image.

 static BufferedImage Mat2BufferedImage(Mat matrix)throws Exception { MatOfByte mob=new MatOfByte(); Imgcodecs.imencode(".jpg", matrix, mob); byte ba[]=mob.toArray(); BufferedImage bi=ImageIO.read(new ByteArrayInputStream(ba)); return bi; } 
+5
source

Source: https://habr.com/ru/post/1207504/


All Articles