I am not going to explain the professional terms in detail. Interested readers can check the reference link at the end of the article, where there is an easy-to-understand explanation:
Let’s first find an example image (shot with Canon 550D):
Example picture: butterfly.jpg
Let’s see how to use Imagick to implement image histogram:
Copy code The code is as follows:
$file = 'butterfly.jpg';
$size = array(
'width' => 256,
'height' => 100,
);
$image = new Imagick($file);
$histogram = array_fill_keys(range(0, 255), 0);
foreach ($image->getImageHistogram() as $pixel) {
$rgb = $pixel->getColor();
$histogram[$rgb['r']] += $pixel-> getColorCount();
$histogram[$rgb['g']] += $pixel->getColorCount();
$histogram[$rgb['b']] += $pixel-> getColorCount();
}
$max = max($histogram);
$threshold = ($image->getImageWidth() * $image->getImageHeight()) / 256 * 12;
if ($max > $threshold) {
$max = $threshold;
}
$image = new Imagick();
$draw = new ImagickDraw();
$image->newImage($size['width'], $size['height'], 'white');
foreach ($histogram as $x => $count) {
if ($count == 0) {
continue;
}
$draw->setStrokeColor('black');
$height = min($count, $max) / $max * $size['height'];
$draw->line($x, $size['height'], $x, $size['height'] - $height);
$image- >drawImage($draw);
$draw->clear();
}
$image->setImageFormat('png');
$image->writeImage(' histogram.png');
?>
Note: The reason why $threshold is added to the code is because sometimes the values of certain color levels may be very large. If Failure to do so will interfere with the final generation effect. As for why we need to divide by 256 first and then multiply by 12, there is no reason at all. I just decided on my head. You can also use other methods.
The final generated histogram has basically the same effect as Photoshop. Here is the one from Photoshop:
Histogram generated by Photoshop
Note: After opening the picture with Photoshop, Just select Window and then Histogram.
What this article is actually talking about is the histogram drawing method of the RGB channel. In principle, the RGB histogram is the result of the accumulation of red, green, and blue histograms. As for the histograms of the three primary colors of red, green, and blue, the above code can be slightly modified. .
Note: There is an open source project of image histogram implemented in HTML5 on XARG.ORG. The effect is good and worth learning.
Finally, by the way, if you are interested in photography knowledge, you can refer to: How to interpret the histogram of a digital camera.
http://www.bkjia.com/PHPjc/324322.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324322.htmlTechArticleI do not intend to explain professional terms in detail. Interested readers can check the reference link at the end of the article, where there are Easy-to-understand explanation: Let’s first find an example image (using Canon...