Digital Image Processing Chapter 3 Neighbourhood Processing.
Neighbourhood Processing. We have seen in chapter 2 that an image can be modified by applying a particular function to each pixel value. Neighbourhood processing may be considered as an extension of this, where a function is applied to a neighbourhood of each pixel. The idea is to move a “mask”: a rectangle (usually with sides of odd length) or other shape over the given image. As we do this, we create a new image whose pixels have grey values calculated from the grey values under the mask. The combination of mask and function is called a filter. If the function by which the new grey value is calculated is a linear function of all the grey values in the mask, then the filter is called a linear filter. A linear filter can be implemented by multiplying all elements in the mask by corresponding elements in the neighbourhood , and adding up all these products..
Spatial filtering thus requires three steps: position the mask over the current pixel, Form all products of filter elements with the corresponding elements of the neighbourhood . Add up all the products. This must be repeated for every pixel in the image. Allied to spatial filtering is spatial convolution. The method for performing a convolution is the same as that for filtering, except that the filter must be rotated by 180 degree before multiplying and adding. Note also that in practice, most filter masks are rotationally symmetric, so that spatial filtering and spatial convolution will produce the same output..
3/13/2022. 4.
Smoothing filters are used for blurring and for noise reduction.
4/8/2022. 6. Two Smoothing Averaging Filter Masks.
Some Matlab Matrix generation function: >> magic(n) is an N-by-N matrix constructed from the integers >> eye(N) is an N-by-N matrix with 1's on the diagonal and zeros elsewhere. eye(M,N) is an M-by-N matrix. >> ones(N) is an N-by-N matrix of ones. ones(M,N) is an M-by-N matrix of ones. >> zeros(N) is an N-by-N matrix of zeros. zeros(M,N) is an M-by-N matrix of zeros. >> randn (N) returns an N-by-N matrix containing random values drawn from the standard normal distribution. randn (M,N) returns an M-by-N matrix. >> randi (IMAX,N) returns an N-by-N matrix containing random integer values drawn from the discrete uniform distribution on 1:IMAX. randi (IMAX,M,N) returns an M-by-N matrix..
Example of Spatial filtering. 3/13/2022. 8.
Edges of the image. Ignore the edges. That is, the mask is only applied to those pixels in the image for with the mask will lie fully within the image. This means all pixels except for the edges, and results in an output image which is smaller than the original. If the mask is very large, a significant amount of information may be lost by this method. We applied this method in our example above. “Pad” with zeros. We assume that all necessary values outside the image are zero. This gives us all values to work with, and will return an output image of the same size as the original, but may have the effect of introducing unwanted artifacts (for example, edges) around the image..
Filtering in Matlab. The filter2 function does the job of linear filtering for us; its use is >> filter2( filter,image,shape ) and the result is a matrix of data type double . The parameter shape is optional, it describes the method for dealing with the edges: >>filter2( filter,image,’same ’) is the default; it produces a matrix of equal size to the original image matrix. It uses zero padding. >>filter2( filter,image,’valid ’) applies the mask only to “inside” pixels. The result will always be smaller than the original:.
3/13/2022. 11.
3/13/2022. 12.
3/13/2022. 13. >> filter2( filter,image,’full ’) returns a result larger than the original; it does this by padding with zero, and applying the filter at all places on and around the image where the mask intersects the image matrix..
3/13/2022. 14. We can create our filters by hand, or by using the fspecial function ; this has many options which makes for easy creation of many different filters. We shall use the average option, which produces averaging filters of given size; thus >> fspecial (’average’,[5,7]) will return an averaging filter of size 5*7 more simply >> fspecial (’average’,11) will return an averaging filter of size 11*11. If we leave out the final number or vector, the averaging filter is returned. >> c= imread (’ cameraman.tif ’); >> f1= fspecial (’average’); >> cf1=filter2(f1,c); >> figure,imshow (c), figure,imshow (cf1/255).
3/13/2022. 15. (a) Original image (c) Using a 9 X 9 filter (b) Average filtering (d) Using a 25 x 25 filter.
Frequencies; low and high pass filters. The frequencies of an image are a measure of the amount by which grey values change with distance. High frequency components are characterized by large changes in grey values over small distances; example of high frequency components are edges and noise. Low frequency components, on the other hand, are parts of the image characterized by little change in the grey values. These may include backgrounds, skin textures. high pass filter if it “passes over” the high frequency components, and reduces or eliminates low frequency components, low pass filter if it “passes over” the low frequency components, and reduces or eliminates high frequency components,.
3/13/2022. 17. is a high pass filter. We note that the sum of the coefficients (that is, the sum of all e elements in the matrix), in the high pass filter is zero. This means that in a low frequency part of an image, where the grey values are similar, the result of using this filter is that the corresponding grey values in the new image will be close to zero..
3/13/2022. 18. High pass filter mask in Matlab. imshow ( cf /255) >>cf1=filter2(f1,c); >> figure,imshow (cf1/255).
(a) Laplacian filter (b) Laplacian of Gaussian ("log") filtering.
Clip values. We apply the following thresholding type operation to the grey values produced by the filter to obtain a displayable value.
3/13/2022. 21. Values outside the range 0–255. Scaling transformation. Suppose the lowest grey value produced by the filter if gL and the highest value is gH . We can transform all values in the range gL – gH to the range 0–255 by the linear transformation illustrated below:.
3/13/2022. 22.
Edge sharpening. Spatial filtering can be used to make edges in an image slightly sharper and crisper, which generally results in an image more pleasing to the human eye. The operation is variously called “edge enhancement”, “edge crispening”, or “unsharp masking”. Unsharp masking The idea of unsharp masking is to subtract a scaled “unsharp” version of the image from the original. In practice, we can achieve this affect by subtracting a scaled blurred image from the original..
Edge sharpening. 3/13/2022. 24. >> f= fspecial (’average’); >> xf =filter2( f,x ); >> xu=double(x)- xf /1.5 >> imshow (xu/70) The last command scales the result so that imshow displays an appropriate image; the value may need to be adjusted according to the input image..
Non-linear filters. A non-linear filter is obtained by a non-linear function of the greyscale values in the mask. Simple examples are the maximum filter, which has as its output the maximum value under the mask, and the corresponding minimum filter, which has as its output the minimum value under the mask. Both the maximum and minimum filters are examples of rank-order filters. In such a filter, the elements under the mask are ordered, and a particular value returned as output. So if the values are given in increasing order, the minimum filter is a rank-order filter for which the first element is returned, and the maximum filter is a rank-order filter for which the last element is returned. >> cmax = nlfilter (c,[3,3],’max(x(:))’); The nlfilter function requires three arguments: the image matrix, the size of the filter, and the function to be applied. >> cmin = nlfilter (c,[3,3],’min(x(:))’);.
(a) Using a maximum filter (b) Using a minimum filter.
>> cmax =ordfilt2(c,9,ones(3,3)); and the minimum filter can be applied with >> cmin =ordfilt2(c,1,ones(3,3)); We could apply the median filter with >> cmed =ordfilt2(c,5,ones(3,3)); However, the median filter has its own command , medfilt2,.
4/8/2022. 28. Example: Use of Median Filtering for Noise Reduction.
thanks. 3/13/2022. 29.