2017-10-20 89 views
0

我想测量图像的红色,绿色和蓝色比例。我可以通过getRGB()获取每个像素的RGB值,但无法分别将R,G,B分开。我的代码返回值如rgb 167 163 164。我怎样才能分别测量每一个?测量图像的RGB%

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; 
    } 

回答

0

你只需要计算它在你的后RGB = getPixelData(

RGB [0]为红色,RGB [1]是GREEN ABD RGB [2]是蓝色

的取值范围为0到254