【发布时间】:2019-08-16 17:08:05
【问题描述】:
我尝试遍历图像并从所有像素中获取每个 RGB 颜色值并对其进行处理。但我为所有像素得到相同的 RGB 值。所以显然这是错误的。
我在 Java awt 中使用了 bufferedimage 对象的 getRGB(x,y) 方法。 有人知道这里有什么问题吗?
编辑:
我遇到了问题,将图像转换为缓冲图像时出现了一些错误。我没有在缓冲图像中绘制图像。 以下代码现在按预期工作。
public void printImgDetails(Image img) {
// get the sizes of the image
long heigth = img.getHeight(null);
long width = img.getWidth(null);
// hashSet to hold all brightness values
HashSet<Float> set = new HashSet<Float>(0);
BufferedImage bimage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_BYTE_GRAY);
int rgb;
float[] hsv = new float[3];
// Draw the image on to the buffered image
Graphics2D bGr = bimage.createGraphics();
bGr.drawImage(img, 0, 0, null);
bGr.dispose();
for (int i = 0; i < width; i++) {
for (int j = 0; j < heigth; j++) {
Color c = new Color(bimage.getRGB(j, i));
int r = c.getRed();
int g = c.getGreen();
int b = c.getBlue();
Color.RGBtoHSB(r, g, b, hsv);
System.out.println("r: " + r + " g: " + g + " b: " + b);
set.add(hsv[2]);
}
}
// calculate the average brightness
double sum = 0;
for (float x : set) {
sum += x;
}
double avg = sum / set.size();
// print the results
System.out.println("avg --> " + avg);
}
提前致谢。
【问题讨论】:
-
猜测:问题出在您的代码中……就在那里,看到了吗?说真的:如果您甚至不显示任何代码,我们应该如何提供帮助?你有没有用调试器单步调试你的代码?
-
请阅读How to Ask
-
首先感谢您的回答,这是代码和带有调试器的 corse Id 步骤槽。但现在它正在工作
标签: java image-processing awt rgb