每个文件的增量

Increment per File

下面是我的一些代码。我正在浏览一个文件夹,提取每张图像,找到每个像素的 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"

我该如何解决这个问题?谢谢。

如果您希望 count 仅代表单个文件中的蓝色,则在遍历像素之前将其值重置为 0:

int width = image.getWidth();
int height = image.getHeight();
count = 0;
...