BufferedImage getRGB vs Raster getSample [英] BufferedImage getRGB vs Raster getSample

查看:72
本文介绍了BufferedImage getRGB vs Raster getSample的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用 Java 进行一些图像处理.我使用 ImageIO 库来读取和写入图像.我可以通过以下两种方式读取图像像素值(可能还有其他不知道的方法).

I am trying to do some image processing in Java. I used ImageIO library for reading and writing images. I can read the image pixel value in two ways as follows (there might be other methods which do not know).

  1. 使用 BufferedImage 的 getRGB 方法:

  1. Using BufferedImage's getRGB method:

pixel = image.getRGB(x,y);

pixel = image.getRGB(x,y);

使用 Raster 的 getSample 方法:

Using Raster's getSample method:

WritableRaster raster = image.getRaster();
像素 = raster.getSample(x,y,0);

WritableRaster raster = image.getRaster();
pixel = raster.getSample(x,y,0);

以上两种方式有什么区别?

What is the difference in the above two approaches?

推荐答案

1: 第一种方法将始终以 int ARGB 格式和 sRGB 颜色空间返回一个像素.无论图像的内部表示如何.这意味着除非图像的内部表示是 TYPE_INT_ARGB,否则必须进行一些转换.这有时很有用,因为它是可预测的,但通常它很慢.例如,色彩空间转换非常昂贵.此外,如果图像的精度高于每个样本 8 位和/或每个像素 4 个样本,则会发生精度损失.根据您的用例,这可能会也可能不会被接受.

1: The first approach will always return a pixel in int ARGB format, and in the sRGB color space. Regardless of the image's internal representation. This means that unless the image's internal representation is TYPE_INT_ARGB, some conversion has to be done. This is sometimes useful, because it's predictable, but just as often it's quite slow. As an example, color space conversion is quite expensive. Also, if the image has higher precision than 8 bits per sample and/or 4 samples per pixel, precision loss occurs. This may or may not be acceptable, given your use case.

2:第二种方法可能会给你一个像素值,但不是在所有情况下,因为它会为你提供波段 0(第一个波段)在 (x,y) 处的样本值.对于 TYPE_INT_ARGB,这将与像素值相同.对于 TYPE_BYTE_INDEXED 这将是在查找表中使用的索引(您需要查找它以获得像素值).对于 TYPE_3BYTE_BGR 这只会给你蓝色值(你需要将它与波段 1 和 2 中的样本结合以获得完整的像素值).等其他类型.对于内部未表示为 int 的样本,会发生数据类型转换(在极少数情况下会丢失精度).它可能对你有用,但我从来没有用过 getSample(...) 方法.

2: The second approach may give you a pixel value, but not in all cases, as it gives you the sample value at (x,y) for the the band 0 (the first band). For TYPE_INT_ARGB this will be the same as the pixel value. For TYPE_BYTE_INDEXED this will be the index to use in the look up table (you need to look it up to get the pixel value). For TYPE_3BYTE_BGR this will give you the blue value only (you need to combine it with the samples in band 1 and 2 to get the full pixel value). Etc. for other types. For samples that are not internally represented as an int, data type conversion occurs (and in rare cases precision loss). It might work for you, but I've never had much use for the getSample(...) methods.

相反,我建议您研究一下我认为是获取像素数据的最快方法.那就是使用 getDataElements 方法:

Instead I suggest you look into what I believe to be the fastest way to get at pixel data. That is using the getDataElements method:

Object pixel = null; // pixel initialized on first invocation of getDataElements

for (y) {
    for (x) {
       pixel = raster.getDataElements(x, y, pixel);
    }
}

这将为您提供来自数据缓冲区的本机"值,无需任何转换.

This will give you the "native" values from the data buffer, without any conversion.

然后,您需要对要支持的每种传输类型(请参阅 DataBuffer 类)进行特殊处理,并且可能是非标准类型的常见回退.

You then need to have special handling for each transfer type (see the DataBuffer class) you want to support, and perhaps a common fallback for non-standard types.

对于像素值与归一化 RGB 值,这将与您的方法 2 具有相同的问题",因此您可能需要手动"转换/查找.

This will have the same "problem" as your approach 2 for pixel values vs normalized RGB values, so you might need to convert/look up "manually".

什么方法更好,一如既往,取决于.您必须查看每个用例,并决定哪个更重要.轻松/简单,或最好的性能(或者也许是最好的质量?).

What approach is better, as always, depends. You have to look at each use case, and decide what's more important. Ease/simplicity, or the best possible performance (or perhaps best quality?).

这篇关于BufferedImage getRGB vs Raster getSample的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆