问题了解OpenCV将Mat转换为BufferedImage

问题描述 投票:0回答:1

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

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

然后就是这个对我来说不直观。 System.arraycopy()似乎将值从sourcePixels复制到targetPixels,但是什么行动回来的是image。我可以从代码中猜测targetPixelsimage有关系,但我不知道我们如何将值从sourcePixels复制到targetPixels,但它实际上影响了image的值?

这是演示代码。谢谢!

private static BufferedImage matToBufferedImage(Mat original)
{
    BufferedImage image = null;
    int width = original.width(), height = original.height(), channels = original.channels();
    byte[] sourcePixels = new byte[width * height * channels];

    original.get(0, 0, sourcePixels);

    if (original.channels() > 1)
    {
        image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
    }
    else
    {
        image = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
    }

    final byte[] targetPixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
    System.arraycopy(sourcePixels, 0, targetPixels, 0, sourcePixels.length);

    return image;
}
opencv bufferedimage
1个回答
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);
© www.soinside.com 2019 - 2024. All rights reserved.