以下是我的一些代码。我正在浏览一个文件夹,拉动每个图像,找到每个像素的RGB值,并确定哪些是蓝色。我试图只增加每个文件,但由于某种原因,它继续并添加每个文件的增量。
try {
int width = image.getWidth();
int height = image.getHeight();
for(int k = 0; k < height; k++){
for(int j = 0; j < width; j++){
Color c = new Color(image.getRGB(j, k));
int redVal = c.getRed();
int greenVal = c.getGreen();
int blueVal = c.getBlue();
//24 42 72
if ((redVal >= 0) && (redVal <= 80)) {
if ((greenVal >= 40) && (greenVal <= 105)) {
if ((blueVal >= 80) && (blueVal <= 135)) {
count++;
}
}
}
}
}
System.out.print(count +" pixels are \"blue\", ");
输出:
23700 pixels are "blue"
27199 pixels are "blue"
38136 pixels are "blue"
40834 pixels are "blue"
41443 pixels are "blue"
50349 pixels are "blue"
我该如何解决这个问题?谢谢。
答案 0 :(得分:2)
如果您希望count
仅在单个文件中表示蓝色,则在迭代像素之前将其值重置为0:
int width = image.getWidth();
int height = image.getHeight();
count = 0;
...