ImagickDemos
ImagickDemos copied to clipboard
Explanation of the getColorCount function.
Hallo Danack,
at first I want to thank you for your work because I really love the way you prepared your demo page for Imagick as the php.net Docu is at many points not very helpful when talking about Imagick. So I want to explain the getColorCount function as your comment implies you don't now what it's functionality is meant to do. Even if php.net's page for this function is saying "Warning This function is currently not documented; only its argument list is available." it offers an example on http://php.net/manual/en/imagick.getimagehistogram.php and so you do on http://phpimagick.com/Imagick/getImageHistogram. Looking at your example code we see
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;
}
}
So what this function is basically doing is counting the pixels in the image with the same color code as this pixel which explains why your example code was not working as it was missing the context of an image. I think this context is a necessary condition for this function to return a meaningful result. I hope I was able to show you the way this function works and you will be able to provide a demonstration on your side.
Greetings Joe Pietler
Thanks for pointing me in the right direction, and apologies for the slow response.
It seems like this value is only available some of the time....I guess maybe only when the ImagickPixel is returned out of a getImageHistogram?
For the code below, getting a pixel through $imagick->getImagePixelColor(50, 50);
it seems that the ImagickPixel has a count of zero associated with it. But then getting all of the colors through the histogram function, you can see that the last entry of the histogram, which has the same white color as the pixel retrieved through getImagePixelColor, instead has a count of 256244 associated with it.
color count is 0
r 1.000000 g 1.000000 b 1.000000 array(4) {
["r"]=>
int(255)
["g"]=>
int(255)
["b"]=>
int(255)
["a"]=>
int(1)
}
count: 256244
...
... 254 more entries here.
...
r 1.000000 g 1.000000 b 1.000000 array(4) {
["r"]=>
int(255)
["g"]=>
int(255)
["b"]=>
int(255)
["a"]=>
int(1)
}
count: 256244
$imagick = new Imagick();
$imagick->newPseudoImage(640, 480, "magick:logo");
$imagickPixel = $imagick->getImagePixelColor(50, 50);
$count = $imagickPixel->getColorCount();
echo "color count is ".$imagickPixel->getColorCount().PHP_EOL;
$histogramElements = $imagick->getImageHistogram();
echo "There are ".count($histogramElements)." elements in the histogram.".PHP_EOL;
/** @var $histogramElements ImagickPixel[] */
foreach ($histogramElements as $histogramElement) {
$count = $histogramElement->getColorCount();
printf(
"r %f g %f b %f ",
$histogramElement->getColorValue(\Imagick::COLOR_RED),
$histogramElement->getColorValue(\Imagick::COLOR_GREEN),
$histogramElement->getColorValue(\Imagick::COLOR_BLUE)
);
var_dump($histogramElement->getColor());
echo "count: ".$count.PHP_EOL;
}