Skip to content

Adaptive median filter

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

Overview

The adaptive median filter is a median filter that decides, pixel by pixel, how large its window must be and whether the pixel is noise at all. Where the noise is dense, the window grows until its median is a real value. Pixels that are not extreme in their window keep their values. So it removes much denser salt-and-pepper noise than a 3x3 median, with less blur than a large median.

Four panels in two rows: disks with 30 percent salt-and-pepper noise, a 3x3 median that leaves many specks, a 7x7 median that is clean but blurred, and the adaptive median that is clean and sharp. Four panels in two rows: disks with 30 percent salt-and-pepper noise, a 3x3 median that leaves many specks, a 7x7 median that is clean but blurred, and the adaptive median that is clean and sharp.
With 30 percent noise, a 3x3 median leaves specks and a 7x7 median blurs. The adaptive median does neither.

How it works

  1. Find the min, median and max of the 3x3 window.
  2. If the median equals the min or the max, it is an impulse itself. Grow the window by 2 and look again, up to the largest size.
  3. If the pixel lies strictly between the min and the max, keep it.
  4. Otherwise write the median.

Mathematical foundation

\[ \begin{gathered} \begin{aligned} &\text{A: } z_{\min} < z_{\text{med}} < z_{\max} \;\Rightarrow\; \text{go to B, else grow } S_{xy} \text{ up to } 7, \text{ then output } z_{\text{med}} \\ &\text{B: } I_{\text{out}}(x,y) = \begin{cases} z_{xy} & z_{\min} < z_{xy} < z_{\max} \\ z_{\text{med}} & \text{otherwise} \end{cases} \end{aligned} \end{gathered} \]

The formula shows the default parameter values.

where

  • \(z_{\min}, z_{\text{med}}, z_{\max}\): Min, median and max of the window
  • \(z_{xy}\): The value of the pixel (x, y)
  • \(S_{xy}\): The current window around (x, y)

z_min, z_med and z_max are the min, median and max of the current window S_xy, and z_xy is the pixel. Stage A checks that the median is not an impulse itself. If it is, the window grows by 2 and stage A runs again. Stage B keeps the pixel when it lies strictly between the min and the max, so it is not an impulse, and replaces it by the median otherwise.

Stage A tests the median, stage B tests the pixel. Both use the same test: a value is taken as an impulse when it equals the min or the max of the window. In salt-and-pepper noise, impulses are 0 or 255, so they are always at an end of the sorted window. A real value lies strictly inside the range as long as the window holds some ordinary values. When the largest window is reached without passing stage A, the filter writes the median of that window, as in Gonzalez and Woods.

Kernel or operator

The Inspector shows the largest window and the rule. The Rank stepper in the Learn panel shows the largest window at any pixel, sorted, next to the value the filter gives there.

Algorithm

  1. Round the values to whole numbers and pad the image with the border mode.
  2. For every window size from 3 to the largest, compute the min, median and max of all windows.
  3. For each pixel, find the first size whose median lies strictly between its min and max.
  4. Keep the pixel if it also lies strictly between them, or write the median. If no size fits, write the median of the largest window.

Parameters

Parameter Default Values Meaning
Largest window maxSize 7 3 to 21, odd px The window starts at 3x3 and may grow up to this size.
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

adaptiveMedianFilter in web/src/core/filters/nonlinear/adaptive-median.ts first rounds the values to whole numbers. For each window size it computes the min and max planes with the separable code of the min and max filters, and the median plane with the sliding histogram. Then each pixel walks through the sizes. This costs more memory than growing a window per pixel, but every step is a fast whole-plane pass. 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_5 = \begin{bmatrix} 100 & 104 & 110 & 98 & 102 \\ 106 & 255 & 0 & 255 & 99 \\ 101 & 0 & 255 & 0 & 105 \\ 97 & 255 & 0 & 255 & 103 \\ 108 & 96 & 107 & 100 & 109 \end{bmatrix} \]

The center pixel is 255. Its 3x3 window holds only impulses: 0, 0, 0, 0, 255, 255, 255, 255, 255. There z_min = 0, z_med = 255 and z_max = 255, so the median is an impulse too and stage A grows the window.

In the 5x5 window, z_min = 0, z_med = 103 and z_max = 255. Now z_min < z_med < z_max, so stage B runs. The pixel 255 is not strictly below z_max, so it is an impulse and the output is the median, 103. A plain 3x3 median would give 255 here.

Complexity

  • Time: \(O(N\,S_{\max}^2)\)
  • Memory: \(O(N\,S_{\max})\)

N is the number of pixels per channel. Each size s costs O(N s) for its min, median and max planes, and there are about S_max / 2 sizes.

Artifacts and limitations

Limitations

  • Detects only extreme values. It does not help with Gaussian noise.
  • A pixel equal to the window min or max counts as an impulse, so flat areas next to edges can be replaced by the median.

Artifacts

  • Small blotches where the noise is very dense and the window reaches its largest size.

Tests

The reference follows the two stages of Gonzalez and Woods with NumPy windows. It runs on an input with salt and pepper pixels, two of them side by side, for largest windows 3, 5 and 7 and all five border modes. Before it is used, the script checks that a plane without impulses passes through unchanged.

References

  • Hwang, H. and Haddad, R. A. Adaptive median filters, new algorithms and results. IEEE Transactions on Image Processing 4(4), 499 to 502, 1995. DOI 10.1109/83.370679.
  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 5.3, adaptive median filter.