Skip to content

Median filter

Fact Value
Also called Per-channel median filter
Status Implemented and tested
Family Nonlinear spatial filters
Engine Browser
Type Nonlinear, spatial, order statistic
Output Image
Operator shown as rank (exact)
Support radius floor(size / 2)
Code web/src/core/filters/nonlinear/median.ts, medianFilter
Operator code web/src/core/filters/nonlinear/median.ts, medianWindow
Channels RGB, Brightness only

Overview

The median filter replaces each pixel with the middle value of its window. One very bright or very dark pixel cannot pull the result, because it ends up at the end of the sorted list. This is why it removes salt-and-pepper noise and keeps edges sharp.

Three panels: a gray ramp with salt-and-pepper noise, the result of a 3x3 median, and the result of a 3x3 box filter. Three panels: a gray ramp with salt-and-pepper noise, the result of a 3x3 median, and the result of a 3x3 box filter.
A 3x3 median removes salt-and-pepper noise. A 3x3 box filter smears it.

How it works

  1. Take the square window around the pixel.
  2. Sort its values from dark to bright.
  3. Pick the value in the middle of the list.
  4. Write it as the new pixel value.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \operatorname{median}\,\{\, I(x+i,\, y+j) \;:\; -r \le i \le r,\; -r \le j \le r \,\}, \qquad r = (3 - 1) / 2 \]

The formula shows the default parameter values.

where

  • \(I\): Input channel value
  • \(I_{\text{out}}\): Output channel value
  • \((x, y)\): Pixel position
  • \(r\): Window radius in pixels

The filter collects the window values, sorts them and takes the middle one. The window size is odd, so there is always exactly one middle value and it is one of the input values.

The median is an order statistic. It has no kernel and is not a weighted sum, so the filter is nonlinear. The window size is odd, so there is exactly one middle value, and it is always one of the input values. A noise value can only win when more than half of the window is noise.

Kernel or operator

The Inspector shows the window size, the number of values and the rank of the chosen value. For a 3x3 window that is the 5th of 9 values. The Rank stepper in the Learn panel shows the sorted list at any pixel.

Algorithm

  1. Pad the image with the border mode.
  2. For each pixel and channel, collect the values in the size by size window.
  3. Find the middle value with a sliding 256-bin histogram.
  4. Write it to the output pixel.

Parameters

Parameter Default Values Meaning
Window size size 3 3 to 25, odd px Width and height of the square window.
Border mode border Reflect 101 (default) Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap How pixels outside the image are filled when the window reaches past the edge.
Channels channelScope RGB RGB, Brightness only Which part of the color image the filter changes.

Implementation

The median comes from a sliding 256-bin histogram (Huang, Yang and Tang). Moving one pixel to the right removes one column of the window and adds one, so the cost per pixel grows with the window width, not its area. With the brightness scope, luma is rounded to whole values first. The filter runs in the browser engine, in Web Workers. The studio splits the image into strips. Each strip gets a halo as wide as the support radius, so the strips give exactly the pixels of a whole-image run. The shared filter framework in web/src/core/filters/run.ts handles the border mode, the channel scope, alpha and the final rounding to 8 bits.

Worked example

\[ W = \begin{bmatrix} 12 & 15 & 14 \\ 13 & 255 & 16 \\ 11 & 14 & 13 \end{bmatrix} \]

Sorted: 11, 12, 13, 13, 14, 14, 15, 16, 255. The middle value, the 5th of 9, is 14. The salt pixel 255 moved to the end of the list and was not chosen. The mean of the same window is 40.33, pulled up by that one outlier.

Complexity

  • Time: \(O(N\,k)\)
  • Memory: \(O(N)\)

N is the number of pixels per channel and k is the window width. The sliding histogram method of Huang, Yang and Tang updates one column of k values per step. Sorting every window would cost O(N\,k^2 \log k).

Artifacts and limitations

Limitations

  • Rounds off corners. Removes lines thinner than half the window.
  • Weaker than linear smoothing on Gaussian noise.
  • Large windows turn texture into flat patches.

Artifacts

  • Blotches and streaks with large windows.
  • Color fringes at color edges, because channels are filtered separately.

Tests

The filter is compared with scipy.ndimage.median_filter for window sizes 3, 5 and 7 and all five border modes. The results match exactly.

References