DIP+CV · Programming Exercise

EP03_01 — ➕ Saturated Addition of a Constant

3.14.1 EP03_01 ➕ Saturated Addition of a Constant

In video surveillance systems, cameras in environments with variable lighting produce underexposed images. Adjusting brightness by saturated addition of a constant is the simplest operation for immediate correction, being applied in real time on embedded camera chips and in preprocessing pipelines of mobile robots.

See Figure 3.26 for a simulation of this EP.

3.14.1.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows) and \(C\) (columns).
  2. Constant: Read the integer \(k\) (value to be added).
  3. Data: Read the integer values of the original matrix row by row.
  4. Mapping: For each pixel \(p\), compute the new value using the equation:

\[p' = \text{clip}(p + k)\]

  1. Output: Display the resulting matrix with dimensions \(L \times C\).

3.14.1.2 📌 Computational Constraints

  • Saturation (Clipping): Values must be confined to the interval \([0, 255]\): \[\text{clip}(x) = \max(0, \min(255, x))\]
  • Type: The final result must be an integer (no decimal places).
  • \(k\) can be negative: negative values darken the image; positive values lighten it.

3.14.1.3 🧠 Theoretical Background

Parameter Type Visual Impact
\(k > 0\) Integer Lightens the image; pixels near 255 saturate to white
\(k < 0\) Integer Darkens the image; pixels near 0 saturate to black
\(k = 0\) Integer Image unchanged

3.14.1.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Integer \(k\).
  • Following lines: Integer elements of the original matrix.

Output:

  • Transformed matrix with \(L\) rows and \(C\) columns, integer values separated by spaces.

3.14.1.5 📌 Examples

Input Output Observation
2
3
50
0 100 200
210 240 255
50 150 250
255 255 255
Saturation at 255 for high pixels
1
4
-30
0 20 200 255
0 0 170 225 Saturation at 0 for low pixels
➕ Simulator EP03_01: Constant Saturated Addition p' = clip(p + k)

Adjust the constant value k to observe the image brightness shift and saturation truncation in the range [0, 255].

0
Original Input (p)
Transformed Result (p')
Formula applied: clip(p + (0))
Figure 3.26: Simulador EP03_01: Saturated Addition of Constant (p’ = clip(p + k))
%%writefile EP03_01.py
# Python code
Overwriting EP03_01.py
TestSuite("EP03_01.py").run()
✔️ EP03_01.cases already exists in casos/
📋 5 case(s) loaded from casos/EP03_01.cases

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