Skip to content

Weighted median filter

Fact Value
Also called Center-weighted median filter
Status Implemented and tested
Family Nonlinear spatial filters
Engine Browser
Type Nonlinear, spatial, weighted order statistic
Output Image
Operator shown as rank (exact)
Support radius half the larger side of the weights
Code web/src/core/filters/nonlinear/weighted-median.ts, weightedMedianFilter
Operator code web/src/core/filters/nonlinear/weighted-median.ts, medianWeights
Channels RGB, Brightness only

Overview

The weighted median filter is a median in which each window position counts as many times as its weight. A heavier center keeps thin lines and corners that a plain median removes. A weight of 0 leaves a position out, so the weights also set the shape of the window.

Three panels: a Siemens star with salt-and-pepper noise, a 3x3 median that removes the noise and blurs the thin spokes near the center, and a center-weighted median that keeps more of the spokes. Three panels: a Siemens star with salt-and-pepper noise, a 3x3 median that removes the noise and blurs the thin spokes near the center, and a center-weighted median that keeps more of the spokes.
A heavier center weight keeps more of the thin spokes near the middle.

How it works

  1. Take the window around the pixel.
  2. Count each value as often as the weight of its position.
  3. Sort this longer list and write its middle value.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \min\,\Bigl\{\, v \;:\; \sum_{(i,j)\,:\,I(x+i,\,y+j) \le v} w_{ij} \;\ge\; \tfrac{1}{2} \sum_{(i,j)} w_{ij} \,\Bigr\} \]

The formula shows the default parameter values.

where

  • \(w_{ij}\): Weight of window position (i, j)
  • \(v\): A candidate output value

Sort the window values and add up their weights from the smallest value up. The output is the first value at which the running weight reaches half of the total. For whole weights with an odd total, this is the median of the list in which each value is repeated as often as its weight. For an even total it is the lower of the two middle values.

For weights that are whole numbers, the weighted median is the median of the list in which each value is repeated as often as its weight (Brownrigg, 1984). For any weights of 0 or more, it is the value v that minimizes a weighted sum. That sum adds up each weight times the distance from its window value to v. The filter takes the smallest value at which the running weight reaches half of the total. When the total is even, this is the lower of the two middle values. If the center weight is half of the total or more, the center always wins, and the filter changes nothing.

Kernel or operator

The Inspector shows the weights as a grid and the total weight. The Rank stepper in the Learn panel shows the window at any pixel, sorted, next to the value the filter gives there.

Algorithm

  1. Read the weights. Fall back to the default if they are negative or add up to 0.
  2. Pad the image with the border mode.
  3. For each pixel, add each weight to the histogram bin of the value under it.
  4. Walk the bins from 0 up until the running weight reaches half of the total, and write that value.

Parameters

Parameter Default Values Meaning
Weights weights 1 1 1; 1 3 1; 1 1 1 Rows of numbers, odd sides up to 15 The weight of each window position, row by row, like a kernel. Both sides must be odd, up to 15. Weights must be 0 or more, with a total above 0.
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

weightedMedianPlane in web/src/core/filters/nonlinear/weighted-median.ts adds each weight to the histogram bin of the value under it, then walks the bins from 0 until the running weight reaches half. The weights use the same text format as the weighted average filter. Weights that cannot be read, negative weights or a total of 0 make the filter use the default weights, and the Inspector says so. 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

\[ W = \begin{bmatrix} 40 & 40 & 200 \\ 40 & 200 & 200 \\ 40 & 200 & 40 \end{bmatrix}, \qquad w = \begin{bmatrix} 1 & 1 & 1 \\ 1 & 3 & 1 \\ 1 & 1 & 1 \end{bmatrix} \]

The center value counts 3 times, so the list has 11 entries: 40, 40, 40, 40, 40, 200, 200, 200, 200, 200, 200. The middle entry, the 6th, is 200. The plain median of the 9 values is 40, because 40 occurs 5 times. The heavier center keeps this pixel of a thin bright line.

Complexity

  • Time: \(O(N\,(m + 256))\)
  • Memory: \(O(N)\)

N is the number of pixels per channel and m is the number of positions with a weight above 0.

Artifacts and limitations

Limitations

  • A center weight of half the total or more makes the filter return the input unchanged.
  • Weights must be 0 or more. Negative weights have no meaning here.

Artifacts

  • With a heavy center, noise clusters of two or three pixels can survive.

Tests

The reference sorts each window with NumPy and walks the weights. Before it is used, it is checked twice: with all weights 1 it must equal scipy.ndimage.median_filter, and with whole weights it must equal the median of the repeated values. The test covers four weight patterns, including a plus shape, a single row and fractional weights with an even total, for three border modes.

References

  • Brownrigg, D. R. K. The weighted median filter. Communications of the ACM 27(8), 807 to 818, 1984. DOI 10.1145/358198.358222.
  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 5.