我是JAVA OpenCV的新用户,我只是在通过今天的官方教程学习如何将Mat
对象转换为BufferedImage
。
从演示代码中,我可以了解到输入图像源是矩阵形式,然后sourcePixels
似乎是图像的字节表示数组,因此我们需要从original
的{{1}}矩阵。这里的sourcePixels
具有整个图像字节长度的长度(大小:w * h *通道),因此它将立即获取整个图像字节值。
然后这对我来说并不直观。 sourcePixels
似乎将值从System.arraycopy()
复制到sourcePixels
,但是实际返回的是targetPixels
。我可以从代码中猜出image
与targetPixels
的关系,但是我看不到如何将值从image
复制到sourcePixels
,但实际上会影响targetPixels
?
这是演示代码。谢谢!
image
答案 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);