Convert OpenCV Mat object to BufferedImage

后端 未结 2 1073
陌清茗
陌清茗 2021-02-15 18:43

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 comp

2条回答
  •  耶瑟儿~
    2021-02-15 19:26

    ImageIO.read(...) (and the javax.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 for ImageIO to determine file format from this byte array. Because of this, it will return null.

    Instead, you should create a BufferedImage from the bytes directly. I don't know OpenCV that well, but I'm assuming that the result of Imgproc.cvtColor(rgba, rgba, Imgproc.COLOR_RGB2GRAY, 0) will be an image in grayscale (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's backing array and copy the pixels directly into it
    byte[] data = ((DataBufferByte) gray.getRaster().getDataBuffer()).getData();
    rgba.get(0, 0, data);
    

    Doing it this way, saves you one large byte array allocation and one byte array copy as a bonus. :-)

提交回复
热议问题