For each pixel in the image, the median filter:
- Collects all pixel values within the defined neighborhood (square or circular window)
- Sorts these values in ascending order
- Replaces the center pixel with the median (middle) value
Because the output is always an actual pixel value from the neighborhood — never an interpolated average — the filter preserves edges remarkably well. The key insight: the median is the value that minimizes the sum of absolute differences to all other values in the set, making it the optimal estimate under impulsive noise.
Properties:
- Idempotent for binary images — applying the median filter twice to a binary image gives the same result as applying it once
- Edge preservation — step edges survive because the majority vote on each side of the edge determines the output
- Corner rounding — sharp corners (structures smaller than the kernel) get rounded, similar to morphological opening