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.