Image filtering is one of the main image processing tools. It is simply a bunch of matrix calculations, where a new value for every single pixel is calculated based on the pixel value itself and on a set of surrounding pixel values. The number of pixel values taken into the calculation is determined by a mask (or kernel) size. For example, if the mask is 3 pixels wide and 3 pixels heigh (3 * 3), then 9 pixel values are taken into the calculation. The center value of a mask is the pixel itself. The mask size is always (odd value * odd value), otherwise there would be no center point in the mask. The pixels are usually calculated from the top-left corner (which is almost always x=0,y=0) and down to the bottom-right, row by row. Most digital image filters are variations of one of these three :
Two basic algorithms are the mean and the median filtering. These are low-pass filters.
Pixel values :
40 23 33
44 53 32
43 24 43
Mean filter: the new value for the center pixel (53) is given by:
(40+23+33+44+53+32+43+24+43)/9 = 37.2222 = 37
Median filter: the values are sorted and the middle value is taken:
23, 24, 32, 33, 40, 43, 43, 44, 53 = 40.
The pixel values in the borders are either calculated by available pixel values or alternatively the missing pixels values are taken from the opposite side (mirrored).
The mask can also be weighted, like in the Gaussian and Bartlett filters.
Bartlett - filter, mask size 7 * 7, mask weights:
1 2 3 4 3 2 1 2 4 6 8 6 4 2 3 6 9 12 9 6 3 4 8 12 16 12 8 4 3 6 9 12 9 6 3 2 4 6 8 6 4 2 1 2 3 4 3 2 1
The original pixel has a weight of 16, in other words it is multiplied with 16. All the other pixels are also multiplied with their weight value. The sum of all the pixel values times their weight is than divided by the sum of all weights, which is 256 in this case. That's how we get a weighted mean value for the pixel.
The Gaussian filter weights :
1 4 8 10 8 4 1 4 12 25 29 25 12 4 8 25 49 58 49 25 8 10 29 58 67 58 29 10 8 25 49 58 49 25 8 4 12 25 29 25 12 4 1 4 8 10 8 4 1
The pixel itself has a weight of 67 and the sum of all weights is 999.
Example 2:
Taking out the noise by median filtering the image:

You might want to try our Java-applet for experimenting with a few image operations: low-pass and high-pass filtering, resizing, color2gray-conversion and flipping.