在Java中将图像转换为灰度

时间:2011-02-28 10:12:16

标签: java image-processing

我想显示灰度图像(每像素16位)。到目前为止,我有这个:

DataInputStream aFile = new DataInputStream(new FileInputStream("filename.raw"));
BufferedImage aBufferedImage = new BufferedImage(2000, 2000, BufferedImage.TYPE_USHORT_GRAY);
WritableRaster aRaster = aBufferedImage.getRaster();
byte[] aRow = new byte[2000*2];
aFile.readFully(aRow, 0, 2000*2);

现在,我的问题是如何设置从aRowaBufferedImage的16位强度值?

1 个答案:

答案 0 :(得分:0)

基于http://java.itags.org/java-tech/17212/,您可以将字节数组转换为int或double数组(每个像素有一个数组单元格),然后使用WritableRaster.setSamples()WritableRaster.setPixels()。为避免自己进行字节到用户转换,可以使用DataInputStream.readUnsignedShort()