2017-10-20 68 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