PHP Conference Japan 2024

Imagick::getImageHistogram

(PECL imagick 2, PECL imagick 3)

Imagick::getImageHistogram取得影像直方圖

說明

public Imagick::getImageHistogram(): 陣列

以 ImagickPixel 物件陣列的形式返回影像直方圖。

參數

此函式沒有參數。

回傳值

以 ImagickPixel 物件陣列的形式返回影像直方圖。

錯誤/例外

發生錯誤時拋出 ImagickException。

範例

範例 #1 產生 Imagick::getImageHistogram()

<?php
function getColorStatistics($histogramElements, $colorChannel) {
$colorStatistics = [];

foreach (
$histogramElements as $histogramElement) {
$color = $histogramElement->getColorValue($colorChannel);
$color = intval($color * 255);
$count = $histogramElement->getColorCount();

if (
array_key_exists($color, $colorStatistics)) {
$colorStatistics[$color] += $count;
}
else {
$colorStatistics[$color] = $count;
}
}

ksort($colorStatistics);

return
$colorStatistics;
}



function
getImageHistogram($imagePath) {

$backgroundColor = 'black';

$draw = new \ImagickDraw();
$draw->setStrokeWidth(0); //make the lines be as thin as possible

$imagick = new \Imagick();
$imagick->newImage(500, 500, $backgroundColor);
$imagick->setImageFormat("png");
$imagick->drawImage($draw);

$histogramWidth = 256;
$histogramHeight = 100; // the height for each RGB segment

$imagick = new \Imagick(realpath($imagePath));
//Resize the image to be small, otherwise PHP tends to run out of memory
//This might lead to bad results for images that are pathologically 'pixelly'
$imagick->adaptiveResizeImage(200, 200, true);
$histogramElements = $imagick->getImageHistogram();

$histogram = new \Imagick();
$histogram->newpseudoimage($histogramWidth, $histogramHeight * 3, 'xc:black');
$histogram->setImageFormat('png');

$getMax = function ($carry, $item) {
if (
$item > $carry) {
return
$item;
}
return
$carry;
};

$colorValues = [
'red' => getColorStatistics($histogramElements, \Imagick::COLOR_RED),
'lime' => getColorStatistics($histogramElements, \Imagick::COLOR_GREEN),
'blue' => getColorStatistics($histogramElements, \Imagick::COLOR_BLUE),
];

$max = array_reduce($colorValues['red'] , $getMax, 0);
$max = array_reduce($colorValues['lime'] , $getMax, $max);
$max = array_reduce($colorValues['blue'] , $getMax, $max);

$scale = $histogramHeight / $max;

$count = 0;
foreach (
$colorValues as $color => $values) {
$draw->setstrokecolor($color);

$offset = ($count + 1) * $histogramHeight;

foreach (
$values as $index => $value) {
$draw->line($index, $offset, $index, $offset - ($value * $scale));
}
$count++;
}

$histogram->drawImage($draw);

header( "Content-Type: image/png" );
echo
$histogram;
}

?>

新增註記

使用者貢獻的註記 1 則註記

Iddles
13 年前
我苦思許久,試圖理解為什麼它只返回一個彩色像素列表,卻似乎沒有顏色計數。結果發現,由於某種原因,ImagickPixel 類別有一個「getColorCount」方法,雖然看起來很奇怪,但確實如此。

<?php
$image
= new Imagick("thing.png");
$pixels=$image->getImageHistogram();
foreach(
$pixels as $p){
$colors = $p->getColor();
foreach(
$colors as $c){
print(
"$c\t" );
}
print(
"\t:\t" . $p->getColorCount() . "\n" );
}
?>

這將會印出類似以下的內容

252 250 252 1 : 125
194 156 182 1 : 126
109 18 79 1 : 11440
2 117 162 1 : 12761
255 255 255 1 : 40769

...其中各欄位分別代表紅色、綠色、藍色、Alpha 值,後面接著該顏色在圖片中出現的次數。
To Top