ヒストグラム(histogram)

ヒストグラムとは?
-> 統計で度数分布を示すグラフの一つ。横軸に階級、縦軸に度数をとり、各階級の度数を長方形の柱で示す。

define('cherry', 'cherry.jpg');

$img = imagecreatefromjpeg(cherry);
$imagex = imagesx($img);
$imagey = imagesy($img);

$histogramR = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
$histogramG = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
$histogramB = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

for($y = 0; $y < $imagey; $y++){
	for($x = 0; $x < $imagex; $x++){
		$rgb = imagecolorat($img, $x, $y);
		$r = ($rgb >> 16) & 0xFF;
		$g = ($rgb >> 8) & 0xFF;
		$b = $rgb & 0xFF;
		$histogramR[(int)($r/16)]++;
		$histogramG[(int)($g/16)]++;
		$histogramB[(int)($b/16)]++;
	}
}

function writeHistogram($value, $char){
	echo $value;
	for($i=0; $i<$value; $i++){
		echo $char;
	}
}

echo 'Red <br>';
foreach($histogramR as $countR){
	writeHistogram($countR/100, 'r');
	echo '<br>';
}

Red
53.32rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
41.12rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
446.61rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr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rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
65.12rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
56.57rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
46.29rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
39.89rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr
48.98rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr

OpenCVでは一次元ヒストグラムでは横軸が明度、縦軸が明度のピクセル数
二次元ヒストグラムでは、横軸が色相、縦軸が彩度、グラフの各点の明るさがピクセル数の数を示す。
cvCreateHist関数などを用いる
ヒストグラムを利用して画像同士の類似度を計算する

なるほど、写真からカテゴライズする時などはヒストグラムを使うのか。
顔認証にもヒストグラムは使われているのでしょうか。。。