Post on 19-Dec-2015
Filtering in the Spatial Domain
• Filtering the spatial domain is achieved by convolution
• Qualitatively: Slide the filter to each position, x, then sum up the function multiplied by the filter at that position
duuxgufgfxh )()()(
Convolution Theorem
• Convolution in the spatial domain is the same as multiplication in the frequency domain– Take a function, f, and compute its Fourier transform,
F– Take a filter, g, and compute its Fourier transform, G– Compute H=FG– Take the inverse Fourier transform of H, to get h– Then h=fg
• Multiplication in the spatial domain is the same as convolution in the frequency domain
Filtering Images
• Work in the discrete spatial domain
• Convert the filter into a matrix, the filter mask
• Move the matrix over each point in the image, multiply the entries by the pixels below, then sum– eg 3x3 box filter– averages
111
111
111
9
1
• filter2(filter,image,shape)• filter2(filter,image,'same') is the default; it produc
es a matrix of equal size to the original image matrix.
• filter2(filter,image,'valid') applies the mask only to inside pixels.
• filter2(filter,image,'full') returns a result larger than the original; it does this by padding with zero, and applying the lter at all places on and around the image where the mask intersects the image matrix.
• fspecial function; this has many options which makes for easy creation of many different filters.
• fspecial('average',[5,7]) will return an averaging filter of size 5x7
• fspecial('average',11) will return an averaging filter of size 11x11
• If we leave out the final number or vector, the 3 x3 averaging filter is returned.
• c=imread('cameraman.tif');
• f1=fspecial('average');
• cf1=filter2(f1,c);
• figure,imshow(c),figure,imshow(cf1/255)
Using a 9x9 filterUsing a 25x25 filter
c=imread('cameraman.tif');f9=fspecial('average', 9); cf9=filter2(f9,c); figure,imshow(c),figure,imshow(cf9/255)
f25=fspecial('average', 25); cf25=filter2(f25,c); figure,imshow(c),figure,imshow(cf25/255)
• f=fspecial('laplacian')
• f =
• 0.1667 0.6667 0.1667• 0.6667 -3.3333 0.6667• 0.1667 0.6667 0.1667
• >> cf=filter2(f,c);• imshow(cf/100)
Laplacian of Gaussian (log) Filter
• f1=fspecial('log')
• f1 =
• 0.0448 0.0468 0.0564 0.0468 0.0448• 0.0468 0.3167 0.7146 0.3167 0.0468• 0.0564 0.7146 -4.9048 0.7146 0.0564• 0.0468 0.3167 0.7146 0.3167 0.0468• 0.0448 0.0468 0.0564 0.0468 0.0448
• >> cf1=filter2(f1,c);• figure,imshow(cf1/100)In each case, the sum of all the lter elements is zero.
• cf1=filter2(f1,c);
• figure,imshow(cf1/100)
• >> f2=[1 -2 1;-2 4 -2;1 -2 1];
• cf2=filter2(f2,c);
• >> figure,imshow(mat2gray(cf2));
mat2gray function automatically scales the matrix elements to displayable values
• maxcf2=max(cf2(:));
• mincf2=min(cf2(:));
• f2g=(cf2-mincf2)/(maxcf2-mincf2);
• >> imshow(cf2g)
• >> figure,imshow(cf2/60)
• We can generally obtain a better result by dividing the result of the filtering by a constant before displaying it:
Edge sharpening• p=imread('pelicans.tif');
• u=fspecial('unsharp',0.5);
• pu=filter2(u,p);
• imshow(p),figure,imshow(pu/255)
Non-linear Filters
• maximum filter, which has as its output the maximum value
• minimum filter, which has as its output the minimum value
• rank-order filters. In such a filter, the elements under the mask are ordered
• Colfilt function, which rearranges the image into columns first.
• median filter, which takes the central value of the ordered list.