DIP+CV · Programming Exercise

EP03_10 — ✨ Unsharp Masking (USM)

3.12.10 EP03_10 ✨ Unsharp Masking (USM)

In digitization systems for historical documents and works of art, image sharpness is essential for reading handwritten texts and ornamental details. Unsharp Masking (USM) is the standard sharpening enhancement algorithm used in professional scanners and software such as Adobe Photoshop, controlled by the parameter \(k\) that determines the enhancement intensity.

See Figure 3.35 for a simulation of this EP.

3.12.10.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows) and \(C\) (columns).
  2. Parameter: Read the real value \(k\) (enhancement intensity, \(k \ge 0\)).
  3. Data: Read the pixel matrix \(f\).
  4. Smoothing: Compute \(\bar{f}\) with a \(3\times3\) averaging filter (internal pixels only; borders preserved):

\[\bar{f}(i,j) = \frac{1}{9} \sum_{s=-1}^{1} \sum_{t=-1}^{1} f(i+s, j+t)\]

  1. High-frequency mask: \(m(i,j) = f(i,j) - \bar{f}(i,j)\).
  2. USM Enhancement: For each internal pixel:

\[g(i,j) = \text{clip}\left(\text{round}\left(f(i,j) + k \cdot m(i,j)\right)\right)\]

  1. Border: \(g(i,j) = f(i,j)\) (direct copy).
  2. Output: Display the enhanced matrix \(L \times C\).

3.12.10.2 📌 Computational Constraints

  • Rounding: Apply round before clipping.
  • Saturation: \(\text{clip}(x) = \max(0, \min(255, x))\).
  • Float operations: Compute \(\bar{f}\) and \(m\) in floating point before rounding the final result.
  • \(k = 0\): No enhancement — the output is identical to the input (except for borders).

3.12.10.3 🧠 Theoretical Foundation

Step Operation Description
1 \(\bar{f} = f * \frac{1}{9}\mathbf{1}_{3\times3}\) Smoothing (low frequencies)
2 \(m = f - \bar{f}\) Mask (high frequencies)
3 \(g = \text{clip}(\text{round}(f + k \cdot m))\) Weighted enhancement

3.12.10.4 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integer \(L\).
  • Line 2: Integer \(C\).
  • Line 3: Real \(k\).
  • Subsequent lines: Elements of the original matrix.

Output:

  • Enhanced matrix \(L \times C\).

3.12.10.5 📌 Examples

Input Output Observation
3
3
0.0
100 100 100
100 100 100
100 100 100
100 100 100
100 100 100
100 100 100
k=0: no enhancement
3
3
1.0
50 50 50
50 200 50
50 50 50
50 50 50
50 255 50
50 50 50
k=1: center pixel enhanced and saturated
✨ Simulator EP03_10: Unsharp Masking (USM) g = f + k · m

Adjust the gain factor k, observe the complete enhancement pipeline (blurring, high-frequency mask) and hover over the result.

Gain factor k: k = 1.0
① Original Image f 5×5 pixel matrix
② Blurred f̄ 3 × 3 average
③ Mask m m = f − f̄ (High Frequencies)
④ Result g = f + 1.0·m Hover to inspect
Legend:
3×3 neighborhood
Central Pixel
Border (Copied)
Positive/Negative Mask
Hover over an inner pixel of the result to trace the full pipeline.
Figure 3.35: EP03_10 Simulator: Unsharp Masking (USM)
%%writefile EP03_10.cpp
// your solution
Overwriting EP03_10.cpp
TestSuite("EP03_10.cpp").run()
✔️ EP03_10.cases already exists in casos/
📋 5 case(s) loaded from casos/EP03_10.cases

🔍 Testing C++: EP03_10.cpp
⚠️ EP03_10.cpp: Empty file (fewer than 3 lines). Tests skipped.