{"id":696,"date":"2014-07-12T18:07:47","date_gmt":"2014-07-12T17:07:47","guid":{"rendered":"http:\/\/blog.blockos.org\/?p=696"},"modified":"2014-09-21T17:04:15","modified_gmt":"2014-09-21T16:04:15","slug":"10-tons-histogram","status":"publish","type":"post","link":"https:\/\/blog.blockos.org\/?p=696","title":{"rendered":"10 tons histogram."},"content":{"rendered":"<p>What is an image histogram?<br \/>\nWell, it is fairly simple. Consider we have a grayscale image. Its histogram is an array that stores the number of occurence of each grey level. It gives us a representation of the tone distribution.<\/p>\n<div id=\"attachment_707\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/lena+hist.png\"><img aria-describedby=\"caption-attachment-707\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-707\" title=\"lena+hist\" src=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/lena+hist.png\" alt=\"Lena image and its histogram.\" width=\"512\" height=\"256\" srcset=\"https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/lena+hist.png 512w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/lena+hist-300x150.png 300w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/lena+hist-500x250.png 500w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><p id=\"caption-attachment-707\" class=\"wp-caption-text\">Source image and its histogram.<\/p><\/div>\n<p>If an image is dark, there will be a peak near the lowest tone values. Or the other hand brighter image will have peaks near the maximum tone values.<\/p>\n<div id=\"attachment_705\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/dark_lena+hist.png\"><img aria-describedby=\"caption-attachment-705\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-705\" title=\"dark_lena+hist\" src=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/dark_lena+hist.png\" alt=\"Dark lena with associated histogram.\" width=\"512\" height=\"256\" srcset=\"https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/dark_lena+hist.png 512w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/dark_lena+hist-300x150.png 300w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/dark_lena+hist-500x250.png 500w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><p id=\"caption-attachment-705\" class=\"wp-caption-text\">Dark image<\/p><\/div>\n<div id=\"attachment_701\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/bright_lena+hist1.png\"><img aria-describedby=\"caption-attachment-701\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-701\" title=\"bright_lena+hist\" src=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/bright_lena+hist1.png\" alt=\"Bright image\" width=\"512\" height=\"256\" srcset=\"https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/bright_lena+hist1.png 512w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/bright_lena+hist1-300x150.png 300w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/bright_lena+hist1-500x250.png 500w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><p id=\"caption-attachment-701\" class=\"wp-caption-text\">Bright image with associated histogram.<\/p><\/div>\n<p>The image histogram can also give us informations about its contrast. An image with a low contrast will have a peak around its median value and most of the histogram values will be concentrated around that peak. Alternatively a highly contrasted image will have peaks around its minimal and maximal tone value as long as sparse low values in between.<\/p>\n<div id=\"attachment_712\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/low_contrast_lena+hist.png\"><img aria-describedby=\"caption-attachment-712\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-712\" title=\"low_contrast_lena+hist\" src=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/low_contrast_lena+hist.png\" alt=\"Low contrast Lena and its histogram.\" width=\"512\" height=\"256\" srcset=\"https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/low_contrast_lena+hist.png 512w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/low_contrast_lena+hist-300x150.png 300w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/low_contrast_lena+hist-500x250.png 500w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><p id=\"caption-attachment-712\" class=\"wp-caption-text\">Low contrast image<\/p><\/div>\n<p><div id=\"attachment_713\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/high_contrast_lena+hist.png\"><img aria-describedby=\"caption-attachment-713\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-713\" title=\"high_contrast_lena+hist\" src=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/high_contrast_lena+hist.png\" alt=\"High contrast lena and its histogram\" width=\"512\" height=\"256\" srcset=\"https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/high_contrast_lena+hist.png 512w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/high_contrast_lena+hist-300x150.png 300w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2012\/07\/high_contrast_lena+hist-500x250.png 500w\" sizes=\"(max-width: 512px) 100vw, 512px\" \/><\/a><p id=\"caption-attachment-713\" class=\"wp-caption-text\">High contrast image<\/p><\/div><br \/>\nIt seems logical that if we stretch the histogram over the whole tone range, we will increase the image constrast. This operation is called <strong>histogram equalization<\/strong>. Before going any further, let&#8217;s introduce some basic definitions. The first one is the <strong>probability density function<\/strong>:<\/p>\n<p style=\"text-align: center;\">\\(P^{[l_s,l_f]}_l = \\frac{h_l}{H^{[l_s,l_f]}}\\)<\/p>\n<p style=\"text-align: center;\">With \\(0 \\le l_s <  l_f \\le l_{max}[\/latex]. Usually [latex]l_{max} = 255[\/latex].<\/p>\n<p>[latex]H^{[m,n]}\\) is introduced for brevity. It&#8217;s the sum of the histogram value over the range \\([m,n]\\).<\/p>\n<p style=\"text-align: center;\">\\(H^{[m,n]} = \\sum_{i=m}^{n}h_i\\)<\/p>\n<p>Next is the <strong>probability distribution function<\/strong>.<\/p>\n<p style=\"text-align: center;\">\\(C^{[l_s,l_f]}_l = \\frac{\\sum_{i=l_s}^{l}h_i}{H^{[l_s,l_f]}}\\)<\/p>\n<p>Which can be rewritten as:<\/p>\n<p style=\"text-align: center;\">\\(C^{[l_s,l_f]}_l = \\sum_{i=l_s}^{l}P^{[l_s,l_f]}_i\\)<\/p>\n<p>The mean of the histogram values is also called the image brightness.<\/p>\n<p style=\"text-align: center;\">\\(l_m = \\frac{\\sum_{i=l_s}^{l_f}i \\times h_i}{H^{[l_s,l_f]}}\\)<\/p>\n<p>To put it simply,<\/p>\n<p style=\"text-align: center;\">\\(l_m = \\sum_{i=l_s}^{l_f}i P^{[l_s,l_f]}_i\\)<\/p>\n<p>The aim of histogram equalization is to produce a histogram where each tone has the same number of occurence (uniform distribution). This is equivalent to saying that any tone \\(l\\) has the same density.<\/p>\n<p style=\"text-align: center;\">\\(P&#8217;^{[l_s,l_f]}_{l} = \\frac{1}{l_f &#8211; ls}\\)<\/p>\n<p>Which gives us the ideal probability distribution function.<\/p>\n<p style=\"text-align: center;\">\\(C&#8217;^{[l_s,l_f]}_{l} = \\sum_{i=l_s}^{l}P&#8217;^{[l_s,l_f]}_i = \\frac{l-ls}{lf &#8211; ls}\\)<\/p>\n<p>A way to acheive this is to find \\(l&#8217;\\) that minimizes the difference between \\(C&#8217;^{[l_s,l_f]}_{l&#8217;}\\) and \\(C^{[l_s,l_f]}_{l}\\). In other words, we want to find \\(l&#8217;\\) so that:<\/p>\n<p style=\"text-align: center;\">\\(C^{[l_s,l_f]}_{l} = C&#8217;^{[l_s,l_f]}_{l&#8217;}\\)<\/p>\n<p>Which gives us:<\/p>\n<p style=\"text-align: center;\">\\(l&#8217; = l_s + \\big\\langle(l_f &#8211; l_s) C^{[l_s,l_f]}_l \\big\\rangle\\)<\/p>\n<p>Where \\(\\langle x \\rangle\\) is the nearest integer to \\(x\\).<\/p>\n<p> This technique is sometimes referred as the <strong>classical histogram equalization<\/strong> or <strong>CHE<\/strong>. The drawback of this method is that it changes the image brightness.<\/p>\n<p>The following source code is performing <strong>CHE<\/strong> with \\(ls = 0\\) and \\(l_f = l_{max}\\).<br \/>\nThis code is using the <a href=\"http:\/\/cimg.sourceforge.net\/\" title=\"CImg library\" target=\"_blank\">CImg<\/a> library.<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\n    \/\/ We will load a dark grayscale version of the \r\n    \/\/ classical lena image.\r\n    CImg&lt;unsigned char&gt; source;\r\n    source.load(&quot;dark_lena.png&quot;);\r\n\t\r\n    unsigned long int lut[256];\r\n    unsigned long int histogram[256];\r\n\r\n    \/\/ Build image histogram.\r\n    memset(histogram, 0, sizeof(unsigned long int)*256);\r\n    cimg_forXY(source, x, y)\r\n    {\r\n        histogram[source(x,y,0,0)]++;\r\n    }\r\n\r\n    \/\/ Compute probality distribution function.\r\n    unsigned long int sum = 0;\r\n    for(int i=0; i&lt;256; i++)\r\n    {\r\n        sum += histogram[i];\r\n        lut[i] = floor(255.0 * sum \/ (source.width() * source.height()) + 0.5);\r\n    }\r\n    \r\n    \/\/ Perform CHE\r\n    cimg_forXY(source, x, y)\r\n    {\r\n        source(x,y,0,0) = lut[(int)source(x, y, 0, 0)];\r\n    }\r\n  \r\n<\/pre>\n<div id=\"attachment_712\" style=\"width: 522px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2014\/07\/lena_CHE.png\"><img aria-describedby=\"caption-attachment-712\" decoding=\"async\" loading=\"lazy\" src=\"http:\/\/blog.blockos.org\/wp-content\/uploads\/2014\/07\/lena_CHE.png\" alt=\"CHE\" width=\"509\" height=\"256\" class=\"aligncenter size-full wp-image-934\" srcset=\"https:\/\/blog.blockos.org\/wp-content\/uploads\/2014\/07\/lena_CHE.png 509w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2014\/07\/lena_CHE-300x150.png 300w, https:\/\/blog.blockos.org\/wp-content\/uploads\/2014\/07\/lena_CHE-500x251.png 500w\" sizes=\"(max-width: 509px) 100vw, 509px\" \/><\/a><p id=\"caption-attachment-712\" class=\"wp-caption-text\">Result of CHE source code.<\/p><\/div>\n<h2>References:<\/h2>\n<ul>\n<li>David Menotti Gomes. <a href=\"http:\/\/tel.archives-ouvertes.fr\/docs\/00\/47\/05\/45\/PDF\/2008PEST0226_0_1.pdf\">Contrast Enhancement in Digital Imaging using Histogram Equalization<\/a>. PhD Thesis, June 2008<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What is an image histogram? Well, it is fairly simple. Consider we have a grayscale image. Its histogram is an array that stores the number of occurence of each grey level. It gives us a representation of the tone distribution. If an image is dark, there will be a peak\u2026 <a class=\"continue-reading-link\" href=\"https:\/\/blog.blockos.org\/?p=696\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[27,11],"_links":{"self":[{"href":"https:\/\/blog.blockos.org\/index.php?rest_route=\/wp\/v2\/posts\/696"}],"collection":[{"href":"https:\/\/blog.blockos.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.blockos.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.blockos.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.blockos.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=696"}],"version-history":[{"count":110,"href":"https:\/\/blog.blockos.org\/index.php?rest_route=\/wp\/v2\/posts\/696\/revisions"}],"predecessor-version":[{"id":942,"href":"https:\/\/blog.blockos.org\/index.php?rest_route=\/wp\/v2\/posts\/696\/revisions\/942"}],"wp:attachment":[{"href":"https:\/\/blog.blockos.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=696"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.blockos.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=696"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.blockos.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=696"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}