Skip to content

Alpha-trimmed mean filter

Fact Value
Also called Trimmed mean 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/means.ts, alphaTrimmedMeanFilter
Operator code web/src/core/filters/nonlinear/means.ts, trimOf
Channels RGB, Brightness only

Overview

The alpha-trimmed mean filter sorts each window, drops the t smallest and the t largest values, and averages the rest. With t = 0 it is the box filter, and with the largest t it is the median. Values between suit images with both impulse noise and Gaussian noise.

Four panels in two rows: disks with Gaussian noise and specks, a 3x3 box filter that smears the specks, a 3x3 median that removes them but stays grainy, and a trimmed mean with t = 2 that removes the specks and smooths the grain. Four panels in two rows: disks with Gaussian noise and specks, a 3x3 box filter that smears the specks, a 3x3 median that removes them but stays grainy, and a trimmed mean with t = 2 that removes the specks and smooths the grain.
With Gaussian noise and specks, trimming drops the specks and averages the rest.

How it works

  1. Take the square window around the pixel and sort its values.
  2. Drop the t smallest and the t largest values.
  3. Write the mean of the values that are left.

Mathematical foundation

\[ I_{\text{out}}(x,y) = \frac{1}{n - 2t} \sum_{k = t + 1}^{n - t} W_{(k)}, \qquad n = 3^2,\quad t = 2 \]

The formula shows the default parameter values.

where

  • \(W_{(k)}\): The k-th smallest value of the window
  • \(n\): Number of values in the window
  • \(t\): Values dropped at each end

W with index (k) is the k-th smallest value of the window. The t smallest and the t largest values are dropped, and the n - 2t values in the middle are averaged. Gonzalez and Woods write d = 2t for the number of dropped values.

The trimmed mean is an L-estimator: a weighted sum of order statistics. Here the weights are 1 / (n - 2t) for the ranks t + 1 to n - t and 0 for the rest. Gonzalez and Woods write d = 2t for the number of dropped values. As long as a window holds at most t impulses of each kind, they are all dropped, and the mean of the rest smooths the Gaussian noise.

Kernel or operator

The Inspector shows the window, the number of values and the rank that is used. The Rank stepper in the Learn panel shows the sorted window at any pixel and the value the filter gives there. Here a run of ranks is marked, the values that are averaged.

Algorithm

  1. Limit the trim to (n - 1) / 2.
  2. Pad the image with the border mode.
  3. Keep a 256-bin histogram of the window as it slides along each row.
  4. Skip t values from the bottom of the histogram, add up the next n - 2t values, and divide by n - 2t.

Parameters

Parameter Default Values Meaning
Window size size 3 3 to 15, odd px Width and height of the square window.
Trim per end trim 2 0 to 112 How many of the smallest values, and how many of the largest, are dropped. It is d / 2 in the textbook form. At most (n - 1) / 2, so one value is left.
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

alphaTrimmedMeanFilter in web/src/core/filters/nonlinear/means.ts reads the sliding histogram of histogramPlane. It skips t values from the bottom, adds up the next n - 2t values and divides by n - 2t. The trim is limited to (n - 1) / 2, so at least one value is left. Values are whole numbers. 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} 52 & 61 & 58 \\ 49 & 255 & 63 \\ 55 & 0 & 60 \end{bmatrix} \]

Sorted: 0, 49, 52, 55, 58, 60, 61, 63, 255. With t = 2, the two smallest values (0 and 49) and the two largest (63 and 255) are dropped. The mean of the 5 values left, 52, 55, 58, 60, 61, is 57.20. The plain mean of all 9 values is 72.56.

Complexity

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

N is the number of pixels per channel and k is the window width. Each step updates one column of the histogram and scans its 256 bins.

Artifacts and limitations

Limitations

  • When more than t values in a window are impulses, they leak into the mean.
  • Blurs edges more than the median.

Artifacts

  • Slight blur along edges, like a small box filter.

Tests

The reference sorts each window with NumPy. Before it is used, it is checked against two limits: with t = 0 it must equal scipy.ndimage.uniform_filter, and with the largest t it must equal scipy.ndimage.median_filter. The test covers three window and trim settings for all five border modes.

References

  • Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Section 5.3, alpha-trimmed mean filter.