Use Imagick in php to implement the image histogram.

Source: Internet
Author: User

I am not going to explain the terms in detail. If you are interested, you can refer to the reference link at the end of the article. There are easy-to-understand explanations:

Let's first look for an example image (shot with Canon 550D ):

Example image: butterfly.jpg

Let's take a look at how to use Imagick to implement the image histogram:
Copy codeThe Code is as follows:
<? Php
$ File = 'butterfly.jpg ';
$ Size = array (
& Apos; width & apos; = & apos; 256 & apos,
& Apos; height & apos; = & apos; 100 & apos,
);
$ 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 the $ threshold value is added to the Code is that sometimes the values of certain levels may be very large. If not processed, the final generation effect will be affected. There is no reason why we should first divide 256 and then multiply 12. It's all determined by my head. You can also use other methods.

The final histogram is basically the same as that of Photoshop. Here we will post the Photoshop:

Histogram generated by Photoshop
Note: After opening an image using Photoshop, select a window and select a histogram.
This article only describes how to draw the histogram of the RGB channel. In principle, the RGB histogram is the result of the accumulation of the 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 Image Histogram project implemented by HTML5 on XARG. ORG, which has good results and is worth learning.
Finally, if you are interested in photography, refer to: how to interpret the histogram of a digital camera.

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.