且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 OpenCV 中将 `BufferedImage` 转换为 `Mat`

更新时间:2023-11-11 21:12:22

我也试图做同样的事情,因为需要将处理过的图像与两个库结合起来.我尝试做的是将 byte[] 放入 Mat 而不是 RGB 值.它奏效了!所以我所做的是:

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 到字节数组:

1.Converted BufferedImage to byte array with:

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

2.然后,如果您将 type 设置为 CV_8UC3

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

image_final.put(0, 0, pixels);

您也可以尝试按照 这个答案