Grayscale histogram equalization

Using the cv2.equalizeHist() function with the purpose of equalizing the contrast of a given grayscale image is pretty easy:

image = cv2.imread('lenna.png')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray_image_eq = cv2.equalizeHist(gray_image)

In the grayscale_histogram_equalization.py script, we apply histogram equalization to three images. The first one is the original grayscale image. The second one is the original image but modified, in the sense that we have added 35 to every pixel of the image. The third one is the original image but modified, in the sense that we have subtracted 35 from every pixel of the image. We have also calculated histograms before and after the equalization of the histogram. Finally, all of these images are plotted. The output of this script can be seen in the following screenshot:

In the previous screenshot, we can see that the three equalized images are really very similar and this fact can also be reflected in the equalized histograms, where all three of them are also very similar. This is because histogram equalization tends to normalize the brightness (and also increase the contrast) of images.