Get notified about new tutorials
RECEIVE NEW TUTORIALS

First 15 Minutes Free

302 sessions given

since Jul 09, 2014

since Jul 09, 2014

Likelihood of Reply:
65%

Response Time:
within an hour

Ray Phan

Feb 02, 2015

<p>Histogram equalization seeks to <strong>flatten</strong> your image histogram. Basically, it models the image as a probability density function (or in simpler terms, a histogram where you normalize each entry by the total number of pixels in the image) and tries to ensure that the probability for a pixel to take on a particular intensity is <strong>equiprobable</strong> (with equal probability).</p>
<p>The premise behind histogram equalization is for images that have poor contrast. Images that look like they're too dark, or if they're too washed out, or if they're too bright are good candidates for you to apply histogram equalization. If you plot the histogram, the spread of the pixels is limited to a very narrow range. By doing histogram equalization, the histogram will thus flatten and give you a better contrast image. The effect of this with the histogram is that it stretches the dynamic range of your histogram.</p>
<p>In terms of the mathematical definition, I won't bore you with the details and I would love to have some LaTeX to do it here, but it isn't supported. As such, I defer you to this link that explains it in more detail: <a href="http://www.math.uci.edu/icamp/courses/math77c/demos/hist_eq.pdf" rel="nofollow">http://www.math.uci.edu/icamp/courses/math77c/demos/hist_eq.pdf</a></p>
<p>However, the final equation that you get for performing histogram equalization is essentially a 1-to-1 mapping. For each pixel in your image, you extract its intensity, then run it through this function. It then gives you an output intensity to be placed in your output image.</p>
<p>Supposing that <code>p_i</code> is the probability that you would encounter a pixel with intensity <code>i</code> in your image (take the histogram bin count for pixel intensity <code>i</code> and divide by the total number of pixels in your image). Given that you have <code>L</code> intensities in your image, the <strong>output</strong> intensity at this location given the intensity of <code>i</code> is dictated as:</p>
<pre><code>g_i = floor( (L-1) * sum_{n=0}^{i} p_i )
</code></pre>
<p>You add up all of the probabilities from pixel intensity 0, then 1, then 2, all the way up to intensity <code>i</code>. This is familiarly known as the Cumulative Distribution Function.</p>
<p>MATLAB essentially performs histogram equalization using this approach. However, if you want to implement this yourself, it's actually pretty simple. Assume that you have an input image <code>im</code> that is of an unsigned 8-bit integer type.</p>
<pre><code>function [out] = hist_eq(im, L)
if (~exist(L, 'var'))
L = 256;
else
L = double(max(im(:))) + 1;
end
h = imhist(im) / numel(im);
cdf = cumsum(h);
out = (L-1)*cdf(double(im)+1);
out = uint8(out);
</code></pre>
<p>This function takes in an image that is assumed to be unsigned 8-bit integer. You can optionally specify the number of levels for the output. Usually, <code>L = 256</code> for an 8-bit image and so if you omit the second parameter, <code>L</code> would be assumed as such. The first line computes the probabilities. The next line computes the Cumulative Distribution Function (CDF). The next two lines after compute input/output using histogram equalization, and then convert back to unsigned 8-bit integer. Note that the <code>uint8</code> casting implicitly performs the <strong>floor</strong> operation for us. You'll need to take note that we have to add an <strong>offset of 1</strong> when accessing the CDF. The reason why is because MATLAB starts indexing at 1, while the intensities in your image start at 0.</p>
<p>The MATLAB command <code>histeq</code> pretty much does the same thing, except that if you call <code>histeq(im)</code>, it assumes that you have 32 intensities in your image. Therefore, you can override the <code>histeq</code> function by specifying an additional parameter that specifies how many intensity values are seen in the image just like what we did above. As such, you would do <code>histeq(im, 256);</code>. Calling this in MATLAB, and using the function I wrote above should give you <strong>identical</strong> results.</p>
<p>As a bit of an exercise, let's use an image that is part of the MATLAB distribution called <code>pout.tif</code>. Let's also show its histogram.</p>
<pre><code>im = imread('pout.tif');
figure;
subplot(2,1,1);
imshow(im);
subplot(2,1,2);
imhist(im);
</code></pre>
<p><img src="http://i.stack.imgur.com/14pAR.jpg" alt="enter image description here"></p>
<p>As you can see, the image has poor contrast because most of the intensity values fit in a narrow range. Histogram equalization will flatten the image and thus increase the contrast of the image. As such, try doing this:</p>
<pre><code>out = histeq(im, 256); %//or you can use my function: out = hist_eq(im);
figure;
subplot(2,1,1);
imshow(out);
subplot(2,1,2);
imhist(out);
</code></pre>
<p>This is what we get:</p>
<p><img src="http://i.stack.imgur.com/1MjEA.jpg" alt="enter image description here"></p>
<p>As you can see the contrast is better. Darker pixels tend to move towards the darker end, while lighter pixels get pushed towards the lighter end. Successful result I think! Bear in mind that not all images will give you a good result when you try and do histogram equalization. Image processing is mostly a trial and error thing, and so you put a mishmash of different techniques together until you get a good result.</p>
<p>This should hopefully get you started. Good luck!</p>
<p>This tip was originally posted on <a href="http://stackoverflow.com/questions/24094649/Explanation%20of%20the%20Histogram%20Equalization%20function%20in%20MATLAB/24094955">Stack Overflow</a>.</p>