2012-01-04 51 views
18

我想找到圖像中的主色調, 我該怎麼做?如何找到圖像中的主色調?

這將是巨大的,如果我能以十六進制代碼得到這個(EXM:#eeeeee)

+0

爲什麼PHP?它是一個Web應用程序的一部分? – Alnitak 2012-01-04 16:56:19

+1

並請定義「主導」 - 它可以是整個圖像的平均值,也可以是找到的最常見的特定RGB三元組。 – Alnitak 2012-01-04 16:57:30

+0

圖像中的主色是什麼,是主觀的,取決於圖像的觀看者。因此,沒有看到紅色的人不會發現這種顏色占主導地位。 – hakre 2012-01-04 16:57:34

回答

16

要找到圖像中最主要的顏色,即圖像中最流行的顏色:您需要創建圖像的直方圖。

這裏是這個article on how to create a histogram in PHP的代碼。 (網站已經下線幾次)

<?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>"; 
?> 

在該示例$max是你最「主導」的色彩。

+6

@RichBradshaw aw man。f'ing internet – tkone 2013-09-14 17:57:57

+0

請注意,鏈接的代碼基本上將圖像轉換爲b/w,並且不區分顏色,即完全藍色的圖像將導致與紅色相同的直方圖爲了獲得主色,我建議首先將RGB值轉換爲HSL,並創建色調值的直方圖。 – 2014-08-05 16:41:14

3

有開發的PHP類來處理這個名爲color extract。但是,知道在服務器端執行此操作將需要大量的系統資源。您可能希望改爲使用canvas來做到這一點。

+0

如果你想在JavScript中做到這一點,這裏已經回答了堆棧溢出問題,它完全回答:http://stackoverflow.com/questions/8329765/determine-if-image-is-grayscale-or-color-using-javascript/8329821#8329821 – tkone 2012-01-04 17:03:20

2

聽起來像一個愉快的代碼寫!我在一段時間內做了一個函數,通過每個像素併爲每個像素添加陰影。你可以做的是:

每個像素,找到最高的顏色(R,G,或b)和做數學題($ colorG ++或東西)在結束

,找出一個是什麼最大的,並且會有你最高的紅色陰影。

我不知道什麼顏色會出來,如果你使用的結果RGB值爲...

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> 

此外,我應該說這是剛纔的那不能算是「主色」的形象最重複的顏色。

相關問題