Bonjour,

Je cherche à connaître la couleur la plus présente dans une image.

En parcourant chaque pixel de mon image j'aimerais obtenir les valeurs du pixel (R,G,B) qui est en plus grand nombre dans l'image.
Voici ce que j'ai fait mais le code ne s'arrête jamais. (enfin j'ai attendu 6 min maximum et ça chargeait)
Je pense que ma quadruple boucle est trop longue.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
 
public static void Main_Color(String path_Image) throws IOException{
        int cpt=0;
        listcpt.add(cpt);
        System.out.println(listcpt);
        imgglobal = ImageIO.read(new File("C:\\Users\\guill\\"+path_Image));
        for(int a =0; a< imgglobal.getWidth();a++){
            for(int b =0; b< imgglobal.getHeight(); b++){
                int p = imgglobal.getRGB(a, b);
                int r = (p>>16)&0xff;
                int g = (p>>8)&0xff;
                int bl = p&0xff;
                listpx.add(r);
                listpx.add(g);
                listpx.add(bl);
                System.out.println(listpx);
 
                for(int i =0; i< imgglobal.getWidth();i++){
                    for(int j =0; j< imgglobal.getHeight(); j++){
                        p = imgglobal.getRGB(i, j);
                        r = (p>>16)&0xff;
                        g = (p>>8)&0xff;
                        bl = p&0xff;
                        System.out.println("ok");
 
                        if(r==listpx.get(0) && g==listpx.get(1) && bl==listpx.get(2)){
                            cpt++;
                            System.out.println("+1");
                        }
                    }
                }
                if(cpt > listcpt.get(0)){
                    listcpt.clear();
                    listcpt.add(cpt);
                    listpxSave = listpx;
                }
                cpt = 0;
                listpx.clear();
            }
        }
        System.out.println("The main color is : " + listpxSave);
        System.out.println("This color appears " + listcpt.get(0)+ " times");
 
    }
Je prends le premier pixel et je le compare a tous les pixels y compris à lui même. Une fois terminé je prends le deuxième pixel et je le compare à tous les pixels y compris au premier et à lui même.

Avez vous une idée ?
Merci beaucoup