我正在研究能够计算图像中蓝色/红色/黄色/...像素数量的东西。到目前为止,我已经将这段代码作为测试:

public class Main { 
 
    /* 
   Black: 0,0,0 
   White: 255, 255, 255 
   Red: 255, 0, 0 
   Orange: 255, 127, 0 
   Yellow: 255, 255, 0 
   Green: 0, 255, 0 
   Blue: 0, 0, 255 
   Indigo: 111, 0, 255 
   Violet: 143, 0, 255 
    */ 
 
    static int blackCount = 0; 
    static int whiteCount = 0; 
    static int redCount = 0; 
    static int orangeCount = 0; 
    static int yellowCount = 0; 
    static int greenCount = 0; 
    static int blueCount = 0; 
    static int indigoCount = 0; 
    static int violetCount = 0; 
    static int otherCount = 0; 
 
    static int totalCount = 0; 
 
    public static void main(String[] args) { 
        try { 
 
            String path = "src/colors.jpg"; 
            BufferedImage image = ImageIO.read(new File(path)); 
            int w = image.getWidth(); 
            int h = image.getHeight(); 
            for (int y = 0; y < h; y++) { 
                for (int x = 0; x < w; x++) { 
                    Color c = new Color(image.getRGB(x, y)); 
                    int red = c.getRed(); 
                    int green = c.getGreen(); 
                    int blue = c.getBlue(); 
                    countColor(red, green, blue); 
                    totalCount++; 
                } 
            } 
 
            printColors(); 
 
        } catch (Exception ex) { 
            System.out.println(ex.getMessage()); 
        } 
    } 
 
    private static void countColor(int red, int green, int blue) { 
        if (red == 0 && green == 0 && blue == 0) blackCount++; 
        else if (red == 255 && green == 255 && blue == 255) whiteCount++; 
        else if (red == 255 && green == 0 && blue == 0) redCount++; 
        else if (red == 255 && green == 127 && blue == 0) orangeCount++; 
        else if (red == 255 && green == 255 && blue == 0) yellowCount++; 
        else if (red == 0 && green == 255 && blue == 0) greenCount++; 
        else if (red == 0 && green == 0 && blue == 255) blueCount++; 
        else if (red == 111 && green == 0 && blue == 255) indigoCount++; 
        else if (red == 143 && green == 0 && blue == 255) violetCount++; 
        else otherCount++; 
    } 
 
    private static void printColors() { 
        System.out.println("Black: " + blackCount); 
        System.out.println("White: " + whiteCount); 
        System.out.println("Red: " + redCount); 
        System.out.println("Orange: " + orangeCount); 
        System.out.println("Yellow: " + yellowCount); 
        System.out.println("Green: " + greenCount); 
        System.out.println("Blue: " + blueCount); 
        System.out.println("Indigo: " + indigoCount); 
        System.out.println("Violet: " + violetCount); 
        System.out.println("Other: " + otherCount); 
        System.out.println("Total: " + totalCount); 
    } 

但是您可能会注意到问题……在 RGB 中,颜色“红色”定义为 (255, 0, 0)。因此,包含大量红色的图像可能会返回“0”,因为图像中使用的颜色是 (254, 0, 0) 而不是 (255, 0, 0)。

所以我实际上不仅要计算纯红色像素,还要计算所有“红色”像素。我假设有一种比写一个疯狂的 long if (red = 255), if (red = 254),... 结构更简单的方法来解决这个问题?

请您参考如下方法:

可以将正在处理的像素转换成更方便的颜色模型,即HSL or HSV .

那么定义属于特定组(偏红/偏蓝/等)的组件值范围可能会更简单。


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!