问题理解OpenCV转换垫到BufferedImage

时间:2019-03-03 08:05:32

标签: opencv bufferedimage

我是JAVA OpenCV的新用户,我只是在通过今天的官方教程学习如何将Mat对象转换为BufferedImage

从演示代码中,我可以了解到输入图像源是矩阵形式,然后sourcePixels似乎是图像的字节表示数组,因此我们需要从original的{​​{1}}矩阵。这里的sourcePixels具有整个图像字节长度的长度(大小:w * h *通道),因此它将立即获取整个图像字节值。

然后这对我来说并不直观。 sourcePixels似乎将值从System.arraycopy()复制到sourcePixels,但是实际返回的是targetPixels。我可以从代码中猜出imagetargetPixels的关系,但是我看不到如何将值从image复制到sourcePixels,但实际上会影响targetPixels

这是演示代码。谢谢!

image

1 个答案:

答案 0 :(得分:1)

每个BufferedImage都由字节数组支持,就像OpenCV中的Mat类一样,对((DataBufferByte) image.getRaster().getDataBuffer()).getData();的调用返回此基础字节数组并将其分配给targetPixels,换句话说,targetPixels指向BufferedImage image当前正在环绕的这个基础字节数组,因此,当您调用System.arraycopy时,实际上是从源字节数组进行复制放入BufferedImage的字节数组中,这就是返回image的原因,因为在那一点上,image封装的基础字节数组包含来自original的像素数据,就像这个小例子,在使b指向a之后,对b的修改也将反映在a中,就像tagetPixels一样,因为它指向字节数组image正在封装,从sourcePixels复制到targetPixels也会改变image

int[] a = new int[1];
int[] b = a;
// Because b references the same array that a does
// Modifying b will actually change the array a is pointing to
b[0] = 1;
System.out.println(a[0] == 1);