Java中的AutoScale范围16位灰度图像



我有一个 12-bit grayscale图像。我想用BufferedImage在Java中显示它,并且发现BufferedImage.TYPE_USHORT_GRAY是最合适的。但是,它使我的显示图像几乎是黑色的(我的像素在0〜4095范围内(。

如何自动清楚地显示它?

非常感谢。

如果将数据存储为16位样本(如注释中所示(,则可以:

  • 将样本(16(乘以整个16位范围,并创建BufferedImage.TYPE_USHORT_GRAY图像。直截了当,但需要按摩和复制数据。

  • 或,您还可以使用12位ColorModel创建一个自定义BufferedImage,并且只能按原样使用数据。可能更快,但更多的详细/复杂代码。

这是使用12位灰色模型创建图像的代码:

WritableRaster raster = Raster.createInterleavedRaster(new DataBufferUShort(twelveBitData, twelveBitData.length), w, h, w, 1, new int[]{0}, null);
ColorSpace gray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel twelveBitModel = new ComponentColorModel(gray, new int[]{12}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
BufferedImage image = new BufferedImage(twelveBitModel, raster, twelveBitModel.isAlphaPremultiplied(), null);

完整的,可运行的演示程序。

另外,如果您的数据被存储为"一个半字节"包装了12位样本,则最简单的解决方案是首先将数据粘贴到完整的16位样本中。


演示程序:

public class TwelveBit {
    public static void main(String[] args) {
        int w = 320;
        int h = 200;
        short[] twelveBitData = new short[w * h];
        createRandomData(twelveBitData);
        WritableRaster raster = Raster.createInterleavedRaster(new DataBufferUShort(twelveBitData, twelveBitData.length), w, h, w, 1, new int[]{0}, null);
        ColorSpace gray = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorModel twelveBitModel = new ComponentColorModel(gray, new int[]{12}, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
        BufferedImage image = new BufferedImage(twelveBitModel, raster, twelveBitModel.isAlphaPremultiplied(), null);
        showIt(image);
    }
    private static void createRandomData(short[] twelveBitData) {
        Random random = new Random();
        for (int i = 0; i < twelveBitData.length; i++) {
            twelveBitData[i] = (short) random.nextInt(1 << 12);
        }
    }
    private static void showIt(final BufferedImage image) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("test");
                frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                frame.add(new JLabel(new ImageIcon(image)));
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

您需要从12位扩展到16位。将您的值乘以16

最新更新