Implementation code _php tutorial for implementing image histogram using Imagick in PHP

Source: Internet
Author: User
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 ... ).

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.