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.
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 the return_buff
is returned and converted to BufferedImage I get NULL back. When I comment out the Imgproc.cvtColor
function, the return_buff
is properly converted to a BufferedImage that I can display. It seems like the Imgproc.cvtColor
is returning a Mat object that I couldn't display in Java.
Here's my code to convert from byte[] to BufferedImage:
InputStream in = new ByteArrayInputStream(inputByteArray);
BufferedImage outputImage = ImageIO.read(in);
In above code, outputImage is NULL
Does anybody have any suggestions or ideas?
ImageIO.read(...)
(and thejavax.imageio
package in general) is for reading/writing images from/to file formats. What you have is an array containing "raw" pixels. It's impossible forImageIO
to determine file format from this byte array. Because of this, it will returnnull
.Instead, you should create a
BufferedImage
from the bytes directly. I don't know OpenCV that well, but I'm assuming that the result ofImgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0)
will be an image in grayscale (8 bits/sample, 1 sample/pixel). This is the same format asBufferedImage.TYPE_BYTE_GRAY
. If this assumption is correct, you should be able to do:Doing it this way, saves you one large byte array allocation and one byte array copy as a bonus. :-)
I used this kind of code to convert Mat object to Buffered Image.