回答
要找到图像中最主要的颜色,即图像中最流行的颜色:您需要创建图像的直方图。
这里是这个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
是你最“主导”的色彩。
@RichBradshaw aw man。f'ing internet – tkone 2013-09-14 17:57:57
请注意,链接的代码基本上将图像转换为b/w,并且不区分颜色,即完全蓝色的图像将导致与红色相同的直方图为了获得主色,我建议首先将RGB值转换为HSL,并创建色调值的直方图。 – 2014-08-05 16:41:14
有开发的PHP类来处理这个名为color extract。但是,知道在服务器端执行此操作将需要大量的系统资源。您可能希望改为使用canvas来做到这一点。
如果你想在JavScript中做到这一点,这里已经回答了堆栈溢出问题,它完全回答:http://stackoverflow.com/questions/8329765/determine-if-image-is-grayscale-or-color-using-javascript/8329821#8329821 – tkone 2012-01-04 17:03:20
你应该看看GD and Image Functions。
听起来像一个愉快的代码写!我在一段时间内做了一个函数,通过每个像素并为每个像素添加阴影。你可以做的是:
每个像素,找到最高的颜色(R,G,或b)和做数学题($ colorG ++或东西)在结束
,找出一个是什么最大的,并且会有你最高的红色阴影。
我不知道什么颜色会出来,如果你使用的结果RGB值为...
试试这个:http://www.coolphptools.com/color_extract。
图像颜色提取PHP类从图像文件中提取最常见的颜色(百分比)。颜色值是十六进制的。
关于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>
此外,我应该说这是刚才的那不能算是“主色”的形象最重复的颜色。
虽然它在JavaScript中,但color-thief是最好的获取图像的主色调。
- 1. 获取图像的主色调
- 2. 查找Android中可支配图像的主色颜色
- 3. Matlab:如何找到彩色图像中黑色像素的数量?
- 4. 如何在JPanel上找到主色?
- 5. 缩小图像如何影响主色调?
- 6. Android:如何更改图像的色调?
- 7. 如何找出图像中的“特定像素的颜色”?
- 8. 如何将图像分成块并找到像素颜色
- 9. 如何更改图像中红色的默认色调?
- 10. 如何查找图像中特定颜色的像素数量?
- 11. 在OpenCV中找到相机框架中的主色调Android
- 12. 如何找到图像的中心?
- 13. 如何找到使用PHP的图像的“多数颜色”?
- 14. 如何将非调色板透明图像转换为调色板图像?
- 15. 如何在运动图像中找到JPanel内给定点的像素颜色?
- 16. 如何从捕获图像中找出红色色素?
- 17. 图像中的主要颜色
- 18. 找到主题的深色/浅色
- 19. 如何找到最合适的X颜色来表示图像?
- 20. 如何从彩色图像动画到灰色图像?
- 21. 如何像扫描图像一样调整彩色图像
- 22. 如何在OpenCV中过滤带有邻域主色的图像
- 23. 如何找到该图像或多或少同色w.r.t颜色(色相)?
- 24. Javascript图像主簇颜色
- 25. 如何找到图像中子图像3x3的标准偏差
- 26. 如何在图像中找到网址
- 27. 如何将主图像下的缩略图移动到主图像的右侧?
- 28. 色调不变的图像
- 29. 改变图像的色调
- 30. 如何在swift中找到UIImage中单个像素的颜色
为什么PHP?它是一个Web应用程序的一部分? – Alnitak 2012-01-04 16:56:19
并请定义“主导” - 它可以是整个图像的平均值,也可以是找到的最常见的特定RGB三元组。 – Alnitak 2012-01-04 16:57:30
图像中的主色是什么,是主观的,取决于图像的观看者。因此,没有看到红色的人不会发现这种颜色占主导地位。 – hakre 2012-01-04 16:57:34