【问题标题】:display grayscale image in java在java中显示灰度图像
【发布时间】:2017-12-07 00:08:44
【问题描述】:

我在 java 中创建了 floats 的二维数组,表示灰度图像,当每个像素被标准化时 - 它在 [0,1] 之间。

如何获取二维数组并显示图像(当然是灰度)?

ty!

【问题讨论】:

标签: java image-processing grayscale


【解决方案1】:

最简单的方法是用它制作一个 BufferedImage。为此,您必须将值转换为颜色:

int toRGB(float value) {
    int part = Math.round(value * 255);
    return part * 0x10101;
}

这首先将 0-1 范围转换为 0-255 范围,然后生成所有三个通道(RGB - 红色、绿色和蓝色)具有相同值的颜色,从而形成灰色。

然后,要制作整个图像,设置所有像素值:

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int y = 0; y < height; y++)
    for (int x = 0; x < width; x++)
        image.setRGB(x, y, toRGB(theFloats[y][x]));

获得图像后,您可以将其保存到文件中:

ImageIO.save(image, 'png', new File('some/path/file.png'));

或者,以某种方式显示它,也许使用 Swing.. 参见例如 this question

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 2012-11-20
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 2017-05-07
    相关资源
    最近更新 更多