📖 Neighborhood (Mask) Operation: The output at pixel (x,y) depends on the values of f in a small NEIGHBORHOOD (e.g. 3×3 window) surrounding (x,y), not just f(x,y) alone.
📖 Median Filter: A non-linear neighborhood filter that replaces each pixel's value with the MEDIAN (middle value when sorted) of the intensities in its neighborhood — excellent for removing "salt-and-pepper" (impulse) noise while preserving edges.
Worked Example — 3×3 Median Filter:
Original 3×3 neighborhood (center pixel has noise = 255):
10 12 11
13 255 14 ← center pixel corrupted (salt noise)
12 11 13
Fig: 3×3 Neighborhood with Noisy Center Pixel
Step 1: Collect all 9 values:
10, 12, 11, 13, 255, 14, 12, 11, 13
Step 2: Sort them in ascending order:
10, 11, 11, 12, 12, 13, 13, 14, 255
Step 3: Find the MIDDLE (5th) value:
10, 11, 11, 12, [12], 13, 13, 14, 255
↑
This is the MEDIAN
Step 4: Replace center pixel with median = 12
✅ Result: The noisy pixel (255) is replaced by 12 — matching its neighbors closely, effectively removing the noise spike WITHOUT blurring the surrounding edges (unlike a simple averaging/mean filter, which would still be dragged toward 255).
| Feature | Mean (Averaging) Filter | Median Filter |
| Type | Linear | Non-linear |
| Best for | Gaussian noise | Salt-and-pepper (impulse) noise |
| Edge Preservation | Blurs edges | Preserves edges well |
| Effect of extreme outlier | Drags average toward outlier | Outlier gets discarded (not the median) |
💡 Exam Tip: Median filter numericals always follow the SAME 4 steps — collect neighborhood values, sort them, pick the middle one (median), replace the center pixel. For a 3×3 window (9 values), the median is always the 5th value after sorting.