2012-01-04 17 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> 

此外,我应该说这是刚才的那不能算是“主色”的形象最重复的颜色。

相关问题