DIP+CV · Programming Exercise

EP03_04 — 📊 Histogram Equalization (L bits)

3.14.4 EP03_04 📊 Histogram Equalization (L bits)

In remote sensing satellite images, variation in illumination throughout the day produces low-contrast images. Histogram equalization is automatically applied in satellites such as Landsat to redistribute tones, revealing details of vegetation, relief, and urban areas that are invisible in the original image.

See Figure 3.29 for a simulation of this EP.

3.14.4.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows), \(C\) (columns), and \(B\) (number of bits, with \(L_{\max} = 2^B\)).
  2. Data: Read the pixel matrix \(f\) with values in \([0, 2^B - 1]\).
  3. Histogram: Compute \(h[k]\) = number of pixels with intensity \(k\), for \(k = 0 \ldots 2^B-1\).
  4. Probability: \(p[k] = h[k] / (L \cdot C)\).
  5. CDF: \(\text{cdf}[k] = \sum_{j=0}^{k} p[j]\); cumulative distribution function.
  6. LUT: \(\text{lut}[k] = \text{round}\left(\text{cdf}[k] \cdot (2^B - 1)\right)\); Look-Up Table.
  7. Application: \(g[i,j] = \text{lut}[f[i,j]]\).
  8. Output: Display the equalized matrix of size \(L \times C\).

3.14.4.2 📌 Computational Constraints

  • Rounding: Use mathematical rounding (round) in the LUT.
  • Bits: The number of levels is \(2^B\) (e.g., \(B=3 \Rightarrow 8\) levels, \(B=8 \Rightarrow 256\) levels).
  • Cumulative CDF: \(\text{cdf}[k] = \sum_{j=0}^{k} p[j]\), with \(\text{cdf}[2^B-1] = 1.0\).

3.14.4.3 🧠 Theoretical Foundation

Step Operation Formula
1 Histogram \(h[k] \leftarrow\) number of pixels with intensity \(k\)
2 Probability \(p[k] = h[k] / (L \cdot C)\)
3 CDF \(\text{cdf}[k] = \sum_{j=0}^{k} p[j]\)
4 LUT \(\text{lut}[k] = \text{round}(\text{cdf}[k] \cdot (2^B-1))\)
5 Application \(g[i,j] = \text{lut}[f[i,j]]\)

3.14.4.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Integer \(B\) (number of bits).
  • Following lines: Integer elements of the matrix.

Output:

  • Equalized matrix with \(L\) rows and \(C\) columns.

3.14.4.5 📌 Examples

Input Output Observation
5
5
3
3 4 2 3 4
4 3 3 4 3
2 3 4 3 2
3 4 3 2 3
4 3 2 3 4
5 7 1 5 7
7 5 5 7 5
1 5 7 5 1
5 7 5 1 5
7 5 1 5 7
Example with 3 bits from the chapter
1
4
3
0 0 7 7
0 0 7 7 Extreme bimodal histogram
📊 Simulator EP03_04: Histogram Equalization lut[k] = round(cdf[k] · (L − 1))

Choose the bit depth (B) and generate images to analyze the dynamic spreading of the histogram and the remapping table (LUT) in real time.

3 bits → 8 levels
1 bit (2 levels) 4 bits (16 levels) 8 bits (256 levels)
Original Input
Equalized Result
Original Histogram
Equalized Histogram
LUT (Remapping Table k → v)
lut[k] = round(cdf[k] · 7) | B=3, levels=8
Figure 3.29: Simulator EP03_04: Histogram Equalization (Levels L = 2^B)
%%writefile EP03_04.py
# Python code
Overwriting EP03_04.py
TestSuite("EP03_04.py").run()
✔️ EP03_04.cases already exists in casos/
📋 5 case(s) loaded from casos/EP03_04.cases

🔍 Testing Python: EP03_04.py
⚠️ EP03_04.py: Empty file (fewer than 3 lines). Tests skipped.