Synthetic aperture radar (SAR) images used in environmental and military monitoring suffer from a specific type of noise called speckle, which has characteristics similar to salt-and-pepper noise. The median filter is the standard method for removing this noise because it preserves the edges of structures while eliminating spurious points.
See Figure 3.34 for a simulation of this exercise.
3.14.9.1 📋 Implementation Guidelines
Dimensions: Read the integers \(L\) (rows) and \(C\) (columns).
Data: Read the pixel matrix \(f\).
3×3 Median Filter: For each internal pixel \((i,j)\) with \(1 \le i < L-1\), \(1 \le j < C-1\):
Collect the 9 pixels from the \(3 \times 3\) neighborhood: \(\{f(i+s, j+t) : s,t \in \{-1,0,1\}\}\).
Sort the 9 values in ascending order.
Assign \(g(i,j)\) to the central value (index position 4, considering index 0).
Output: Display the filtered \(L \times C\) matrix.
3.14.9.2 📌 Computational Constraints
Window: Always \(3 \times 3 = 9\) elements.
Median: The central element of the sorted sequence (index 4 from 0 to 8).
No clipping: The median of values in \([0, 255]\) remains in \([0, 255]\).
Nonlinear: The median filter cannot be expressed as a linear convolution.
3.14.9.3 🧠 Theoretical Background
Noise
Mean Filter
Median Filter
Salt and pepper (0 or 255)
Spreads the noise
Removes without distorting edges
Gaussian
Reduces effectively
Reduces partially
3.14.9.4 📦 Input and Output Specification (VPL)
Input:
Line 1: Integer \(L\).
Line 2: Integer \(C\).
Following lines: Matrix elements.
Output:
Filtered \(L \times C\) matrix.
3.14.9.5 📌 Examples
Input
Output
Observation
3
3
100 100 100
100 0 100
100 100 100
100 100 100
100 100 100
100 100 100
Black point removed: median of 8×100+1×0 = 100
3
3
50 50 50
50 255 50
50 50 50
50 50 50
50 50 50
50 50 50
White point (salt) removed
📉 Simulator EP03_09: 3×3 Median Filterg = Median(Neighbors)
Inject impulsive noise (salt and pepper) and hover over internal pixels of the result to inspect the sorting of the neighborhood vector and the noise removal.
Salt (255) and pepper (0) noise — ~30% of internal pixels affected
Image f — With NoiseSalt (255) and pepper (0) visible
Result g — Without NoiseHover to inspect
3×3 Neighborhood Vector — SortedHover over an internal pixel of the result to visualize
—
Legend:
3×3 Window
Central Pixel
Border (Copied)
Median
Noise (Removed)
Hover over an internal pixel of the result to see the sorting process.
Figure 3.34: EP03_09 Simulator: 3×3 Median Filter
%%writefile EP03_09.py# Python code
Overwriting EP03_09.py
TestSuite("EP03_09.py").run()
✔️ EP03_09.cases already exists in casos/
📋 5 case(s) loaded from casos/EP03_09.cases
🔍 Testing Python: EP03_09.py
⚠️ EP03_09.py: Empty file (fewer than 3 lines). Tests skipped.