Skip to content

Median absolute deviation thresholding

Fact Value
Also called MAD thresholding, Hampel filter, Hampel identifier
Status Implemented and tested
Family Adaptive and statistical filters
Engine Browser
Type Nonlinear, statistical, outlier test with robust statistics
Output Image
Operator shown as estimator (exact)
Support radius floor(size / 2)
Code web/src/core/filters/adaptive/mad-outlier.ts, madOutlierFilter
Operator code web/src/core/filters/adaptive/mad-outlier.ts, medianAndScale
Channels RGB

Overview

MAD thresholding, also called the Hampel filter, finds values that lie much further from the local median than the local values usually do. The usual distance is the median absolute deviation, the MAD. Because the threshold follows the local spread, busy areas tolerate more variation than flat ones. The filter can repair the outliers with the median, or show where they are.

Three panels: disks with Gaussian grain and black and white specks, the outlier view with white dots only at the specks, and the repaired image with the grain still there and the specks gone. Three panels: disks with Gaussian grain and black and white specks, the outlier view with white dots only at the specks, and the repaired image with the grain still there and the specks gone.
With t = 3, the specks are outliers but the Gaussian grain is not, because the MAD measures it.

How it works

  1. Find the median m of the window.
  2. Find the median of the distances from m, the MAD, and scale it by 1.4826 to get S.
  3. If the value is further than t times S from m, it is an outlier. Replace it by m, or mark it.

Mathematical foundation

\[ S = 1.4826\,\operatorname{median}_{w \in W} |w - m|, \qquad I_{\text{out}} = \begin{cases} m & |I - m| > t\,S \\ I & \text{otherwise} \end{cases}, \qquad t = 3 \]

The formula shows the default parameter values.

where

  • \(m\): The median of the window
  • \(S\): The scaled median absolute deviation
  • \(t\): The threshold in units of S

m is the median of the window W, and the MAD is the median of the distances from m. The factor 1.4826, which is 1 divided by the 75th percentile of the standard normal distribution, makes S estimate the standard deviation of Gaussian noise. A value further than t S from the median is an outlier. In a perfectly flat window S is 0, so any different value counts.

The three-sigma rule calls a value an outlier when it is more than three standard deviations from the mean. Outliers inflate both the mean and the standard deviation, so they can hide. Hampel replaced both with the median and the MAD, which ignore up to half of the samples. For Gaussian noise the MAD is about 0.6745 standard deviations, so the factor 1.4826 turns it into an estimate of the standard deviation. In a flat window the MAD is 0, and any value that differs from the median is an outlier.

Kernel or operator

The Inspector shows the test with the current t. The Learn panel shows a line profile, and the worked example below runs the test for one window.

Algorithm

  1. Pad the image with the border mode.
  2. For each pixel and channel, sort the window and take its median m.
  3. Sort the distances to m and take their median, times 1.4826, as S.
  4. Where the value is further than t S from m, write m or mark the pixel. Elsewhere keep the value.

Parameters

Parameter Default Values Meaning
Window size size 5 3 to 9, odd px Width and height of the square window for the median and the MAD.
Threshold t t 3 0.5 to 10 How many times the robust scale S a value may be from the median.
Show output Repaired image Repaired image, Outliers Replace outliers by the median, or show white where any channel is an outlier.
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.

Implementation

madOutlierFilter in web/src/core/filters/adaptive/mad-outlier.ts sorts each window to find m, then sorts the distances to find the MAD, in medianAndScale. Each channel is tested on its own. The mark view writes white where any channel is an outlier. It is a color filter: the framework in web/src/core/filters/run.ts hands it the red, green and blue planes together, with the border mode, and keeps alpha. 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} 50 & 54 & 49 \\ 52 & 120 & 51 \\ 48 & 55 & 53 \end{bmatrix} \]

The median is m = 52. The distances to it, sorted, are 0, 1, 1, 2, 2, 3, 3, 4, 68, so the MAD is 2 and S = 1.4826 x 2 = 2.965. With t = 3 the limit is 8.90. The center is 68 away from m, above the limit, so it is an outlier and becomes 52. The value 55 is 3 away, below the limit, so it would stay.

Complexity

  • Time: \(O(N\,k^2 \log k)\)
  • Memory: \(O(N)\)

N is the number of pixels and k is the window width. Each window is sorted twice per channel.

Artifacts and limitations

Limitations

  • In flat areas S is 0 or very small, so tiny real changes count as outliers.
  • Sorting each window twice makes it slower than the median filter.

Artifacts

  • Fine texture in flat surroundings can be flattened.

Tests

The reference computes the median and the scaled MAD of each NumPy window on a color input with impulses. Before it is used, the script checks the scaled MAD against scipy.stats.median_abs_deviation with the normal scale. The test covers both views, two window sizes and two thresholds for all five border modes.

References