linear filters in image processing and computer vision
DESCRIPTION
In this report, some basic theory about linear filtering is presented. The convolution operations is presented together with some basic theory on averaging and Gaussian filters. Some implementations of these filters in MATLAB are also presented.TRANSCRIPT
UNIVERSITY OF STAVANGER
Department of Electrical Engineering and Computer Science
Linear Filters in Image Processing andComputer Vision
Project in ELE510 Image Processing with Robot Vision
By Shahed Farid
October 17, 2012
Contents1 Introduction 3
2 Theory 42.1 Discrete Convolution . . . . . . . . . . . . . . . . . . . . . . . 42.2 Edge-Problems . . . . . . . . . . . . . . . . . . . . . . . . . . 62.3 Averaging Filter . . . . . . . . . . . . . . . . . . . . . . . . . . 72.4 Gaussian Filter . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Implementation, Experiments and Results 103.1 Averaging Filter Implementation . . . . . . . . . . . . . . . . . 103.2 Gaussian Filter Implementation . . . . . . . . . . . . . . . . . 14
4 Conclusion 19
Bibliography 20
A MATLAB-Code 21A.1 Averaging Filter (Zero-Padding at Edges) . . . . . . . . . . . . 21A.2 Averaging Filter (Mirror-Reflection at Edges) . . . . . . . . . . 22A.3 Gaussian Filter (Zero-Padding at Edges) . . . . . . . . . . . . 23A.4 Gaussian Filter (Mirror-Reflection at Edges) . . . . . . . . . . 24
2
1 Introduction
In image processing and computer vision, filters are mainly used to atten-uate or amplify certain aspects of images. Almost every image processingapplication involves some kind of filtering to achieve the desired end-results.Some examples are image smoothing, edge-detection and image-enhancement[2].
Figure 1: Some filter applications, top: smoothing, middle: edge-detection,bottom: enhancement [1]
In this report, some basic theory about linear filtering is presented. The con-volution operations is presented together with some basic theory on averagingand Gaussian filters. Some implementations of these filters in MATLAB arealso presented.
3
2 Theory
An image is represented digitally as a matrix with pixel values. Filtering canbe seen as neighborhood processing around a pixel. When a mask (a matrixwith values called filter coefficients) is moved over the image, a new image iscreated whose pixels have values calculated from the values under the mask.Each location of the newly created matrix is filled with a weighted sum ofthe pixel values from the locations sorrounding the corresponding location inthe image [7, 6]. Figure 2 shows an example of this operation.
Figure 2: Masking [7]
2.1 Discrete Convolution
Mathematically, the operation mentioned above can be described with dis-crete convolution.
Figure 3: Convolution, modified version from [3]
If F is the input image and H is the convolution kernel (mask), the outputimage, G, can be written as
G(x, y) =N∑
k=−N
N∑l=−N
H(x− k, y − l) F (k, l) (1)
4
where x and y are input- and output image matrix elements and k and l arethe elements in the filter kernel matrix. A linear filter thus builds an outputimage, G, from an input image, F . In most cases, F and G has the samesize. Each pixel within the output image is computed from the neighboringpixels from the same point in the input image. N is used to describe theneighborhood size. The neighborhood size is (2N + 1) by (2N + 1). If e.g.N = 2, the neighborhood size is 5 by 5 [7, 3].
Figure 4: Neighborhood size [3]
A linear filter is completely characterized by its filter coefficients. It is thecoefficients that defines the convolution kernel, the kernel defines the differentweights that will be applied to different pixels in the input image [3].
5
2.2 Edge-Problems
Figure 5: Mask at edge [7]
Some areas which can be problematic when using convolution are the edgesin an image. The filter kernel will fall partly outside the image and thereforethere will not be enough values to calculate the output image [3, 6, 7]. Someof the solutions to this problem are described below:
Zero-padding: Add a border with zero-pixels. This method can give darkedges around the image.
Mirror-reflection: Input array values outside the bounds of the array arecomputed by mirror-reflecting the array across the array border. Quotationfrom [5].
Ignore edges: The mask is only applied to those pixels for which the maskwill lie entirely within the image. The output image will be smaller than theinput image and a significant amount of information can be lost by using thismethod.
Periodic assumption: Input array values outside the bounds of the arrayare computed by implicitly assuming the input array is periodic. Quotationfrom [5].
6
2.3 Averaging Filter
One simple filter is the averaging filter. The effect of this filter is blurring(or smoothing), thus having a low-pass effect. The details in an image (andsome noise) has high frequency components, these are attenuated.
The averaging filter kernel is defined as
HAV G(k, l) =
{ 1(2N+1)2
−N ≤ k, l ≤ −N0 otherwise
(2)
This filter replaces each pixel in the image with an unweighted average com-puted over a fixed region [6, 3]. One example of this type of filter is thefollowing (with a 9 by 9 filter kernel):
12
34
56
78
9
1
2
3
4
5
6
7
8
9−1
−0.5
0
0.5
1
1.5
z
xy
Figure 6: 9 by 9 averaging filter kernel
7
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.01230.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.01230.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.01230.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.01230.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.01230.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.01230.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.01230.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.01230.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
The averaging filter gives a poor blurring effect since there is an unweighted,uniform average computation. Usually, one want a blurring effect that re-sembles that of viewing of an image through a translucent screen. This filtertype will not give such a result. A better blurring effect is achieved using aGaussian filter, described next.
2.4 Gaussian Filter
With Gaussian filtering, an image is filtered using the two-dimensional Gaus-sian function. The pixel at the center of the mask is weighted more (has thehighest Gaussian value) than those at the boundaries. The neighboring pix-els (relative to the center pixel) receives smaller weights as their distance tothe center pixel increases [3]. The Gaussian filter kernel is defined as
HGAUSS(k, l) =1
2πσ2e(−
k2+l2
2σ2) (3)
where σ is the standard deviation (called sigma). This parameter controlsimportant aspects of the filtering [6]:
• If the standard deviation is very small, the filtering will not have alarge effect on the image because the pixel weights off the center willbe very small.
• A larger standard deviation will cause the neighboring pixels to havelarger weights. Some blurring will be applied to the input image.
• A too large standard deviation will cause a lot of blurring and much ofthe the details in the image will disappear.
8
One example of a Gaussian filter kernel is the following with size 9 by 9 andσ = 2:
12
34
56
78
9
1
2
3
4
5
6
7
8
90
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
0.045
xy
z
Figure 7: 9 by 9 Gaussian filter kernel with σ = 2
0.0008 0.0018 0.0034 0.0050 0.0056 0.0050 0.0034 0.0018 0.00080.0018 0.0044 0.0082 0.0119 0.0135 0.0119 0.0082 0.0044 0.00180.0034 0.0082 0.0153 0.0223 0.0253 0.0223 0.0153 0.0082 0.00340.0050 0.0119 0.0223 0.0325 0.0368 0.0325 0.0223 0.0119 0.00500.0056 0.0135 0.0253 0.0368 0.0417 0.0368 0.0253 0.0135 0.00560.0050 0.0119 0.0223 0.0325 0.0368 0.0325 0.0223 0.0119 0.00500.0034 0.0082 0.0153 0.0223 0.0253 0.0223 0.0153 0.0082 0.00340.0018 0.0044 0.0082 0.0119 0.0135 0.0119 0.0082 0.0044 0.00180.0008 0.0018 0.0034 0.0050 0.0056 0.0050 0.0034 0.0018 0.0008
The center pixel has the highest value (0.0417), and the neighboring pix-els have smaller values. The values are symmetrically distributed aroundthe center pixel, with smaller values as the distance from the center pixelincreases.
The Gaussian filter gives a blurring that preserves boundaries and edges bet-ter than the regular averaging filter, where the weighing is uniform [6, 3].In computer vision, Gaussian filters are sometimes used as a pre-processingstage in applications that involves image structure enhancing at differentscales [4]. Some edge-detection applications also uses Gaussian filtering.
9
3 Implementation, Experiments and Results
In this section, implementations of the averaging and Gaussian filters inMATLAB are presented. Functions provided with the Image ProcessingToolbox has been used [5]:
• imread is used to read images into MATLAB and store them as ma-trices. For example, F = imread(’cameraman.tif’) will read in theimage called cameraman (included in MATLAB) and store it as a ma-trix.
• fspecial is used to create the filter masks. The type and size of the fil-ter mask can be specified. For example, H = fspecial(’average’, 3),will create a 3 by 3 averaging filter kernel. When creating Gaussianmasks with this function, the sigma parameter can be specified.
• imfilter is used to perform the actual filtering with the created filtermasks. It can be specified what to do at the edges, the default optionis zero-padding. For example, G = imfilter(F, H); will filter theimage F with the filter mask H using zero-padding at the edges.
3.1 Averaging Filter Implementation
Implemented the averaging filter with different kernel sizes for comparison,3 by 3, 9 by 9 and 11 by 11. Using equation 2, this gives the following kernelvalues:
HAV G, 3 =
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
0.1111 0.1111 0.1111
10
HAV G, 9 =
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123
HAV G, 11 is an 11 by 11 matrix with values 0.0083.
Filtered the image cameraman with these three filters using zero-padding atthe edges, the result is shown in figure 8. The MATLAB-code can be foundin appendix A.1.
11
Original Image Filtered Image. Avg. mask: (3 x 3)
Filtered Image. Avg. mask: (9 x 9) Filtered Image. Avg. mask: (11 x 11)
Figure 8: Averaging using zero-padding
The image has been blurred, with more blurring as the mask size increases.The details (the high frequency components) have been attenuated, as ex-pected. The effects of zero padding can be seen as black borders around theedges.
Figure 9 shows the result of the same filtering operation when mirror re-flection is used at the edges instead of zero-padding. This results in imageswithout black borders at the edges. The MATLAB-code can be found inappendix A.2.
12
Original Image Filtered Image. Avg. mask: (3 x 3)
Filtered Image. Avg. mask: (9 x 9) Filtered Image. Avg. mask: (11 x 11)
Figure 9: Averaging with mirror-reflection
The blurring that the averaging filter gives is uniform, which is not what istypically wanted.
The next section shows an implementation of the Gaussian filter.
13
3.2 Gaussian Filter Implementation
An implementation of a Gaussian filter kernel is presented where the kernelsize is 9 by 9 and sigma is varied (σ = 0.8, 1, 2).
With σ = 0.8, the filter kernel is:
12
34
56
78
9
1
2
3
4
5
6
7
8
90
0.05
0.1
0.15
0.2
0.25
xy
z
Figure 10: 9 by 9 Gaussian kernel with σ = 0.8
HGAUSS, 9, 0.8 =
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.00000.0000 0.0000 0.0000 0.0001 0.0002 0.0001 0.0000 0.0000 0.00000.0000 0.0000 0.0005 0.0050 0.0109 0.0050 0.0005 0.0000 0.00000.0000 0.0001 0.0050 0.0521 0.1139 0.0521 0.0050 0.0001 0.00000.0000 0.0002 0.0109 0.1139 0.2487 0.1139 0.0109 0.0002 0.00000.0000 0.0001 0.0050 0.0521 0.1139 0.0521 0.0050 0.0001 0.00000.0000 0.0000 0.0005 0.0050 0.0109 0.0050 0.0005 0.0000 0.00000.0000 0.0000 0.0000 0.0001 0.0002 0.0001 0.0000 0.0000 0.00000.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
14
With σ = 1, the filter kernel is:
12
34
56
78
9
1
2
3
4
5
6
7
8
90
0.02
0.04
0.06
0.08
0.1
0.12
0.14
0.16
xy
z
Figure 11: 9 by 9 Gaussian kernel with σ = 1
HGAUSS, 9, 1 =
0.0000 0.0000 0.0000 0.0000 0.0001 0.0000 0.0000 0.0000 0.00000.0000 0.0000 0.0002 0.0011 0.0018 0.0011 0.0002 0.0000 0.00000.0000 0.0002 0.0029 0.0131 0.0215 0.0131 0.0029 0.0002 0.00000.0000 0.0011 0.0131 0.0586 0.0965 0.0586 0.0131 0.0011 0.00000.0001 0.0018 0.0215 0.0965 0.1592 0.0965 0.0215 0.0018 0.00010.0000 0.0011 0.0131 0.0586 0.0965 0.0586 0.0131 0.0011 0.00000.0000 0.0002 0.0029 0.0131 0.0215 0.0131 0.0029 0.0002 0.00000.0000 0.0000 0.0002 0.0011 0.0018 0.0011 0.0002 0.0000 0.00000.0000 0.0000 0.0000 0.0000 0.0001 0.0000 0.0000 0.0000 0.0000
15
With σ = 2, the filter kernel is:
12
34
56
78
9
1
2
3
4
5
6
7
8
90
0.005
0.01
0.015
0.02
0.025
0.03
0.035
0.04
0.045
xy
z
Figure 12: 9 by 9 Gaussian kernel with σ = 2
HGAUSS, 9, 2 =
0.0008 0.0018 0.0034 0.0050 0.0056 0.0050 0.0034 0.0018 0.00080.0018 0.0044 0.0082 0.0119 0.0135 0.0119 0.0082 0.0044 0.00180.0034 0.0082 0.0153 0.0223 0.0253 0.0223 0.0153 0.0082 0.00340.0050 0.0119 0.0223 0.0325 0.0368 0.0325 0.0223 0.0119 0.00500.0056 0.0135 0.0253 0.0368 0.0417 0.0368 0.0253 0.0135 0.00560.0050 0.0119 0.0223 0.0325 0.0368 0.0325 0.0223 0.0119 0.00500.0034 0.0082 0.0153 0.0223 0.0253 0.0223 0.0153 0.0082 0.00340.0018 0.0044 0.0082 0.0119 0.0135 0.0119 0.0082 0.0044 0.00180.0008 0.0018 0.0034 0.0050 0.0056 0.0050 0.0034 0.0018 0.0008
The original image, together with the filtered images are shown in figure 13.Zero-padding was used at the edges. The MATLAB-code can be found inappendix A.3.
16
Original Image Filtered Image. Gauss. mask: (9 x 9). Sigma = 0.8
Original Image Filtered Image. Gauss. mask: (9 x 9). Sigma = 0.8
Figure 13: 9 by 9 Gaussian kernels with σ = 0.8, 1, 2 using zero-padding
The images have been blurred. Compared to the regular average filter, thisfilter has given a better blurring model because the center pixel has a heigherweight than the surrounding ones.
Figure 14 shows the same filtering applied when using mirror-reflection atthe edges. The MATLAB-code can be found in appendix A.4.
17
Original Image Filtered Image. Gauss. mask: (9 x 9). Sigma = 0.8
Original Image Filtered Image. Gauss. mask: (9 x 9). Sigma = 0.8
Figure 14: 9 by 9 Gaussian kernels with σ = 0.8, 1, 2 using mirror-reflection
18
4 Conclusion
In this report, a brief presentation about linear filters in image processing hasbeen made. The convolution operation has been described and the basics ofthe averaging filter and the Gaussian filter have been presented. These filterswas also been implemented in MATLAB and the results were discussed. Itwas shown that the Gaussian filter gives a better blurring model than theaveraging filter.
19
References
[1] An Introduction to Digital Image Processing - Graphics Pro-gramming and Theory. http://www.gamedev.net/page/resources/_/technical/graphics-programming-and-theory/an-introduction-to-digital-image-processing-r2007. Accessed:Oct 12, 2012.
[2] Digital Filters. http://homepages.inf.ed.ac.uk/rbf/HIPR2/filtops.htm. Accessed: Oct 7, 2012.
[3] Digital Image Processing - Image Filtering. http://www.irisa.fr/temics/staff/lemeur/teaching/MiticVis_partII.pdf. Accessed:Oct 7, 2012.
[4] Gaussian Scale Space. http://igitur-archive.library.uu.nl/dissertations/2002-0904-155155/c2.pdf. Accessed: Oct 15, 2012.
[5] Image Processing Toolbox. http://www.mathworks.se/help/images/index.html. Accessed: Oct 12, 2012.
[6] David A. Forsyth and Jean Ponce. Computer Vision - A Modern Ap-proach 2nd Edition. Pearson.
[7] Alasdair McAndrew. An Introduction to Digital Image Processing withMATLAB. Technical report, Victoria University of Technology.
20
A MATLAB-Code
A.1 Averaging Filter (Zero-Padding at Edges)
Simple averaging with different kernel sizes. Zero-padding at the edges.
F = imread(’cameraman.tif’);
H_3 = fspecial(’average’, 3);H_9 = fspecial(’average’, 9);H_11 = fspecial(’average’, 11);
G_3 = imfilter(F, H_3);G_9 = imfilter(F, H_9);G_11 = imfilter(F, H_11);
subplot(2, 2, 1)imshow(F)title(’Original Image’)
subplot(2, 2, 2)imshow(G_3)title(’Filtered Image. Avg. mask: (3 x 3)’)
subplot(2, 2, 3)imshow(G_9)title(’Filtered Image. Avg. mask: (9 x 9)’)
subplot(2, 2, 4)imshow(G_11)title(’Filtered Image. Avg. mask: (11 x 11)’)
21
A.2 Averaging Filter (Mirror-Reflection at Edges)
Simple averaging with different kernel sizes. Mirror-reflection at the edges.
F = imread(’cameraman.tif’);
H_3 = fspecial(’average’, 3);H_9 = fspecial(’average’, 9);H_11 = fspecial(’average’, 11);
G_3 = imfilter(F, H_3, ’symmetric’);G_9 = imfilter(F, H_9, ’symmetric’);G_11 = imfilter(F, H_11, ’symmetric’);
subplot(2, 2, 1)imshow(F)title(’Original Image’)
subplot(2, 2, 2)imshow(G_3)title(’Filtered Image. Avg. mask: (3 x 3)’)
subplot(2, 2, 3)imshow(G_9)title(’Filtered Image. Avg. mask: (9 x 9)’)
subplot(2, 2, 4)imshow(G_11)title(’Filtered Image. Avg. mask: (11 x 11)’)
22
A.3 Gaussian Filter (Zero-Padding at Edges)
Gaussian filtering, kernel size 9 by 9 and varying sigma-values. Zero-paddingat the edges.
F = imread(’cameraman.tif’);
H_9_0_8 = fspecial(’gaussian’, 9, 0.8);H_9_1 = fspecial(’gaussian’, 9, 1);H_9_2 = fspecial(’gaussian’, 9, 2);
G_9_0_8 = imfilter(F, H_9_0_8);G_9_1 = imfilter(F, H_9_1);G_9_2 = imfilter(F, H_9_2);
subplot(2, 2, 1)imshow(F)title(’Original Image’)
subplot(2, 2, 2)imshow(G_9_0_8)title(’Filtered Image. Gauss. mask: (9 x 9). Sigma = 0.8’)
subplot(2, 2, 3)imshow(G_9_1)title(’Filtered Image. Gauss. mask: (9 x 9). Sigma = 1’)
subplot(2, 2, 4)imshow(G_9_2)title(’Filtered Image. Gauss. mask: (9 x 9). Sigma = 2’)
23
A.4 Gaussian Filter (Mirror-Reflection at Edges)
Gaussian filtering, kernel size 9 by 9 and varying sigma-values. Mirror-reflection at the edges.
F = imread(’cameraman.tif’);
H_9_0_8 = fspecial(’gaussian’, 9, 0.8);H_9_1 = fspecial(’gaussian’, 9, 1);H_9_2 = fspecial(’gaussian’, 9, 2);
G_9_0_8 = imfilter(F, H_9_0_8, ’symmetric’);G_9_1 = imfilter(F, H_9_1, ’symmetric’);G_9_2 = imfilter(F, H_9_2, ’symmetric’);
subplot(2, 2, 1)imshow(F)title(’Original Image’)
subplot(2, 2, 2)imshow(G_9_0_8)title(’Filtered Image. Gauss. mask: (9 x 9). Sigma = 0.8’)
subplot(2, 2, 3)imshow(G_9_1)title(’Filtered Image. Gauss. mask: (9 x 9). Sigma = 1’)
subplot(2, 2, 4)imshow(G_9_2)title(’Filtered Image. Gauss. mask: (9 x 9). Sigma = 2’)
24