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.
How it works¶
- Take the square window around the pixel.
- Sort its values from dark to bright.
- Pick the value in the middle of the list.
- Write it as the new pixel value.
Mathematical foundation¶
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¶
- Pad the image with the border mode.
- For each pixel and channel, collect the values in the size by size window.
- Find the middle value with a sliding 256-bin histogram.
- 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¶
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¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapters 3 and 5.
- Tukey, J. W. Exploratory Data Analysis. Addison-Wesley, 1977.
- Huang, T. S., Yang, G. J. and Tang, G. Y. A fast two-dimensional median filtering algorithm. IEEE Transactions on Acoustics, Speech, and Signal Processing, 27(1), 1979. DOI 10.1109/TASSP.1979.1163188.
- SciPy documentation, scipy.ndimage.median_filter. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.median_filter.html