Use Imagick in php to implement the image histogram.

Source: Internet
Author: User
People who have played SLR cameras should know the image histogram (ImageHistogram). Simply put, it calculates the proportion of each color level in the total pixel to reflect the image exposure. 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:
The code is as follows:
$ 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.