What is Histogram Equalization?

Histogram Equalization is an image processing technique that aims to improve the contrast of an image by redistributing its pixel intensity values. It attempts to “spread out” the most frequent intensity values, effectively stretching the dynamic range of the image.


Why We Need Histogram Equalization

  1. Enhanced Contrast: The primary reason is to improve the overall contrast of an image, making details more visible. Images with low contrast often appear washed out or too dark.

  2. Better Visualization: By enhancing contrast, equalization can make it easier for both humans and computer algorithms to analyze images.

  3. Preprocessing Step: It’s often used as a preprocessing step for other image processing or computer vision tasks, such as object detection or recognition, as better contrast can lead to improved performance.

  4. Correcting Uneven Lighting: Equalization can help to mitigate the effects of uneven lighting conditions in an image.


Derivation of Histogram Equalization

Pixel Value (rk)Number of Pixels (nk)Probability of rk (pr(rk))Cumulative Probability (Pr(rk))Output Intensity (sk)
0n0pr(r0) = n0/nPr(r0) = pr(r0)s0 = (L-1) * Pr(r0)
1n1pr(r1) = n1/nPr(r1) = pr(r0) + pr(r1)s1 = (L-1) * Pr(r1)
2n2pr(r2) = n2/nPr(r2) = pr(r0) + pr(r1) + pr(r2)s2 = (L-1) * Pr(r2)
knkpr(rk) = nk/nPr(rk) = Σ pr(rj) (from j=0 to k)sk = (L-1) * Pr(rk)
L-1nL-1pr(rL-1) = nL-1/nPr(rL-1) = Σ pr(rj) (from j=0 to L-1)sL-1 = (L-1) * Pr(rL-1)
totaln
  • Pixel Value (rk): Represents the possible intensity levels in the image, ranging from 0 to L-1.
  • Number of Pixels (nk): The number of pixels in the image that have the intensity value rk.
  • Probability of rk (pr(rk)): The probability of a pixel having intensity rk. It’s calculated as nk (number of pixels with intensity rk) divided by n (total number of pixels).
  • Cumulative Probability (Pr(rk)): The cumulative probability up to intensity level rk. It’s the sum of probabilities of all intensity levels from 0 up to rk.
  • Output Intensity (sk): The new, equalized intensity value. It’s calculated by multiplying the cumulative probability Pr(rk) by (L-1), where L is the total number of intensity levels. The result s_k is usually rounded to the nearest integer.

Example of histogram equalization

Okay, let’s combine all the steps of the histogram equalization process for the given image data into a single, comprehensive table:

Image Data:

2 3 3 2 
0 2 2 3 
1 5 6 1 
1 1 5 7

Combined Histogram Equalization Table:

Pixel Value (rk)No. of Pixels (nk)pr(rk) = nk/nCumulative Probability (Pr(rk))Output sk = (L-1) * Pr(rk)Rounded sk
011/16 = 0.06250.06257 * 0.0625 = 0.43750
144/16 = 0.250.0625 + 0.25 = 0.31257 * 0.3125 = 2.18752
244/16 = 0.250.3125 + 0.25 = 0.56257 * 0.5625 = 3.93754
333/16 = 0.18750.5625 + 0.1875 = 0.757 * 0.75 = 5.255
400/16 = 00.75 + 0 = 0.757 * 0.75 = 5.255
522/16 = 0.1250.75 + 0.125 = 0.8757 * 0.875 = 6.1256
611/16 = 0.06250.875 + 0.0625 = 0.93757 * 0.9375 = 6.56257
711/16 = 0.06250.9375 + 0.0625 = 1.07 * 1.0 = 77

Explanation:

  • n (total number of pixels) = 16
  • L (total intensity levels) = 8
  • The table now combines all the intermediate calculations:
    • Number of pixels for each intensity level.
    • Probability of each intensity level.
    • Cumulative probability.
    • Output intensity level before rounding.
    • Final rounded output intensity level.

Equalized Image: Using the “Rounded sk” column, we replace the original pixel values:

Original Image:

2 3 3 2 
0 2 2 3 
1 5 6 1 
1 1 5 7

Equalized Image:

4 5 5 4 
0 4 4 5 
2 6 7 2 
2 2 6 7

Why This Transformation Works:

The CDF represents the cumulative probability up to a certain intensity level. By using the CDF as the transformation, we are essentially mapping the intensity levels in a way that is proportional to their cumulative frequency. This tends to spread out the intensity values, particularly in regions where the original histogram has large peaks (high frequency of certain intensity values).


What is Entropy in Computer Vision?

In computer vision, entropy is a measure of the randomness or information content in an image. It quantifies the average level of uncertainty associated with the pixel intensities.

  • High Entropy: An image with high entropy has a wide range of pixel intensities distributed relatively evenly. This often corresponds to images with high contrast and a lot of detail.
  • Low Entropy: An image with low entropy has a narrow range of pixel intensities, often concentrated around a few values. These images tend to be low-contrast and may appear dull or lacking in detail.

Formula of Entropy (for an image): The entropy (H) of an image is calculated using the following formula: Where:

  •  is the probability of a pixel having intensity value i. It’s calculated by dividing the number of pixels with intensity i by the total number of pixels.
  • The summation is over all possible intensity values (e.g., 0 to 255 for an 8-bit grayscale image).
  •  is the base-2 logarithm.

Effect of Entropy on Histogram Equalization

  • Entropy Increase: Histogram equalization generally increases the entropy of an image. By spreading out the pixel intensities, it makes the distribution more uniform, leading to higher randomness and information content.

  • Content Quality: While increasing entropy often improves the visual quality by enhancing contrast, it’s not always a direct correlation. There can be cases where increasing entropy might introduce noise or make certain details less discernible.

  • Relationship: Histogram equalization aims to achieve a more uniform histogram, which often corresponds to higher entropy. However, the primary goal is contrast enhancement, and entropy is a related but not always the primary indicator of quality.


Why We Need Histogram Equalization?

  • Contrast Enhancement: The primary reason to use histogram equalization is to improve the contrast of an image, making details more visible, especially in images with low contrast or a narrow range of intensities.
  • Visual Improvement: It can make images more visually appealing and easier to interpret.
  • Preprocessing: Histogram equalization can serve as a preprocessing step for other image processing tasks, like feature extraction or object detection, where good contrast can improve performance.
  • Highlighting Details: In medical imaging or satellite imagery, histogram equalization can reveal subtle details that might be difficult to see in the original image.
  • Standardization: It can be used to standardize images, making them more consistent across different datasets or acquisition conditions.

Why We Don’t Need Histogram Equalization?

  • Already Good Contrast: If an image already has good contrast and a wide range of intensities, histogram equalization might not provide significant improvement and could even degrade the quality.
  • Noise Amplification: In images with a lot of noise, histogram equalization can amplify the noise, making it more noticeable.
  • Unnatural Appearance: Sometimes, histogram equalization can make images look unnatural or artificial, especially if the intensity distribution is altered too drastically.
  • Specific Applications: In some applications, preserving the original intensity distribution is crucial. For example, in scientific imaging, absolute intensity values might be important for analysis, and histogram equalization would distort that information.
  • Computational Cost: While not extremely computationally intensive, histogram equalization does require some processing. In real-time applications or resource-constrained environments, it might not be necessary or feasible.
  • Loss of Information: Although it often enhances visual information, in some cases, histogram equalization might lead to loss of fine details or subtle variations in intensity.
  • Color Distortion: When applied to color images, histogram equalization is often done independently on each color channel. This can lead to color shifts or distortions because the relationship between the channels might be altered.

Output Distribution of Transformation After Histogram Equalization

Ideally, after histogram equalization, the output distribution of pixel intensities should be approximately uniform.

  • Uniform Histogram: The transformed histogram should be relatively flat, meaning that each intensity level has roughly the same number of pixels.
  • Linear CDF: The CDF of the equalized image should be approximately linear, indicating an even distribution of intensity values across the entire range.