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 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 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 1 75 5 25 1 16 31 46 61 76 91 16 121 136 151 166 181 196 211 226 241 256 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 18. 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 1 75 5 25 1 16 31 46 61 76 91 16 121 136 151 166 181 196 211 226 241 256 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 i L-1 where L for an 8-bit image would be 256, the available intensities are in that case to 255. An intensity (i) value of L-1 is considered as white and an i value of 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). s = T(i), i L 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 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(1)=5 and T(11)=4 should not be possible. Another restriction is T(i) -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 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). CDF(i t) = p k 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. t k= probability that a random pixel has the intensity k = P k = Equation 3 amount of pixels with intensity k total number of pixels The values are often normalized, so that p + p 1 + + p L-1 =1. The total probability that any pixel has an intensity value in the acceptable range ( 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 to 9, which means that i 9. Page 4 of 7
6 2 3 4 5 5 5 6 7 5 5 6 4 8 6 4 7 4 3 4 8 5 6 6 7 8 6 4 2 1 2 3 4 5 6 7 8 9 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. 1 2 1 3 2 4 5 5 6 6 6 7 3 8 2 9 Table 1 Frequency 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: P = 25 = P 1 = 25 = P 2 = 1 25 =.4 P 3 = 2 25 =.8 P 4 = 5 25 =.2 P 5 = 6 25 =.24 P 6 = 6 25 =.24 P 7 = 3 25 =.12 P 8 = 2 25 =.8 P 9 = 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
s = T(i) = floor((l 1) p k ) = floor(9 p k ) i k= 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) 1 2 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, 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. i k= 7 1 2 5 5 5 7 8 5 5 7 2 9 7 2 8 2 1 2 9 5 7 7 8 7 6 5 4 3 2 1 1 2 3 4 5 6 7 8 9 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 1. Page 6 of 7
Figure 9 Figure 1 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 1 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