Type: Non-Linear Digital Filter
Library: OpenCV (cv2.medianBlur)
Application: Salt-and-Pepper Noise Removal
The Median Filter is a non-linear digital filtering technique, often used to remove noise from an image or signal. Unlike linear filters (like Gaussian or Box Blur) which calculate the average, the Median filter replaces a pixel with the median value of its neighbourhood. This preserves edges while eliminating outliers.
For a given pixel at location (x, y), we consider a neighbourhood w centered around it. The new intensity value I'(x, y) is calculated by sorting all pixel intensities in the window and picking the middle one.
Because the median is a robust statistic, unrepresentative pixel values (like extreme black or white dots caused by sensor noise) are effectively ignored, rather than being averaged into the result.
The implementation is as follows . The kernel size (k) determines the size of the neighbourhood.
Constraint: The kernel size must always be an odd integer (e.g., 3, 5, 7) to ensure there is a single central pixel to replace.
A naive implementation of a median filter requires sorting the values in the k × k window for every pixel. This would result in a complexity of:
However, OpenCV uses an optimized Histogram-based approach (for 8-bit images) or Huang's Algorithm, which brings the complexity down to O(r) or even O(1) relative to the window size, allowing for real-time processing on the server.
Try the Denoising Tool:
Launch Live Tool