Converting `BufferedImage` to `Mat` in OpenCV

Jompa234 picture Jompa234 · Feb 19, 2013 · Viewed 48.6k times · Source

How can I convert a BufferedImage to a Mat in OpenCV?

I'm using the JAVA wrapper for OpenCV(not JavaCV). As I am new to OpenCV I have some problems understanding how Mat works.

I want to do something like this. (Based on Ted W. reply):

BufferedImage image = ImageIO.read(b.getClass().getResource("Lena.png"));

int rows = image.getWidth();
int cols = image.getHeight();
int type = CvType.CV_16UC1;
Mat newMat = new Mat(rows, cols, type);

for (int r = 0; r < rows; r++) {
    for (int c = 0; c < cols; c++) {
        newMat.put(r, c, image.getRGB(r, c));
    }
}

Highgui.imwrite("Lena_copy.png", newMat);

This doesn't work. Lena_copy.png is just a black picture with the correct dimensions.

Answer

andriy picture andriy · Feb 26, 2013

I also was trying to do the same thing, because of need to combining image processed with two libraries. And what I’ve tried to do is to put byte[] in to Mat instead of RGB value. And it worked! So what I did was:

1.Converted BufferedImage to byte array with:

byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();

2. Then you can simply put it to Mat if you set type to CV_8UC3

image_final.put(0, 0, pixels);

Edit: Also you can try to do the inverse as on this answer