简体   繁体   English

将值从0转换为1到灰度图像(Java)

[英]Convert values from 0 to 1 to a grayscale image (Java)

I have two dimensional matrix which stores values between 0 to 1. I want to plot these values as levels of gray scale. 我有一个二维矩阵,用于存储0到1之间的值。我想将这些值绘制为灰度级别。

If the value is 1, it should be drawn as white. 如果值为1,则应绘制为白色。 If the value is 0, it should be drawn as black. 如果值为0,则应绘制为黑色。

How would I do that in java? 我将如何在Java中做到这一点?

I tried the classes: Color and BufferedImage , but I could not figure it out. 我尝试了以下类: ColorBufferedImage ,但无法弄清楚。

To create an image and set the pixels: 要创建图像并设置像素:

final BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

for (int y = 0; y < height; y++)
{
  for (int x = 0; x < width; x++)
  {
    image.setRGB(x, y, color);
  }
}

color is an int, in this case, in ARGB format (top byte is alpha, then red byte, green byte, blue byte). color是int,在这种情况下,为ARGB格式(顶部字节为alpha,然后为红色字节,绿色字节,蓝色字节)。 Since you're doing greyscale, you want R, G and B to be the same value. 由于您要进行灰度处理,因此您希望R,G和B为相同的值。 You don't want alpha, so you should set that top byte to 0xFF. 您不希望使用Alpha,因此应将最高字节设置为0xFF。

See: BufferedImage.setRGB() 请参阅: BufferedImage.setRGB()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM