有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

每个文件的java增量

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

我该怎么解决这个问题?谢谢


共 (1) 个答案

  1. # 1 楼答案

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

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