par
Invité » 08 févr. 2006, 18:53
Bonjour,
Voici ma methode complete pour ceux que ca interessent.
Elle realise le calcul de couleur dominante a partir d'un palette, dit si l'image est en couleur, noir et blanc ou niveaux de gris.
Désolé je n'est pas encore eu trop le temps de la commenté ni vraiment de la tester !
private function setCouleurs()
{
$netb = true;
$niveaugris = true;
$palette = Array([0]=>Array(0,0,0), //noir
[1]=>Array(128,128,128), //gris
[2]=>Array(128,0,0), //marron
[3]=>Array(255,0,0), //rouge
[4]=>Array(0,128,0), //vertfoncé
[5]=>Array(0,255,0), //vert
[6]=>Array(128,128,0), //olive
[7]=>Array(255,255,0), //jaune
[8]=>Array(0,0,128), //marine
[9]=>Array(0,0,255), //bleu
[10]=>Array(128,0,128), //violet
[11]=>Array(255,0,255), //fuchsia
[12]=>Array(0,128,128), //cobalt
[13]=>Array(0,255,255), //aqua
[14]=>Array(192,192,192),//argent
[15]=>Array(255,255,255),//blanc
[16]=>Array(255,128,0) //orange
);
for ($i=1 ; $i < ImageSX($this->miniatureNonReechantillonnee) ; $i++) {
for ($j=1 ; $j < ImageSX($this->miniatureNonReechantillonnee) ; $j++) {
$pixel = ImageColorAt($miniatureNonReechantillonnee,$i,$j);
if ($pixel != 0 || $pixel != 255255255) $netb = false;
$r = ($pixel >> 16) & 0xFF;
$v = ($pixel >> 8) & 0xFF;
$b = $pixel & 0xFF;
if ($r != $v || $v != $b || $r != $b) $niveaugris = false;
$distMin = 1000;
for ($k=0 ; $k < count($palette) && $disMin != 0 ; $k++) {
$distance = pow($palette[$k][0] - $r,2) + pow($palette[$k][1] - $v,2) + pow($palette[$k][2] - $b,2);
if ($distance < $distMin){$disMin = $distance;$indCoulProche = $k;}
}
$paletteDom[$indCoulProche]++;
}
}
$max = 0;
foreach ($paletteDom as $key => $val)if ($val > $max)$this->couleurDominante = $key);
$this->couleurDominante = $this->couleurDominante[0].$this->couleurDominante[1].$this->couleurDominante[2];
if ($netb)$this->coloration = 1;
else if ($niveaugris)$this->coloration = 2;
else $this->coloration = 3;
}
Bonjour,
Voici ma methode complete pour ceux que ca interessent.
Elle realise le calcul de couleur dominante a partir d'un palette, dit si l'image est en couleur, noir et blanc ou niveaux de gris.
Désolé je n'est pas encore eu trop le temps de la commenté ni vraiment de la tester !
[php]private function setCouleurs()
{
$netb = true;
$niveaugris = true;
$palette = Array([0]=>Array(0,0,0), //noir
[1]=>Array(128,128,128), //gris
[2]=>Array(128,0,0), //marron
[3]=>Array(255,0,0), //rouge
[4]=>Array(0,128,0), //vertfoncé
[5]=>Array(0,255,0), //vert
[6]=>Array(128,128,0), //olive
[7]=>Array(255,255,0), //jaune
[8]=>Array(0,0,128), //marine
[9]=>Array(0,0,255), //bleu
[10]=>Array(128,0,128), //violet
[11]=>Array(255,0,255), //fuchsia
[12]=>Array(0,128,128), //cobalt
[13]=>Array(0,255,255), //aqua
[14]=>Array(192,192,192),//argent
[15]=>Array(255,255,255),//blanc
[16]=>Array(255,128,0) //orange
);
for ($i=1 ; $i < ImageSX($this->miniatureNonReechantillonnee) ; $i++) {
for ($j=1 ; $j < ImageSX($this->miniatureNonReechantillonnee) ; $j++) {
$pixel = ImageColorAt($miniatureNonReechantillonnee,$i,$j);
if ($pixel != 0 || $pixel != 255255255) $netb = false;
$r = ($pixel >> 16) & 0xFF;
$v = ($pixel >> 8) & 0xFF;
$b = $pixel & 0xFF;
if ($r != $v || $v != $b || $r != $b) $niveaugris = false;
$distMin = 1000;
for ($k=0 ; $k < count($palette) && $disMin != 0 ; $k++) {
$distance = pow($palette[$k][0] - $r,2) + pow($palette[$k][1] - $v,2) + pow($palette[$k][2] - $b,2);
if ($distance < $distMin){$disMin = $distance;$indCoulProche = $k;}
}
$paletteDom[$indCoulProche]++;
}
}
$max = 0;
foreach ($paletteDom as $key => $val)if ($val > $max)$this->couleurDominante = $key);
$this->couleurDominante = $this->couleurDominante[0].$this->couleurDominante[1].$this->couleurDominante[2];
if ($netb)$this->coloration = 1;
else if ($niveaugris)$this->coloration = 2;
else $this->coloration = 3;
}[/php]