答えて
画像内で最も支配的な色、つまり画像で最も一般的な色を見つけるには、画像のヒストグラムを作成する必要があります。
ここにコードは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>";
?>
を(ウェブサイトがラインオフを複数回行ってきました)。
このリンクは現在アダルトのウェブサイトに行く: –
@RichBradshaw aw man。f'ing internet。 – tkone
リンクされたコードは基本的に画像を白黒に変換し、色を区別しないことに注意してください。つまり、完全に青い画像は赤色と同じヒストグラムになります主な色を得るには、まずRGB値をHSLに変換し、色相値のヒストグラムを作成することをお勧めします。 –
color extract名付け、これを扱う開発したPHPクラスは、あります。しかし、サーバー側でこれを行うには、相当なシステムリソースが必要になることがわかります。代わりにcanvasでこれを行うことができます。
JavScriptでこれを行う場合は、すでに回答済みですそれは正確に答えているスタックオーバーフローの質問:http://stackoverflow.com/questions/8329765/determine-if-image-is-grayscale-or-color-using-javascript/8329821#8329821 – tkone
GD and Image Functionsをご覧ください。
PHPで画像から色情報を抽出することについては、questionと同様ですが、これはgithubのclassにリンクしています。
書くと楽しいコードが好きです。私はすべてのピクセルを通過し、それぞれに陰影を追加する関数を後で作りました。何あなたができることは次のとおりです。
各画素ごとに、最高の色(R、G、またはb)を見つけると数学($ colorG ++または何かを)やる
終わりに、1が何であるかを見つけます最大で、最高のrgbシェードがあります。
は、私はあなたが結果のRGB値を使用した場合の色が出て来るのだろうか...
はこれを試してみてください:http://www.coolphptools.com/color_extract。
Image Color Extract PHPクラスは、最も一般的な色(パーセンテージ)を画像ファイルから取り込みます。色の値は16進数です。
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>
も、私は、これは「支配的な色」と考えることができない画像でちょうど最も繰り返さ色であると言うべきです。
でも、その画像はjavacriptではなく、color-thiefが優位な色を得るのに最適です。
- 1. 画像内のパターンを見つける
- 2. 画像内のエッジを見つける
- 3. pythonファイル内のパスを持つ画像を見つける
- 4. Numpy画像の中にサブ画像を見つける
- 5. opencv画像を他の画像に見つける
- 6. JPG画像内のテキストのバウンディングボックスを見つける
- 7. 画像内の別々の塊を見つける
- 8. 画像内の数字の座標(x、y)を見つける
- 9. 画像のURLを見つける
- 10. 画像の十字を見つける
- 11. 画像内で最大のブロブを見つける
- 12. 画像内の白い矩形を見つける
- 13. 別の画像の中の画像を見つける
- 14. 複数の画像間の差分画像を見つける
- 15. 違いを見つける - 14x14画像
- 16. 3D画像でサーフェスを見つける
- 17. リストの前に画像を見つける方法は?
- 18. Raphael JS:SVG画像のパスを見つけるには?
- 19. ステレオ画像ペアの視差マップを見つけるには
- 20. 画像内の正方形を見つけるアルゴリズムはありますか?
- 21. C#で画像パスを見つけるには?
- 22. 空の(f.i.白)スペースを画像内で高速に見つける方法
- 23. Pygameはサブディレクトリ内に画像フォルダを見つけられません
- 24. 画面上の画像を見つける
- 25. Jqueryは左上のピクセル画像の色を見つける
- 26. 2つの画像の差のバウンディングボックスを見つけるか?
- 27. 2つの画像で共通のものを見つける
- 28. Python openCV画像のフォルダ内に画像を見つける(道路標識認識)
- 29. Pythonで同様のカラーパレットを持つ画像を見つける
- 30. Laravel 5.2で画像の場所を見つける方法は?
なぜPHPですか?それはWebアプリケーションの一部ですか? – Alnitak
と "dominant"を定義してください - 画像全体の平均、または最も一般的な特定のRGBトリプルを見つけることができます。 – Alnitak
イメージ内の支配的な色は、主観的でイメージの視聴者によって異なります。赤色に見えない人は、例えばその色の支配的な色を決して見つけられません。 – hakre