
198 A Computational Introduction to Digital Image Processing, Second Edition
MATLAB/Octave
>> a7 = fspecial(’average’,7);
>> g7 = imfilter(gsp,a7);
or
Python
In : c7 = ndi.uniform
_
filter(csp,7)
and the result is shown in Figure 8.4(b).
(a) 3 × 3 averaging (b) 7 × 7 averaging
FIGURE 8.4: Attempting to clean salt and pepper noise with average filtering
Median Filtering
Median filtering seems almost tailor-made for removal of salt and pepper noise. Recall
that the median of a numeric list is the middle value when the elements of the set are sorted.
If there are an even number of values, the median is defined to be the mean of the middle
two. A median filter is