我试图浏览图像并从所有像素中获取每个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);
}
提前谢谢。
当我在编辑中徘徊时,在图像和缓冲图像之间转换时出现了问题。我忘了将图像绘制到缓冲百科中。就是这样。
如果每个像素都获得相同的值,则有几个可能的原因。
a( 您的图像在每个像素中都具有相同的值
b( 您不会在呼叫getRGB
之间更改 x 和 y
c( 您读取了其他内容,但返回值为 getRGB