Skip to content

Nonlinear Filtering

Image filtering changes pixel values using information from nearby pixels. Different filters can examine the same neighborhood but use different rules to determine the output.

1. 8-bit Grayscale Images

A grayscale image stores one intensity value at each pixel. That value describes how dark or bright the pixel is.

In an 8-bit grayscale image, each pixel can have one of 256 possible values:

\[ 0 \le I \le 255 \]
8-bit Grayscale Intensity
0 — Black 255 — White
0
64
128
192
255
A smaller number represents a darker pixel. A larger number represents a brighter pixel.

2. Image Filtering

Noise can cause unwanted changes in pixel intensities. Filtering changes pixel values using neighboring image data.

Original Image → Noisy Image → Filtered Image
Original
→
Noisy
→
Filtered

3. The Neighborhood

A neighborhood is a collection of pixels surrounding the location currently being processed.

Neighborhood shape can be described in several ways. Common pixel-grid distance measures include Euclidean distance, Manhattan or city-block distance, and Chebyshev or chessboard distance. Their definitions are introduced separately.

On this page, we use a square neighborhood \(W\).

Example Square Neighborhood
98
102
99
101
255
97
100
103
99
102
96
101
100
104
98
99
102
97
101
100
103
98
99
102
96
Current center pixel
Example
100
\[ r=2 \] \[ L=5 \] \[ N=25 \]
The \(5\times5\) neighborhood is only an example. The filter definitions below are written using variables so they also apply to other odd-sized square neighborhoods.

4. Neighborhood Variables

Let \(r\) be the radius of a square neighborhood measured from its center pixel.

\[ L = 2r+1 \] \[ N = L^2 = (2r+1)^2 \]
\(W\)
Current neighborhood around the pixel being processed.
\(r\)
Neighborhood radius measured from the center.
\(L\)
Number of pixels along one side of the square neighborhood.
\(N\)
Total number of pixels in the neighborhood.
\(x_{k,l}\)
Input intensity at image location \((k,l)\).
\(y_{k,l}\)
Filtered output intensity at location \((k,l)\).
For the square example used throughout this page, \(r=2\), so \(L=5\) and \(N=25\).

5. Mean Filter

The mean filter replaces the center value with the average of all \(N\) values in the neighborhood.

\[ y_{k,l} = \frac{1}{N} \sum_{i=-r}^{r} \sum_{j=-r}^{r} x_{k+i,l+j} \]
98
102
99
101
255
97
100
103
99
102
96
101
100
104
98
99
102
97
101
100
103
98
99
102
96
Example Output
106.08
All \(N\) values contribute.
Because every value contributes to the average, an unusually high or low intensity can affect the result.

6. Median Filter

The median filter sorts the \(N\) neighborhood values and chooses the middle ranked value.

For an odd number of samples, define the middle rank as

\[ m = \frac{N+1}{2} \] \[ y_{\mathrm{median}} = x_{(m)} \]
The notation \(x_{(m)}\) means the value at rank \(m\) after the neighborhood intensities have been sorted.
98
102
99
101
255
97
100
103
99
102
96
101
100
104
98
99
102
97
101
100
103
98
99
102
96
Example
\[ N=25 \] \[ m = \frac{25+1}{2} = 13 \]
100

7. Alpha-Trimmed Mean Filter

The alpha-trimmed mean sorts the neighborhood values, removes selected values from both ends of the sorted list, and averages what remains.

Let \(d\) represent the total number of values removed. For an even \(d\), \(d/2\) values are removed from each end.

\[ y_{\mathrm{trim}} = \frac{1}{N-d} \sum_{i=d/2+1}^{N-d/2} x_{(i)} \]

One way to choose \(d\) is

\[ d = \operatorname{round} (\alpha N) \]
\(\alpha\)
Trimming parameter controlling how much of the sorted list is removed.
\(d\)
Total number of samples removed from the sorted list.
The exact rule used to make \(d\) even is an implementation detail. The central idea is to remove values from both ends before averaging.

8. Sigma Filter

The sigma filter compares each neighborhood value \(x\) with the current center intensity \(x_c\).

\[ |x-x_c| \le \sigma \]
98
102
99
101
255
97
100
103
99
102
96
101
100
104
98
99
102
97
101
100
103
98
99
102
96
Example \[ x_c=100 \] \[ \sigma=10 \] Accepted range: \[ 90 \le x \le 110 \]

9. Symmetric Nearest-Neighbor Mean Filter

The SNN filter pairs positions that are symmetric around the center. From each pair, the value closer in intensity to the center is selected.

For a symmetric neighborhood containing an odd number \(N\) of samples:

\[ \frac{N-1}{2} \] symmetric pairs are formed.

One value is selected from each pair, and the center value is also included.

\[ \frac{N-1}{2} + 1 = \frac{N+1}{2} \]
Therefore the number of values averaged by this SNN construction is \((N+1)/2\).
98
102
99
101
255
97
100
103
99
102
96
101
100
104
98
99
102
97
101
100
103
98
99
102
96
One Pair \[ |255-100| = 155 \] \[ |103-100| = 3 \]
103
For the \(N=25\) example, there are \((25-1)/2=12\) symmetric pairs. Twelve selected neighbors plus the center gives \((25+1)/2=13\) values.

10. Order-Statistic Filters

Order-statistic filters sort the neighborhood intensities from smallest to largest:

\[ x_{(1)} \le x_{(2)} \le \cdots \le x_{(N)} \]
The number in parentheses represents a rank in the sorted list, not a spatial pixel coordinate.
Example Ranked List for \(N=25\)
96
96
97
97
98
98
98
99
99
99
99
100
100
100
101
101
101
102
102
102
102
103
103
104
255
Minimum \[ x_{(1)} \] 1st value after sorting
Median \[ x_{(m)} \] where \[ m = \frac{N+1}{2} \]
Maximum \[ x_{(N)} \] Last value after sorting
Minimum, median, and maximum filtering can all begin with the same neighborhood. The difference is which rank in the sorted list is chosen.

11. Minimum and Maximum Filters

The minimum and maximum filters select opposite ends of the ranked neighborhood values.

\[ y_{\min}(k,l) = \min_{(i,j)\in W} x_{k+i,l+j} \] \[ y_{\max}(k,l) = \max_{(i,j)\in W} x_{k+i,l+j} \]
Minimum Filter
98
102
99
101
255
97
100
103
99
102
96
101
100
104
98
99
102
97
101
100
103
98
99
102
96
↓
96
The smallest value in \(W\) becomes the output at the center position.
Maximum Filter
98
102
99
101
255
97
100
103
99
102
96
101
100
104
98
99
102
97
101
100
103
98
99
102
96
↓
255
The largest value in \(W\) becomes the output at the center position.
The selected pixel does not physically move. Only its intensity value is used as the output value at the center location.
For a flat gray-scale structuring element, this local minimum/maximum behavior is closely related to gray-scale morphology: erosion uses a local minimum and dilation uses a local maximum.

12. Recursive Median Filtering

An ordinary median filter calculates each output from the current input neighborhood.

A recursive median filter can reuse output values that were calculated earlier in the scan.

255
102
101
98
101
100
103
99
97
100
101
99
102
Already filtered
Current pixel
Not processed yet
Current Location

Previous output values may be reused.

Later positions still contribute input values.

101
Example result

A general one-dimensional recursive-median notation is

\[ y_k = \operatorname{median} \left( y_{k-r}, \ldots, y_{k-1}, x_k, x_{k+1}, \ldots, x_{k+r} \right) \]
The exact two-dimensional neighborhood and scan order depend on the chosen recursive-median implementation. The defining idea is that previous filtered outputs can influence later calculations.

13. Comparing the Main Denoising Filters

These filters use the same local neighborhood but apply different rules to determine the output value.

Mean Filter

Average all neighborhood values.

Average
Median Filter

Sort the neighborhood values and select the middle-ranked value.

Rank
Alpha-Trimmed Mean Filter

Remove values from both ends of the sorted list, then average the values that remain.

Trim + Average
Sigma Filter

Keep neighborhood values that are sufficiently close to the center intensity, then average the accepted values.

Range Selection
SNN Mean Filter

Compare symmetric pixel pairs, select one value from each pair, and average the selected values.

Symmetric Pairs
The main difference is the selection rule used before producing the output value.

14. Repeated Filtering

A filter can be applied repeatedly. Each new iteration uses the result of the previous iteration.

\[ I^{(t+1)} = F \left( I^{(t)} \right) \]
\(t\) is the current iteration number, \(I^{(t)}\) is the image at that iteration, and \(F\) is the selected filtering operation.
Repeated filtering can strengthen an effect, but it can also progressively modify edges and small image structures.

15. Histogram

A grayscale histogram counts how many pixels occur at each intensity.

Example Gray-Level Histogram
Pixel Count
0 128 255
Gray Level
A histogram describes how frequently intensity values occur. It does not show where those pixels are located.

16. Mean and Standard Deviation

Mean

\[ \mu = \frac{1}{M} \sum_{i=1}^{M} x_i \]

\(M\) is the number of intensity samples being summarized.

The mean describes their average intensity.

Standard Deviation

\[ s = \left( \frac{ \sum_{i=1}^{M} (x_i-\mu)^2 }{ M-1 } \right)^{1/2} \]

Standard deviation describes how much the intensities vary around their mean.

\(M\) is used here instead of \(N\) because these statistics may be calculated over any selected collection of image pixels, not only one filtering neighborhood.

17. Interactive Example

The interactive visualizer below uses a square \(5\times5\) example neighborhood.

In the notation above, this example uses \(r=2\), \(L=5\), and \(N=25\). These are example values rather than part of the general filter definitions.

Select a pixel in the grid and change its 8-bit grayscale value. The visualizer can then show how the selected nonlinear filter changes the center output.

The current interactive visualizer focuses on the mean, median, alpha-trimmed mean, sigma, and SNN mean filters. Minimum, maximum, and recursive median are introduced above to explain the broader family of nonlinear filtering methods.

Summary

Concept Main Idea
\(W\) Current filtering neighborhood.
\(r\) Radius of a square neighborhood.
\(L\) Side length, \(L=2r+1\).
\(N\) Number of neighborhood pixels, \(N=L^2\).
Mean Filter Average all \(N\) neighborhood values.
Median Filter Select rank \(m=(N+1)/2\) for odd \(N\).
Alpha-Trimmed Mean Remove \(d\) values from the sorted list and average the remaining \(N-d\) values.
Sigma Filter Keep neighborhood values whose intensity is close to the center value.
SNN Mean Form symmetric pairs and select one value from each pair according to similarity with the center.
Order Statistic A value identified by its rank after sorting.
Minimum Filter Select \(x_{(1)}\), the smallest ranked value.
Maximum Filter Select \(x_{(N)}\), the largest ranked value.
Recursive Median Previously calculated outputs may influence later median calculations.
Distance Measures Euclidean, Manhattan/city-block, and Chebyshev/chessboard distance are common ways to describe spatial relationships on a pixel grid.

Nonlinear Filtering Visualizer
\(5\times5\) Neighborhood

Click a pixel to select it. The center is identified by its orange border.

Center Selected Used
Selected Pixel
Row 3, Column 3
Center pixel
100
0 = black 255 = white
Filter Calculation
New Center Value
0
Original Noisy Image
0 → grayscale intensity → 255
Filtered Image
Current filtered result
Iteration
0
Mean
0
Standard Deviation
0
Histogram

Published inDigital Image Processing

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *