DIP+CV · Programming Exercise

EP03_07 — 🔍 Laplacian Operator (w4) for Edge Enhancement

3.14.7 EP03_07 🔍 Laplacian Operator (w4) for Edge Enhancement

In high-resolution tomography, the sharpness of edges between tissues is critical for diagnosis. The Laplacian operator is widely used in medical image preprocessing pipelines to automatically enhance anatomical contours before segmentation, avoiding manual intervention by the radiologist.

See Figure 3.32 for a simulation of this EP.

3.14.7.1 📋 Implementation Guidelines

  1. Dimensions: Read the integers \(L\) (rows) and \(C\) (columns).
  2. Data: Read the pixel matrix \(f\).
  3. Laplacian (w4): For each internal pixel \((i,j)\) with \(1 \le i < L-1\), \(1 \le j < C-1\), compute:

\[\nabla^2 f(i,j) = f(i-1,j) + f(i+1,j) + f(i,j-1) + f(i,j+1) - 4 \cdot f(i,j)\]

  1. Enhancement: Compute the enhanced image:

\[g(i,j) = \text{clip}(f(i,j) - \nabla^2 f(i,j))\]

  1. Border: Border pixels are copied directly: \(g(i,j) = f(i,j)\).
  2. Output: Display the enhanced matrix \(L \times C\).

3.14.7.2 📌 Computational Constraints

  • Kernel w4: \(\begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{bmatrix}\) — only 4-neighbors.
  • Saturation: \(\text{clip}(x) = \max(0, \min(255, x))\) applied to the enhancement result.
  • No rounding: The Laplacian uses only integer additions/subtractions.

3.14.7.3 🧠 Theoretical Background

Region \(\nabla^2 f\) Enhancement Effect
Uniform \(\approx 0\) No change
Rising edge \(< 0\) Pixel lightened
Falling edge \(> 0\) Pixel darkened

3.14.7.4 📦 Input and Output Specification (VPL)

Input:

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

Output:

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

3.14.7.5 📌 Examples

Input Output Observation
3
3
0 0 0
0 100 0
0 0 0
0 0 0
0 255 0
0 0 0
Isolated peak: lap=−400, g=100−(−400)=500 → clip=255
3
3
50 50 50
50 50 50
50 50 50
50 50 50
50 50 50
50 50 50
Uniform region: Laplacian=0, no change
📐 Simulator EP03_07: Laplacian Operator (w4) g = f ∓ ∇²f

Select the enhancement variant and hover over the inner pixels of the result to inspect the 4-point neighborhood and the Laplacian equation.

Variant:
① Original Image f Step with light noise
② Laplacian ∇²f Detected edges (±128 shift)
③ Result g = f − ∇²f Hover to inspect
Kernel w4 (4-Neighbor)
0
+1
0
+1
−4
+1
0
+1
0
∇²f = T + B + L + R − 4·f
Legend:
Kernel 4-Neighbors
Central Pixel
Border (Copied)
Hover over an inner pixel of the result to detail the equation.
Figure 3.32: Simulator EP03_07: Laplacian Operator (w4) for Edge Enhancement
%%writefile EP03_07.py
# Python Code
Overwriting EP03_07.py
TestSuite("EP03_07.py").run()
✔️ EP03_07.cases already exists in casos/
📋 5 case(s) loaded from casos/EP03_07.cases

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