Histogram Equalization is used in image processing to “enhance” a grayscale image by increasing its contrast and often used in images where both foreground and background are dark or light.
The image might actually look bad photography wise but makes it better for scientific purposes like thermal, xrays, satellite images.
An image histogram is a graph of the tonal distribution (the lightness) of an image.
What histogram equalization does is to try to make the stuff in the graph above more spread out instead of the clump we see right now that’s in the middle.
How to do it
Wikipedia has a link to some pseudo-code in the “External Links” area of their page on the same subject. Here I will just show the basic steps:
You will need the grayscale image in matrix form
- Count total number of pixels in a certain intensity, for all intensities.
- Get the pixel probability (number of pixels in that intensity / total number of pixels in image)
- Get cumulative pixel probability (copy first probability then add the next probability. Keep doing this for all probabilities, like 0.6 = 0.6, 0.6 + 0.25 = 0.85, 0.85 + 0.03 = 0.87… until it is = 1)
- Multiply the cumulative probability of a pixel by the maximum intensity
- Round up or round down to make the new cumulative probability a whole number
- Map these new values to a new image matrix
The pixels should be evenly distributed through the intensity range. It should look something like this