DIP+CV · Programming Exercise

EP02_03 — 🎨 Gray Level Quantization

2.12.3 EP02_03 🎨 Gray Level Quantization

In this activity, you must implement uniform quantization of an image, reducing the number of original gray intensity levels to a new scale based on a smaller number of bits.

  • Read two integers L and C, representing the dimensions of the matrix.
  • Read an integer \(k\) (\(1 \le k \le 8\)), representing the new number of bits of the image.
  • Calculate the number of levels (\(N = 2^k\)) and the interval size (step).
  • For each pixel \(p\), calculate the new value \(p'\) by mapping it to the index of the corresponding discretized level (ranging from \(0\) to \(2^k-1\)).
  • Print the resulting matrix with the same original dimension values.
  • See Figure 2.14 for a simulation of this EP.

📌 Important:

  • Posterization: When drastically reducing the number of levels (e.g., \(k=2\)), you will notice that smooth gradients become abrupt color bands due to the loss of amplitude resolution.
  • Step Calculation: The interval between each level is defined by \(step = 256 / 2^k\).
  • Mapping: The uniform quantization method by truncation that maps the pixel to the index of its respective discretized level is given by:

\[p' = \left\lfloor \frac{p}{step} \right\rfloor\]

In terms of implementation (as in Python), this is equivalent to integer division: p' = p // step.

2.12.3.1 🧠 Amplitude Discretization

While subsampling deals with spatial resolution, quantization focuses on the precision of color (amplitude). Reducing bits means simplifying the chromatic information:

Parameter Function Effect
Bits (\(k\)) Color depth Defines how many different tones the image can have (\(2^k\)).
Step Tone interval Spacing between the allowed gray levels.
Posterization Visual phenomenon Transformation of continuous variations into blocks of solid color.

2.12.3.2 📋 Task (VPL specification)

Input:

The first line contains L.

The second line contains C.

The third line contains the number of bits k.

The following lines contain the elements of the \(L \times C\) matrix.

Output:

The transformed matrix with the indices of the quantized levels, maintaining the original size \(L \times C\).

2.12.3.3 📌 Examples

Input Output Observation
1
4
2
0 80 170 255
0 1 2 3 With \(k=2\), we have \(2^2=4\) discrete levels available (\(0,1,2,3\)). The step is \(256/4=64\). Applying integer division element-wise: \(0 // 64 = 0\), \(80 // 64 = 1\), \(170 // 64 = 2\), \(255 // 64 = 3\).
1
5
1
10 50 120 200 250
0 0 0 1 1 With \(k=1\), we have \(2^1=2\) levels (\(0\) and \(1\)). Step \(=256/2=128\). Pixels less than \(128\) result in \(0\), and pixels greater than or equal to \(128\) result in \(1\).
🎚️ EP02_03 Simulator: Quantization and Bit Depth q = round(p · (L − 1) / 255)

Adjust the number of output bits (b) to observe the mapping of the 256 continuous gray levels to L = 2ᵇ discrete quantization levels.

8
Discrete levels (L = 2ᵇ): 256  |  Values shown: 0 a 255
Original (8 bits → 0…255)
Quantized (Range 0…255)
Output bits = 8 → 256 levels (original values preserved)
Figure 2.14: EP02_03 Simulator: Quantization and Bit Depth (Reduction of Gray Levels)
%%writefile EP02_03.py
# Python Code
Overwriting EP02_03.py
TestSuite("EP02_03.py").run()
✔️ EP02_03.cases already exists in casos/
📋 5 case(s) loaded from casos/EP02_03.cases

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