测量图像的RGB%

时间:2017-10-20 17:21:35

标签: java image rgb

我想测量图像的红色,绿色和蓝色百分比。我可以通过getRGB()获取每个像素的RGB值,但不能分别对R,G,B进行分离。我的代码返回的值如rgb 167 163 164。我该如何分别测量每一个?

public class RGB {
    public static final String IMG = "/home/aritra/workspace/oopencv/picture/Pic652.jpg";

    public static void main(String[] args) {

        BufferedImage img;

        try {
            img = ImageIO.read(new File(IMG));

            int[][] pixelData = new int[img.getHeight() * img.getWidth()][3];
            int[] rgb;

            int counter = 0;

            for(int i = 0; i < img.getWidth(); i++){
                for(int j = 0; j < img.getHeight(); j++){
                    rgb = getPixelData(img, i, j);

                }  
                //System.out.println(counter);
            }


        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private static int[] getPixelData(BufferedImage img, int x, int y) {
    int argb = img.getRGB(x, y);
    int  red[]={0};
    int rgb[] = new int[] {
        (argb >> 16) & 0xff, //red
        (argb >>  8) & 0xff, //green
        (argb      ) & 0xff  //blue
    };

    System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);

    return rgb;
    }

1 个答案:

答案 0 :(得分:0)

你只需要在rgb = getPixelData(

)之后计算它

rgb [0]为RED,rgb [1]为GREEN abd rgb [2]为蓝色

值范围为0到254