I do not intend to explain the professional terminology in detail, and interested readers can refer to the reference link at the end of the article, where there is an understandable explanation:
Let's look at an example image (taken with Canon 550D):
Example Image: Butterfly.jpg
Here's a look at how to implement an image histogram using Imagick:
Copy CodeThe 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 value of $threshold is added to the code because sometimes the values of certain levels may be very large, and if you do not do so, you will interfere with the resulting results. As for why first except 256, followed by 12, there is no reason to say, all of me a pat on the head, you can also use other methods.
The resulting histogram is basically the same as Photoshop's, and here's what Photoshop looks like:
Photoshop-generated histograms
Note: When you open a picture using Photoshop, select the window and select the histogram.
This article is actually just RGB channel histogram rendering method, in principle, the RGB histogram is the red green and blue histogram accumulation results, as for the red and green blue color of the respective histogram, the above code slightly modified.
Note: Xarg.org has a HTML5 implementation of the image histogram open source project, the effect is good, worth learning.
Finally, by the way, if you are interested in photographic knowledge, please refer to: How to interpret the digital camera histogram.
http://www.bkjia.com/PHPjc/324322.html www.bkjia.com true http://www.bkjia.com/PHPjc/324322.html techarticle I do not intend to explain the professional terminology in detail, and interested readers can refer to the reference link at the end of the article, where there is an easy-to-understand explanation: We first look for an example image (with Canon ... ).