Image Processing Toolbox User's Guide 
Using Median Filtering
Median filtering is similar to using an averaging filter, in that each output pixel is set to an average of the pixel values in the neighborhood of the corresponding input pixel. However, with median filtering, the value of an output pixel is determined by the median of the neighborhood pixels, rather than the mean. The median is much less sensitive than the mean to extreme values (called outliers). Median filtering is therefore better able to remove these outliers without reducing the sharpness of the image. The medfilt2
function implements median filtering.
Note
Median filtering is a specific case of orderstatistic filtering, also known as rank filtering. For information about orderstatistic filtering, see the reference page for the ordfilt2 function.

The following example compares using an averaging filter and medfilt2
to remove salt and pepper noise. This type of noise consists of random pixels' being set to black or white (the extremes of the data range). In both cases the size of the neighborhood used for filtering is 3by3.
medfilt2
does a better job of removing noise, with less blurring of edges.
Using Linear Filtering  Using Adaptive Filtering 
© 19942005 The MathWorks, Inc.