Skip to content

Instantly share code, notes, and snippets.

@hagino3000
Created September 3, 2012 05:05
Show Gist options
  • Save hagino3000/3606830 to your computer and use it in GitHub Desktop.
Save hagino3000/3606830 to your computer and use it in GitHub Desktop.
Create Histgram (without normalize)
<?php
main();
// 終了
exit;
function zero_array() {
$ret = array();
for ($i = 0; $i < 255; $i++) {
$ret[$i] = 0;
}
return $ret;
}
function main() {
$im = imagecreatefrompng("Austria.png");
$rgb = imagecolorat($im, 70, 42);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$width = imagesx($im);
$height = imagesy($im);
$hist_r = zero_array();
$hist_g = zero_array();
$hist_b = zero_array();
for ($i = 0; $i < $width; $i++) {
for ($j = 0; $j < $height; $j++) {
$rgb = imagecolorat($im, $i, $j);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8) & 0xFF;
$b = $rgb & 0xFF;
$hist_r[$r] += 1;
$hist_g[$g] += 1;
$hist_b[$b] += 1;
}
}
echo "======= size =======\n";
var_dump($width);
var_dump($height);
var_dump(json_encode(array(
r => $hist_r,
g => $hist_g,
b => $hist_b
)));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment