Histogram Matching

How to generate a histogram for an image, how to equalize the histogram, and finally how to modify your image histogram to be similar to another histogram.

Ali Pourramezan Fard
Towards Data Science

--

The code is available here on Github.

Please clap if you like the post.

What is an image histogram?

Before start defining the histogram, for simplicity, we use grayscales images. Then later I explain the process for the color images as well.

The image histogram indicates the intensity distribution of an image. In other words, the image histogram shows the number of pixels in an image having a specific intensity value. As an example, assume a normal image with pixel intensities varies from 0 to 255. In order to generate its histogram we only need to count the number of pixels having intensity value 0, then 1 and continue to the 255. In Fig.1, we have a sample 5*5 image with pixel diversities from 0 to 4. In the first step for generating the histogram, we create the Histogram Table, by counting the number of each pixel intensities. Then we can easily generate the histogram by creating a bar chart based on the histogram table.

Figure 1: The process of generating an image histogram

How to generate an image histogram?

In python, we can use the following two functions to create and then display the histogram of an image.

Code 1: Generating Histogram

Most of the time when we create a histogram, we normalize the histogram by dividing the number of pixels with each intensity value by the normalizing factor which is the multiplication of the image width and image height. For ease of use, if the input image of the generate_histogram function is a color image, we first convert to a grayscale image(see line# 6).

How to equalize an image histogram?

Histogram equalization is commonly used in order to enhance the contrast of the image. Accordingly, this technique can’t guarantee to always improve the quality of the image. Calculating CDF (cumulative distributive function) is a common way to equalizing the histogram of an image. In Fig.2, we have calculated the CDF for the sample image that we created in Fig.1. Furthermore, in Fig.3 we show the equalized histogram of the previous sample.

Figure 2: Calculating CDF.
Figure 3: Equalized Histogram.

In order to calculate the equalized histogram in python, I have created the following codes:

Code 2: Equalizing Histogram

Here is 3 different image taken by me and used as examples. As shown in Fig.4, for the first image the histogram shows that the number of pixels with low intensity is more than the brighter pixels. The situation is totally reverse for the second image, where the density of the brighter pixels is much more than the darker ones. The third image seems to have a semi-normal histogram.

Figure 4: Three different types of images and their histograms as well as the equalized histograms.

Enhancing images using equalized histogram

As mentioned, we can modify the contrast level of an image using its equalized histogram. As shown in Code.2, line #12, for each pixel in an input image, we can use its equalized value. The results might be better than the original image, but it is not guaranteed. In Fig.5, we depict the modified version of the 3 images. As shown, modifying the images using their equalized histogram results in images with a higher level of contrast. This feature can be useful in many computer vision tasks.

Figure 5: Contrast modification using the equalized histogram. The leftmost column is the original image. The middle column is the result of the contrast modification. The rightmost column is the histogram of the modified images.

What is the histogram matching?

Assume we have two images and each has its specific histogram. So we want to answer this question before going further, is it possible to modify one image based on the contrast of another one? And the answer is YES. In fact, this is the definition of the histogram matching. In other words, given images A, and B, it is possible to modify the contrast level of A according to B.

Histogram matching is useful when we want to unify the contrast level of a group of images. In fact, Histogram equalization is also can be taken as histogram matching, since we modify the histogram of an input image to be similar to the normal distribution.

In order to match the histogram of images A and B, we need to first equalize the histogram of both images. Then, we need to map each pixel of A to B using the equalized histograms. Then we modify each pixel of A based on B.

Let’s clarify the above paragraph using the following example, in Fig.6.

Figure 6: Histogram Matching

In Fig.6, we have image A as the input image and Image B as the target image. We want to modify the histogram of A, based on the distribution of B. In the first step, we calculate both histogram and the equalized histogram of both A, and B. Then we need to map each pixel of A, based on the value of its equalized histogram to the value of B. So, for example for pixels with the intensity level of 0 in A, the corresponding value of A equalized histogram is 4. Now, we take a look at the B equalized histogram and find the intensity value corresponding to 4, which is 0. So we map the 0 intensity from A to 0 from B. We continue for all intensity values of A. If there is no map from the equalized histogram of A to B, we just need to pick the nearest value.

I have implemented the above process in Python as well

Code 3: Histogram Matching in Python
Figure 7: Histogram matching example. We modified the left image’s histogram to match the center image’s histogram.

Fig.7 shown an example of histogram matching. As you see, while the leftmost image is a bright image, the center image can be considered a better image in terms of the contrast level. So, we decided to modify the leftmost using the contract of the center image. The result, which is the rightmost image has been improved.

Conclusion

In this article, I explained histogram matching which is a useful method while we cope with the images. I first started by explaining how to generate an image histogram. Then, how to equalize the generated histogram, and finally how to modify a picture based on the contrast level of another picture, called histogram matching. The code with the explanation is also available on Github.

The code is available here on Github.

Please clap if you like the post.

--

--