【发布时间】:2018-11-20 02:19:42
【问题描述】:
我正在循环浏览具有 X 和 Y 坐标的图像。但是,我只能使用以下方法设置 sRGB 值:image.setRGB(int x, int y, int rgb);
我想为当前的 X 和 Y 坐标设置一个自定义的 RED、GREEN 和 BLUE 值。在这种情况下,新的 RGB 值是原始 RGB 值的平均值。
我得到的结果是蓝色图像而不是灰度图像。
for (int y = 0; y < imageInput.getHeight(); y++) {
for (int x = 0; x < imageInput.getWidth(); x++) {
int clr = imageInput.getRGB(x, y);
Color color = new Color(clr);
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int sumRGB = red + green + blue;
int avg = sumRGB/3;
System.out.println("sRGB: " + clr);
System.out.println("Input Image: x= " + x + " y= " + y + " [" + red + "," + green + "," + blue + "]");
System.out.println("Avg RGB: " + avg);
imageCopy.setRGB(x, y, avg);
int clrCopy = imageCopy.getRGB(x, y);
color = new Color(clrCopy);
red = color.getRed();
green = color.getGreen();
blue = color.getBlue();
System.out.println("Output Image: x= " + x + " y= " + y + " [" + red + "," + green + "," + blue + "]\n");
}
}
【问题讨论】:
-
1) 为了尽快获得更好的帮助,请发帖 minimal reproducible example 或 Short, Self Contained, Correct Example。 2) 例如,获取图像的一种方法是热链接到在this Q&A 中看到的图像。
-
要获得更好的性能(跳过颜色模型转换等),请参阅此答案的后半部分:stackoverflow.com/a/49650210/1052284