Skip to content

Switching median filter

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

Overview

The switching median filter repairs only the pixels that look like impulses. It compares each pixel with the median of its window. When the difference is above a threshold, the pixel is replaced by the median. Otherwise it keeps its exact value. Thin lines and texture that a median filter would blur stay as they are.

Three panels: a Siemens star with salt-and-pepper noise, a 3x3 median that removes the noise and blurs the spokes, and the switching median with threshold 40 that removes the noise and keeps the spokes. Three panels: a Siemens star with salt-and-pepper noise, a 3x3 median that removes the noise and blurs the spokes, and the switching median with threshold 40 that removes the noise and keeps the spokes.
The switching median repairs the specks and keeps the spokes, because it changes only pixels far from the median.

How it works

  1. Find the median of the window around the pixel.
  2. Measure how far the pixel is from that median.
  3. If it is further than the threshold, write the median. Otherwise keep the pixel.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \begin{cases} m(x,y) & |I(x,y) - m(x,y)| > T \\ I(x,y) & \text{otherwise} \end{cases}, \qquad T = 40 \]

The formula shows the default parameter values.

where

  • \(m(x, y)\): The median of the window around the pixel
  • \(T\): The detection threshold

m is the median of the window. A pixel far from the median of its neighbors is taken as an impulse and replaced by that median. Pixels close to it keep their own value. This switching between the median and the input is the idea of Sun and Neuvo (1994).

The filter switches between two outputs, the median and the input, based on an impulse detector. Sun and Neuvo (1994) showed that such switching keeps detail that the median filter destroys, because most pixels are never changed. The detector here is the absolute difference to the median. A threshold of 0 replaces every pixel that differs from its median, which gives the plain median filter.

Kernel or operator

The Inspector shows the window, the median rank and the threshold. The Rank stepper in the Learn panel shows the sorted window at any pixel and the value the filter gives there.

Algorithm

  1. Pad the image with the border mode.
  2. Compute the median of every window with a sliding histogram.
  3. Compare each pixel with its median.
  4. Write the median where the difference is above T, and the pixel itself elsewhere.

Parameters

Parameter Default Values Meaning
Window size size 3 3 to 15, odd px Width and height of the square window for the median.
Threshold threshold 40 0 to 255 How far a pixel may differ from the window median before it counts as an impulse.
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

switchingMedianFilter in web/src/core/filters/nonlinear/switching-median.ts computes the median plane with the sliding histogram of the median filter, then applies the rule in switchValue to each pixel. The filter runs in the browser engine, in Web Workers, through the shared filter framework in web/src/core/filters/run.ts. It handles the border mode, the channel scope, alpha and the rounding to 8 bits. Each strip of the image gets a halo as wide as the support radius, so strips give exactly the pixels of a whole-image run.

Worked example

Window center Median m Difference T Output
200 60 140 40 60
90 60 30 40 90

Both windows have the same 8 neighbors, 62, 58, 61, 57, 60, 59, 63, 56. With 200 in the center, the sorted window is 56, 57, 58, 59, 60, 61, 62, 63, 200 and the median is 60. The difference 140 is above T = 40, so the pixel is replaced. With 90 in the center the median is again 60, but the difference 30 is below T, so the pixel keeps its value.

Complexity

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

N is the number of pixels per channel and k is the window width, as for the median filter.

Artifacts and limitations

Limitations

  • Fine bright or dark details that differ from the median by more than T are removed as well.
  • Impulses close to the local values, below T, stay.

Artifacts

  • With a low threshold, thin lines break up, as with the median filter.

Tests

The reference applies the same rule to scipy.ndimage.median_filter. Before it is used, the script checks that T = 0 gives the median filter and T = 255 changes nothing. The test covers three window and threshold settings on an input with impulses, for all five border modes.

References