MediumComputer Vision

Histogram Equalization

Computer Vision

Medium

Problem

Histogram equalization improves image contrast by redistributing pixel intensities so they span the full [0, 255] range more uniformly. It is one of the most commonly used image enhancement techniques.

Given a 2D grayscale image with integer pixel values in [0, 255], apply histogram equalization and return the transformed image.

Algorithm

  1. Compute the histogram: count the frequency of each intensity value (0 through 255).
  2. Compute the cumulative distribution function (CDF): cdf[i] = sum of hist[0] through hist[i].
  3. Find cdf_min, the smallest non-zero value in the CDF (the CDF of the darkest pixel that actually appears).
  4. Map each pixel value v to a new value using:

new\_val = round\left(\frac{cdf[v] - cdf_{min}}{total\_pixels - cdf_{min}} \times 255\right)

If all pixels have the same value (total_pixels equals cdf_min), map every pixel to 0.

Theory

Histogram equalization is an image processing technique that adjusts the contrast of an image by redistributing pixel intensity values. The goal is to spread out the most frequent intensity values, effectively increasing the global contrast. This transforms images with narrow intensity ranges into images that utilize the full available range.


Why Histogram Equalization?

Enhance contrast: Images with poor lighting or limited dynamic range become more visually informative.

Standardize images: Different images captured under varying conditions can be normalized to similar intensity distributions.

Preprocessing for analysis: Many computer vision algorithms perform better on contrast-enhanced images.

Reveal hidden details: Features in dark or washed-out regions become visible after equalization.


Understanding Image Histograms

An image histogram shows the distribution of pixel intensities:

X-axis: Intensity values (0 to 255 for 8-bit grayscale)

Y-axis: Number of pixels at each intensity level

Narrow histogram: Low contrast - pixels clustered in small intensity range

Wide histogram: High contrast - pixels spread across full intensity range

Skewed histogram: Image is predominantly dark (left-skewed) or bright (right-skewed)


The Equalization Goal

Transform the histogram to be approximately uniform - each intensity level should have roughly the same number of pixels.

Before: Histogram peaked at certain values, sparse elsewhere

After: Histogram approximately flat across all intensity levels

Effect: Maximizes information entropy of the image


Mathematical Foundation

The transformation is based on the cumulative distribution function (CDF) of pixel intensities.

Step 1 - Compute histogram:

h(k) = \text{number of pixels with intensity } k

For k = 0, 1, 2, ..., L-1 where L is the number of intensity levels (typically 256).

Step 2 - Compute probability distribution:

p(k) = \frac{h(k)}{N}

Where N is the total number of pixels.

Step 3 - Compute cumulative distribution function (CDF):

\text{CDF}(k) = \sum_{j=0}^{k} p(j)

Step 4 - Apply transformation:

s_k = \text{round}((L-1) \cdot \text{CDF}(k))

The new intensity s_k for each original intensity k is the CDF scaled to the output range.


Worked Example

Small image (4x4 pixels, intensities 0-7):

Pixel values: [0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 6, 7]

Step 1 - Compute histogram:

Total pixels N = 16

Step 2 - Compute probabilities:

Step 3 - Compute CDF:

Step 4 - Compute new intensities (L=8):

Transformation mapping: 0→0, 1→1, 2→3, 3→4, 4→5, 5→6, 6→7, 7→7

The intensities are now more spread out across the range.


The CDF as a Transformation Function

The CDF naturally creates a transformation that:

Stretches common intensities: Regions with many pixels (high histogram bars) get mapped to wider output ranges.

Compresses rare intensities: Regions with few pixels get compressed.

Monotonic: The transformation preserves ordering - brighter pixels stay brighter.


Properties of Histogram Equalization

Automatic: No parameters to tune - the algorithm adapts to each image.

Deterministic: Same input always produces same output.

Reversible in theory: The mapping function could be inverted (though information is lost due to rounding).

Global operation: Uses statistics from entire image.


Handling Different Bit Depths

8-bit images: L = 256 intensity levels (0-255)

16-bit images: L = 65536 intensity levels (0-65535)

Floating-point images: Must be quantized or handled differently

The formula scales appropriately with L.


Limitations of Global Histogram Equalization

Over-enhancement: Can amplify noise in uniform regions.

Loss of local contrast: Global statistics may not suit all image regions.

Unnatural appearance: Aggressive equalization can create artificial-looking results.

Background noise amplification: Dark regions with sensor noise become more visible.


Adaptive Histogram Equalization (AHE)

Addresses limitations by applying equalization locally:

Process:

  1. Divide image into small tiles
  2. Equalize each tile independently
  3. Interpolate at tile boundaries to avoid artifacts

CLAHE (Contrast Limited AHE):


Color Image Equalization

For color images, options include:

Equalize each channel independently: Can cause color shifts

Convert to HSV/LAB, equalize intensity only: Preserves color relationships

Equalize luminance in LAB space: Often best results


Computational Considerations

Histogram computation: O(N) where N is number of pixels

CDF computation: O(L) where L is number of intensity levels

Transformation: O(N) - apply lookup table to each pixel

Total complexity: O(N + L), effectively O(N) since typically L << N

Memory: O(L) for histogram and CDF arrays


Relationship to Probability Theory

Histogram equalization performs a probability integral transform:

Original distribution: PDF given by normalized histogram

Target distribution: Uniform distribution

CDF transformation: Maps any distribution to uniform

This is a fundamental technique in probability and statistics for generating uniform random variables from arbitrary distributions.


Where Histogram Equalization Shows Up

Examples

Example 1

Input
image = [[0, 1], [2, 3]]
Output
[[0, 85], [170, 255]]
Explanation
Four equally frequent intensities are spread across the full output range.

Example 2

Input
image = [[100, 100], [100, 100]]
Output
[[0, 0], [0, 0]]

Hints

  1. Build a 256-bin histogram, then convert it into a cumulative count array.
  2. Use the first positive cumulative count as the lower endpoint of the mapping.

Requirements

Constraints

Starter Code

def histogram_equalize(image: list) -> list:
    """
    Returns the histogram-equalized grayscale image.
    """
    # Write code here
    pass

Test Cases

CaseMatches
Sequentialpublic
All samepublic