【发布时间】:2020-06-20 14:24:05
【问题描述】:
我知道有很多类似的问题,但似乎没有一个能解决我的问题。我想读取图像的像素值并将它们存储在双精度数组中。由于图像只有灰度,我将 RGB 值转换为灰度值。我还将值的范围从0-255 更改为0-1。
这是我的代码:
public static double[] getValues(String path) {
BufferedImage image;
try {
image = ImageIO.read(new File(path));
int width = image.getWidth();
int height = image.getHeight();
double[] values = new double[width * height];
int index = 0;
for(int x = 0; x < width; x++) {
for(int y = 0; y < height; y++) {
Color color = new Color(image.getRGB(y, x));
int gray = (color.getRed() + color.getGreen() + color.getBlue()) / 3;
values[index++] = gray / 255d;
}
}
return values;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
但是,当我使用它将黑色图像转换为双精度值时,我只希望 0.0 作为数组中的值。但我得到的看起来有点像以下:
[0.058823529411764705, 0.058823529411764705, 0.058823529411764705, ...]
你能告诉我我做错了什么吗?谢谢。
【问题讨论】:
标签: java image colors rgb grayscale