Histogram Equalization
In this exercise, we will implement histogram equalization.
Sometimes, an image has a relatively narrow distribution of brightness values (see Fig. 1). Such an image has low contrast, which makes details difficult to distinguish. There are many techniques for improving image contrast; in this exercise, we will implement a simple one.
First, compute the histogram of the image. The histogram indicates how many pixels in the image have a particular brightness value . In our case, , where is the number of brightness levels in the image. For an 8-bit grayscale image, .
The histogram value for brightness level is defined as
where is the number of pixels with brightness value .
Next, compute the cumulative distribution function corresponding to the histogram:
Finally, compute the new brightness value using
where is the original brightness value, is the smallest non-zero value of the cumulative distribution function, and is the new brightness value.
To speed up the process, construct a simple look-up table (LUT) with entries. First, compute the transformed brightness value for every possible input value and store the results in the LUT. Then, iterate over the image and replace each brightness value with the corresponding value from the LUT.
Expected Output
Fig. 1: An example of histogram equalization. Notice the input and output histograms and cumulative distribution functions.