2017-10-20 12 views
0

Je souhaite mesurer le pourcentage de couleur rouge, vert et bleu d'une image. Je peux obtenir les valeurs RVB de chaque pixel à travers getRGB() mais je ne peux pas séparer R, G, B chacun. Mon code renvoie la valeur comme rgb 167 163 164. Comment puis-je mesurer chacun séparément?Mesure le% RVB d'une image

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

Répondre

0

Vous avez juste besoin de le calculer sur votre pour après rgb = getPixelData (

rgb [0] est rouge, rgb [1] est VERT abd rgb [2] est bleu

Le les valeurs vont de 0 à 254