# What are the mean and median filters?

## Mean filter

The mean filter is a simple sliding-window spatial filter that replaces the
center value in the window with the average (mean) of all the pixel values in
the window. The window, or kernel, is usually square but can be any shape.
An example of mean filtering of a single 3x3 window of values is shown below.

unfiltered values
5 |
3 |
6 |

2 |
1 |
9 |

8 |
4 |
7 |

5 + 3 + 6 + 2 + 1 + 9 + 8 + 4 + 7 = 45

45 / 9 = 5

.

mean filtered
* |
* |
* |

* |
5 |
* |

* |
* |
* |

Center value (previously 1) is
replaced by the mean of all nine values (5).

## Median filter

The median filter is also a sliding-window spatial filter, but it replaces the
center value in the window with the median of all the pixel values in
the window. As for the mean filter, the kernel is usually square but can be any shape.
An example of median filtering of a single 3x3 window of values is shown below.

unfiltered values
6 |
2 |
0 |

3 |
97 |
4 |

19 |
3 |
10 |

in order:

0, 2, 3, 3, **4**, 6, 10, 15, 97

.

median filtered
* |
* |
* |

* |
4 |
* |

* |
* |
* |

Center value (previously 97) is
replaced by the median of all nine values (4).

Note that for the first (top) example, the median filter would also return a value
of 5, since the ordered values are 1, 2, 3, 4, **5**, 6, 7, 8, 9.
For the second (bottom) example, though, the mean filter returns the value 16 since the
sum of the nine values in the window is 144 and 144 / 9 = 16. This illustrates
one of the celebrated features of the median filter: its ability to remove
'impulse' noise (outlying values, either high or low). The median filter is also
widely claimed to be 'edge-preserving' since it theoretically preserves step
edges without blurring. However, in the presence of noise it does blur edges
in images slightly.

## General Image Processing References

Kenneth R. Castleman,
Digital
Image Processing. Prentice
Hall, 1996.

Anil K. Jain, Fundamentals of Digital Image Processing.
Prentice Hall, 1989.

William K. Pratt, Digital Image Processing. Wiley, 1991.

Rafael C. Gonzalez and Richard E. Woods, Digital Image Processing.
Addison-Wesley, 1992.

I. Pitas and A. N. Venetsanopoulos, Nonlinear Digital Filters: Principles
and Applications. Kluwer Academic, 1990.

Return to Image Processing in Java.

Mark A. Schulze

http://www.markschulze.net/

mark@markschulze.net

Last Updated: 24 April 2001

Copyright © 1996-2001 by Mark A. Schulze. All Rights Reserved.