Filters
Another 3 3 smoothing filter
5.4 Nonlinear Filters
5Filters
Fig. 5.14 3×3 linear box filter ap- plied to a grayscale image corrupted with salt-and-pepper noise. Original (a), filtered image (b), enlarged details (c, d). Note that the indi- vidual noise pixels are only flattened but not removed.
Original Box filter
(a) (b)
(c) (d)
Fig. 5.15 Effects of a 1D minimum fil- ter on different local signal structures. Original signal (top) and result after filtering (bottom), where the colored bars indicate the extent of the filter. The step edge (a) and the linear ramp (c) are shifted to the right by half the filter width, and the narrow pulse (b) is completely removed.
Width of filter
(a) (b) (c)
whereR denotes the filter region (set of filter coordinates, usually a square of size 3×3 pixels). Figure 5.15illustrates the effects of a 1D minimum filter on various local signal structures.
Figure 5.16 shows the results of applying 3×3 pixel minimum and maximum filters to a grayscale image corrupted with “salt-and- pepper” noise (i.e., randomly placed white and black dots), respec- tively. Obviously the minimum filter removes the white (salt) dots, because any single white pixel within the 3×3 filter region is replaced 106
5.4Nonlinear Filters
Minimum filter Maximum filter
(a) (b)
(c) (d)
Fig. 5.16
Minimum and maximum fil- ters applied to a grayscale image corrupted with “salt- and-pepper” noise (see original in Fig. 5.14(a)). The 3×3 minimumfilter eliminates the bright dots and widens all dark image structures (a, c). The maximumfilter shows the ex- act opposite effects (b, d).
by one of its surrounding pixels with a smaller value. Notice, how- ever, that the minimum filter at the same time widens all the dark structures in the image.
The reverse effects can be expected from themaximumfilter. Any single bright pixel is a local maximum as soon as it is contained in the filter regionR. White dots (and all other bright image structures) are thus widened to the size of the filter, while now the dark (“pepper”) dots disappear.5
5.4.2 Median Filter
It is impossible of course to design a filter that removes any noise but keeps all the important image structures intact, because no filter can discriminate which image content is important to the viewer and which is not. The popular median filter is at least a good step in this direction.
5 The image shown inFigs. 5.14and5.16, called “Lena” (or “Lenna”), is one of the most popular test images in digital image processing ever and thus of historic interest. The picture of the Swedish “playmate” Lena Sjööblom (Söderberg?), published inPlayboy in 1972, was included in a collection of test images at the University of Southern California and was subsequently used by researches throughout the world (presumably without knowledge of its delicate origin) [115].
107
5Filters The median filter replaces every image pixel by themedianof the pixels in the current filter regionR, that is,
I(u, v) = median
(i,j)∈R{I(u+i, v+j)}. (5.40) The median of a set of 2n+ 1 valuesA={a0, . . . , a2n}can be defined as the center value an after arranging (sorting) A to an ordered sequence, that is,
median(a0, a1, . . . , an−1
nvalues
,an, an+1, . . . , a2n
nvalues
) =an, (5.41) where ai ≤ ai+1. Figure 5.17 demonstrates the calculation of the median filter of size 3×3 (i.e., n= 4).
Fig. 5.17 Calculation of the median.
The nine pixel values col- lected from the 3×3 im- age region are arranged as a vector that is subsequently sorted (A). The center value ofAis taken as the median.
7 21 0
9 5 8 3 0
72 1 09 58 0 3 7 2
1 0 0 9 5 8
3
a0
an−1
an+1
a2n
an= median(A)
I A
Sort
Equation (5.41) defines the median of an odd-sized set of values, and if the side length of the rectangular filters is odd (which is usually the case), then the number of elements in the filter region is odd as well. In this case, the median filter does not create any new pixel values that did not exist in the original image. If, however, the number of elements is even, then the median of the sorted sequence A = (a0, . . . , a2n−1) is defined as the arithmetic mean of the two adjacent center valuesan−1 andan,
median
a0, . . . ,an−1
nvalues ai≤an
,an, . . . , a2n−1
nvalues ai≥an
=an−1+an
2 . (5.42)
By averagingan−1andan, new pixel values are generally introduced by the median filter if the region is of even size.
Figure 5.18compares the results of median filtering with a linear- smoothing filter. Finally, Fig. 5.19 illustrates the effects of a 3×3 pixel median filter on selected 2D image structures. In particular, very small structures (smaller than half the filter size) are eliminated, but all other structures remain largely unchanged. A sample Java implementation of the median filter of arbitrary size is shown in Prog.
5.5. The constantKspecifies the side length of the filter regionRof size (2r+ 1)×(2r+ 1). The number of elements inR (equal to the length of the vectorA) is
(2r+ 1)2= 4(r2+r) + 1, (5.43) and thus the index of the middle vector element is n = 2(r2+r).
Setting r= 1 gives a 3×3 median filter (n= 4), r= 2 gives a 5×5 108
5.4Nonlinear Filters
Box filter (linear) Median filter (nonlinear)
(a) (b)
(c) (d)
Fig. 5.18
Linear smoothing filter vs.
median filter applied to a grayscale image corrupted with salt-and-pepper noise (see original in Fig. 5.14(a)). The 3×3 linear box filter (a, c) reduces the bright and dark peaks to some extent but is unable to remove them com- pletely. In addition, the entire image is blurred. The median filter (b, d) effectively elimi- nates the noise dots and also keeps the remaining structures largely intact. However, it also creates small spots of flat in- tensity that noticeably affect the sharpness.
(a) (b)
(c) (d)
Fig. 5.19
Effects of a 3×3 pixel median filter on different 2D image structures. Isolated dots are eliminated (a), as are thin lines (b). The step edge remains unchanged (c), while a corner is rounded off (d).
filter (n = 12), etc. The structure of this plugin is similar to the arbitrary size linear filter in Prog. 5.3.
5.4.3 Weighted Median Filter
The median is a rank order statistic, and in a sense the “majority” of the pixel values involved determine the result. A single exceptionally high or low value (an “outlier”) cannot influence the result much but only shift the result up or down to the next value. Thus the median (in contrast to the linear average) is considered a “robust” measure.
In an ordinary median filter, each pixel in the filter region has the same influence, regardless of its distance from the center.
109
5Filters
Prog. 5.5 Median filter of arbitrary size (PluginFilter_Median). An arrayAof typeintis defined (line 16) to hold the region’s pixel values for each filter po- sition (u, v). This array is sorted by using the Java utility methodArrays.sort()in line 32. The center element of the sorted vector (A[n]) is taken as the median value and stored in the original image (line 33).
1 import ij.ImagePlus;
2 import ij.plugin.filter.PlugInFilter;
3 import ij.process.ImageProcessor;
4 import java.util.Arrays;
5
6 public class Filter_Median implements PlugInFilter {
7
8 final int r = 4; // specifies the size of the filter 9
10 public void run(ImageProcessor ip) {
11 int M = ip.getWidth();
12 int N = ip.getHeight();
13 ImageProcessor copy = ip.duplicate();
14
15 // vector to hold pixels from (2r+1)x(2r+1) neighborhood:
16 int[] A = new int[(2 * r + 1) * (2 * r + 1)];
17
18 // index of center vector elementn= 2(r2+r): 19 int n = 2 * (r * r + r);
20
21 for (int u = r; u <= M - r - 2; u++) {
22 for (int v = r; v <= N - r - 2; v++) {
23 // fill the pixel vectorAfor filter position (u,v):
24 int k = 0;
25 for (int i = -r; i <= r; i++) {
26 for (int j = -r; j <= r; j++) {
27 A[k] = copy.getPixel(u + i, v + j);
28 k++;
29 }
30 }
31 // sort vector A and take the center elementA[n]:
32 Arrays.sort(A);
33 ip.putPixel(u, v, A[n]);
34 }
35 }
36 }
37 }
The weighted median filter assigns individual weights to the posi- tions in the filter region, which can be interpreted as the “number of votes” for the corresponding pixel values. Similar to the coefficient matrixH of a linear filter, the distribution of weights is specified by a weight matrix W, withW(i, j)∈N. To compute the result of the modified filter, each pixel value I(u+i, v+j) involved is inserted W(i, j) times into the extended pixel vector
A= (a0, . . . , aL−1) of length L=
(i,j)∈R
W(i, j). (5.44)
This vector is again sorted, and the resulting center value is taken as the median, as in the standard median filter. Figure 5.21illustrates the computation of the weighted median filter using the 3×3 weight matrix
110
5.4Nonlinear Filters
Median Filter Weighted Median Filter
(a) (b)
(c) (d)
Fig. 5.20
Ordinary vs. weighted median filter. Compared to the ordi- nary median filter (a, c), the weighted median (b, d) shows superior preservation of struc- tural details. Both filters are of size 3×3; the weight matrix in Eqn. (5.45) was used for the weighted median filter.
W =
⎡
⎣1 2 1 2 3 2 1 2 1
⎤
⎦, (5.45)
which requires an extended pixel vector of length L = 15, equal to the sum of the weights in W. If properly used, the weighted median filter yields effective noise removal with good preservation of structural details (seeFig. 5.20for an example).
Of course this method may also be used to implement ordinary median filters of nonrectangular shape; for example, across-shaped median filter can be defined with the weight matrix
W+=
⎡
⎣0 1 0 1 1 1 0 1 0
⎤
⎦. (5.46)
Not every arrangement of weights is useful, however. In particular, if the weight assigned to the center pixel is greater than the sum of all other weights, then that pixel would always have the “majority vote”
and dictate the resulting value, thus inhibiting any filter effect.
5.4.4 Other Nonlinear Filters
Median and weighted median filters are two examples of nonlinear filters that are easy to describe and frequently used. Since “nonlin-
111
5Filters
Fig. 5.21 Weighted median example.
Each pixel value is inserted into the extended pixel vec- tor multiple times, as spec- ified by the weight matrix W. For example, the value 0 from the center pixel is inserted three times (since W(0,0) = 3) and the pixel value 7 twice. The pixel vector is sorted and the center value (2) is taken as the median.
37
21
0 95
8 0 3 7 2
1 0 0 9 5 8
1 2 1 2 3 2 1 2 1
7
00
0
5 1
3
7 1 0
9 5
8 0
7 00
0
5 12
a0
an−1
an+1
a2n an= weighted median I(u, v)
W(i, j)
A
Sort
ear” refers to anything that is not linear, there are a multitude of filters that fall into this category, including the morphological filters for binary and grayscale images, which are discussed in Ch. 9. Other types of nonlinear filters, such as the corner detector described in Ch. 7, are often described algorithmically and thus defy a simple, compact description.
In contrast to the linear case, there is usually no “strong theory”
for nonlinear filters that could, for example, describe the relationship between the sum of two images and the results of a median filter, as does Eqn. (5.23) for linear convolution. Similarly, not much (if anything) can be stated in general about the effects of nonlinear filters in frequency space.