2012-01-04 9 views
18

私は画像内で支配的な色を見つけたい、 どうすればいいですか?画像内のドミナントカラーを見つけるには?

私はHEXコードでこれを取得することができれば、それは素晴らしいことだ(EXM:#eeeeee)

+0

なぜPHPですか?それはWebアプリケーションの一部ですか? – Alnitak

+1

と "dominant"を定義してください - 画像全体の平均、または最も一般的な特定のRGBトリプルを見つけることができます。 – Alnitak

+0

イメージ内の支配的な色は、主観的でイメージの視聴者によって異なります。赤色に見えない人は、例えばその色の支配的な色を決して見つけられません。 – hakre

答えて

16

画像内で最も支配的な色、つまり画像で最も一般的な色を見つけるには、画像のヒストグラムを作成する必要があります。

ここにコードはarticle on how to create a histogram in PHPです。 $maxはあなたの最も「支配」の色である例では

<?php 
$source_file = "test_image.jpg"; 

// histogram options 

$maxheight = 300; 
$barwidth = 2; 

$im = ImageCreateFromJpeg($source_file); 

$imgw = imagesx($im); 
$imgh = imagesy($im); 

// n = total number or pixels 

$n = $imgw*$imgh; 

$histo = array(); 

for ($i=0; $i<$imgw; $i++) 
{ 
     for ($j=0; $j<$imgh; $j++) 
     { 

       // get the rgb value for current pixel 

       $rgb = ImageColorAt($im, $i, $j); 

       // extract each value for r, g, b 

       $r = ($rgb >> 16) & 0xFF; 
       $g = ($rgb >> 8) & 0xFF; 
       $b = $rgb & 0xFF; 

       // get the Value from the RGB value 

       $V = round(($r + $g + $b)/3); 

       // add the point to the histogram 

       $histo[$V] += $V/$n; 

     } 
} 

// find the maximum in the histogram in order to display a normated graph 

$max = 0; 
for ($i=0; $i<255; $i++) 
{ 
     if ($histo[$i] > $max) 
     { 
       $max = $histo[$i]; 
     } 
} 

echo "<div style='width: ".(256*$barwidth)."px; border: 1px solid'>"; 
for ($i=0; $i<255; $i++) 
{ 
     $val += $histo[$i]; 

     $h = ($histo[$i]/$max)*$maxheight; 

     echo "<img src=\"img.gif\" width=\"".$barwidth."\" 
height=\"".$h."\" border=\"0\">"; 
} 
echo "</div>"; 
?> 

を(ウェブサイトがラインオフを複数回行ってきました)。

+0

このリンクは現在アダルトのウェブサイトに行く: –

+6

@RichBradshaw aw man。f'ing internet。 – tkone

+0

リンクされたコードは基本的に画像を白黒に変換し、色を区別しないことに注意してください。つまり、完全に青い画像は赤色と同じヒストグラムになります主な色を得るには、まずRGB値をHSLに変換し、色相値のヒストグラムを作成することをお勧めします。 –

3

color extract名付け、これを扱う開発したPHPクラスは、あります。しかし、サーバー側でこれを行うには、相当なシステムリソースが必要になることがわかります。代わりにcanvasでこれを行うことができます。

+0

JavScriptでこれを行う場合は、すでに回答済みですそれは正確に答えているスタックオーバーフローの質問:http://stackoverflow.com/questions/8329765/determine-if-image-is-grayscale-or-color-using-javascript/8329821#8329821 – tkone

2

書くと楽しいコードが好きです。私はすべてのピクセルを通過し、それぞれに陰影を追加する関数を後で作りました。何あなたができることは次のとおりです。

各画素ごとに、最高の色(R、G、またはb)を見つけると数学($ colorG ++または何かを)やる

終わりに、1が何であるかを見つけます最大で、最高のrgbシェードがあります。

は、私はあなたが結果のRGB値を使用した場合の色が出て来るのだろうか...

3

はこれを試してみてください:http://www.coolphptools.com/color_extract

Image Color Extract PHPクラスは、最も一般的な色(パーセンテージ)を画像ファイルから取り込みます。色の値は16進数です。

2

tkoneの回答に関しては、$ maxはイメージの色の濃度を示す単なるパラメータです。私は、六角色を返すために、コードを少し変更されます。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Empty Document</title> 
</head> 

<body> 
<?php 

error_reporting(0); 
function rgb2hex($rgb) { 
    $hex = "#"; 
    $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT); 
    $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT); 
    $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT); 

    return $hex; // returns the hex value including the number sign (#) 
} 


$source_file = "image.jpg"; 

// histogram options 

$maxheight = 300; 
$barwidth = 2; 

$im = ImageCreateFromJpeg($source_file); 

$imgw = imagesx($im); 
$imgh = imagesy($im); 

// n = total number or pixels 

$n = $imgw*$imgh; 

$histo = array(); 

for ($i=0; $i<$imgw; $i++) 
{ 
     for ($j=0; $j<$imgh; $j++) 
     { 

       // get the rgb value for current pixel 

       $rgb = ImageColorAt($im, $i, $j); 
       //echo $rgb."<br>"; 
       // extract each value for r, g, b 

       $r = ($rgb >> 16) & 0xFF; 
       $g = ($rgb >> 8) & 0xFF; 
       $b = $rgb & 0xFF; 

       // get the Value from the RGB value 

       $V = round(($r + $g + $b)/3); 
       //echo $V."<br>"; 
       // add the point to the histogram 

       $histo[$V] += $V/$n; 
       $histo_color[$V] = rgb2hex([$r,$g,$b]); 

     } 
} 

// find the maximum in the histogram in order to display a normated graph 

$max = 0; 
for ($i=0; $i<255; $i++) 
{ 
     if ($histo[$i] > $max) 
     { 
       $max = $histo[$i]; 
     } 
} 

echo "<div style='width: ".(256*$barwidth)."px; border: 1px solid'>"; 
for ($i=0; $i<255; $i++) 
{ 
     $val += $histo[$i]; 

     $h = ($histo[$i]/$max)*$maxheight; 

     echo "<img src=\"img.gif\" width=\"".$barwidth."\" 
height=\"".$h."\" border=\"0\">"; 
} 
echo "</div>"; 

$key = array_search ($max, $histo); 
$col = $histo_color[$key]; 
?> 

<p style="min-width:100px; min-height:100px; background-color:<?php echo $col?>;"></p> 
<img src="<?php echo $source_file?>"> 
</body> 
</html> 

も、私は、これは「支配的な色」と考えることができない画像でちょうど最も繰り返さ色であると言うべきです。

関連する問題