回答
要找到圖像中最主要的顏色,即圖像中最流行的顏色:您需要創建圖像的直方圖。
這裏是這個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