You are on page 1of 7

Histogram equalization

Contents
Background.................................................................................................................................................... 2
Procedure ...................................................................................................................................................... 3

Page 1 of 7
Background
To understand histogram equalization, one must first understand the concept of contrast in an image.
The contrast is defined as the difference in color or intensity between two objects in an image. If the
contrast is too low, it is impossible to distinguish between two objects and they are seen as a single
object. In Figure 1, the contrast is high and it is easy to separate the black from the gray. In Figure 2, it
becomes much more difficult to separate the gray from the darker gray. This is because the colors are
very close to each other and the human eye starts to treat the two colors as one.

Figure 1 Figure 2

These two images can be regarded as having a resolution of 1x2 pixels. For the simplicity, this paper only
discusses 8-bit grayscale images where the intensity can vary between 0 and 255. This means that each
pixel only have one intensity value and no color value. By looking at Figure 1, one sees that the right pixel
is completely black and thus has an intensity of 0 and the left pixel is quite bright with an intensity of
238. In Figure 2, the two pixels have the intensity values 238 and 228. There are many different formulas
for calculating contrast but the main point is that, the larger difference in intensity, the larger is the
contrast. To visualize the contrast in an image in another way than just looking at an image, an intensity
histogram can be used. An intensity histogram is a histogram containing the distribution of all the
possible intensities. An 8-bit image has 256 different intensity values and thus the x-axis will contain 256
different values and the y-axis will display how many of each intensity values there are. A typical
intensity histogram from an 8-bit grayscale image could look like the histogram in Figure 3.

125
Counts

100

75

50

25

0
1 16 31 46 61 76 91 106 121 136 151 166 181 196 211 226 241 256
Intensity

Figure 3

Page 2 of 7
This intensity histogram is a typical example of a “good” histogram which means that almost all of the
pixels are used. This is good because the whole available range of intensities contributes to the image.
Representing a color image in a histogram can be a little more difficult, and it is probably easiest to
create three histograms, one for red, one for green and one for blue, this is outside the scope of this
paper. Another example histogram is seen in Figure 4. The image corresponding to this histogram looks a
little different than the image in the previous example. In this histogram, the pixels are much more
concentrated at intensities in the middle of the spectrum. All pixels are between 95 and 180. The
available intensity values outside of this range are not used. One can draw the conclusion that the
contrast in this image is low because only a portion of the available pixel values are used. No point in
having a large range when only a small portion of it is used.

125
Counts

100

75

50

25

0
1 16 31 46 61 76 91 106 121 136 151 166 181 196 211 226 241 256
Intensity

Figure 4

Images having a histogram similar to the one seen in Figure 4 can be difficult for the human eye to look
at. This is because the human eye is sensitive to intensity in a nonlinear manner while the sensor
responsible for capturing the light works in a more linear fashion. To give the human eye best possible
conditions, the image can be altered so that all the available pixel intensities are used. This is known as
histogram equalization.

Procedure
The mathematical procedure for mapping the image pixels to new pixels values that will increase the
overall contrast in the image is quite straightforward. The intensity of a pixel is represented by i and the
available intensities are 0≤i≤L-1 where L for an 8-bit image would be 256, the available intensities are in
that case 0 to 255. An intensity (i) value of L-1 is considered as white and an i value of 0 is considered

Page 3 of 7
black. It would be good to have a function that based on the intensity value in a pixel and how many
times that intensity value occurs assign a new intensity value to this pixel, this function is declared T(i).

𝑠 = 𝑇(𝑖), 0≤𝑖 ≤𝐿−1

Equation 1

The value s is the new pixel value based on the old pixel value. It is now clear that histogram equalization
manipulates data and if one would perform histogram equalization on a grayscale image where the pixel
values represented some kind of temperature, the new pixel values would have a different temperature.
With better overall contrast, but nevertheless, a temperature that doesn’t represent the true
temperature, but rather a software assigned temperature. The function T(i) should be monotonically
increasing on the interval 0 ≤ i ≤ L-1. The demand for it to be monotonically increasing exists so that one
can be assured that no pixel intensities can be reversed. For example, T(10)=5 and T(11)=4 should not be
possible. Another restriction is 0 ≤ T(i) ≤ 0-L, so that the range of intensities is the same in the original
image and the output image.

The intensity values in an image can be regarded as random variables that can have any value between 0
and L-1. This random event has a so called cumulative distribution function (CDF) associated to itself.
This function describes the likelihood that the random variable will be assigned a value less or equal to a
specific value. For example, when one throws a dice the probability that the dice will end up showing a
value of 2 or less than 2 is 1/6 + 1/6 = 2/6. Which means that CDF(dice≤2) = probability(dice=1) +
probability(dice=2).
𝑡

𝐶𝐷𝐹(𝑖 ≤ 𝑡) = ∑ 𝑝𝑘
𝑘=0

Equation 2

Equation 2 describes the CDF for the probability that a pixel has the intensity equal or lower than t. The
probability that a random pixel has the intensity value k is the number of pixels with the intensity k
divided by the total number of pixels, see Equation 3.

𝑎𝑚𝑜𝑢𝑛𝑡 𝑜𝑓 𝑝𝑖𝑥𝑒𝑙𝑠 𝑤𝑖𝑡ℎ 𝑖𝑛𝑡𝑒𝑛𝑠𝑖𝑡𝑦 𝑘


𝑝𝑟𝑜𝑏𝑎𝑏𝑖𝑙𝑖𝑡𝑦 𝑡ℎ𝑎𝑡 𝑎 𝑟𝑎𝑛𝑑𝑜𝑚 𝑝𝑖𝑥𝑒𝑙 ℎ𝑎𝑠 𝑡ℎ𝑒 𝑖𝑛𝑡𝑒𝑛𝑠𝑖𝑡𝑦 𝑘 = 𝑃𝑘 =
𝑡𝑜𝑡𝑎𝑙 𝑛𝑢𝑚𝑏𝑒𝑟 𝑜𝑓 𝑝𝑖𝑥𝑒𝑙𝑠

Equation 3

The values are often normalized, so that p0+ p1+ …+ pL-1=1. The total probability that any pixel has an
intensity value in the acceptable range (0 ≤ i ≤L-1) is of course 1. The goal is to create some
transformation s=T(i) that creates a new image with a histogram more resembling Figure 3 than Figure 4.
This means that the CDF should be more of a linear function rather than a strict increasing function as
Equation 2. An image with its grayscale values and the corresponding histogram is seen in Figure 5 and
Figure 6. This image has a range from 0 to 9, which means that 0 ≤ i ≤ 9.

Page 4 of 7
6 2 3 4 5 8
Counts
6
5 5 6 7 5
4
5 6 4 8 6 2
4 7 4 3 4 0
8 5 6 6 7 0 1 2 3 4 5 6 7 8 9
Intensity
Figure 5 Figure 6

The histogram of the image tells the user that most of the pixels have values around 5. One can create a
table containing every intensity value and how many pixels that containing this value, this is seen in
Table 1. The intensity values can be regarded as temperature generated by a thermal camera.

Intensity Frequency
0 0
1 0
2 1
3 2
4 5
5 6
6 6
7 3
8 2
9 0
Table 1

Table 1 shows the same information as the histogram but it may be easier to understand if one looks at
the table. Using Equation 3, the following values can be created:

0 0 1 2 5
𝑃0 = = 0 𝑃1 = = 0 𝑃2 = = 0.04 𝑃3 = = 0.08 𝑃4 = = 0.2
25 25 25 25 25

6 6 3 2 0
𝑃5 = = 0.24 𝑃6 = = 0.24 𝑃7 = = 0.12 𝑃8 = = 0.08 𝑃9 = =0
25 25 25 25 25
Equation 4

A check for counting errors can be made by summing all the probability values and confirming that the
sum equals 1, which it does. Now comes the transformation part, the transformation T(i) is given by
Equation 5.

Page 5 of 7
𝑖 𝑖

𝑠 = 𝑇(𝑖) = 𝑓𝑙𝑜𝑜𝑟((𝐿 − 1) ∗ ∑ 𝑝𝑘 ) = 𝑓𝑙𝑜𝑜𝑟(9 ∗ ∑ 𝑝𝑘 )


𝑘=0 𝑘=0

Equation 5

Where floor is the nearest integer down, 7.3 will be 7, 4.7 will be 4 and so on. This transformation uses
the probability value for each intensity value and the available range to create a new pixel value (s) for
every old pixel value (i). Using Equation 4 and Equation 5, it is possible to create Table 2.

i T(i)
0 0
1 0
2 0
3 1
4 2
5 5
6 7
7 8
8 9
9 9
Table 2

Table 2 is the desired mapping from the input image to the output image and the new image is seen in
fig. Bear in mind that the intensity values 0, 1 and 9 are nowhere to be found in the input image but are
included anyway. The resulting image and its histogram can be found in Figure 7 and Figure 8.

Counts
7 0 1 2 5 7
6
5
5 5 7 8 5 4
3
5 7 2 9 7 2
1
0
2 8 2 1 2
0 1 2 3 4 5 6 7 8 9

9 5 7 7 8 Intensity
Figure 7 Figure 8

The resulting images histogram is much better distributed across the whole range and this will result in a
much higher overall contrast across all pixels. If one would apply a grayscale palette to Figure 5 and
Figure 7, the result would look like Figure 9 and Figure 10.

Page 6 of 7
Figure 9 Figure 10

It could also be of interest to plot the transformation function which gives the user an intuitive way of
seeing what is happening to the pixel values. In Figure 11 the transformation function is seen.

Figure 11

The transformed image seen in Figure 10 has higher contrast than the original image but the pixel values
have changed which means that information is lost. This one has to remember when using histogram
equalization.

Page 7 of 7

You might also like