Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • Fundamentals of Spatial Filtering: The name filter is borrowed from frequency domain processing, where ‘filtering’ refers to accepting (passing) or rejecting certain frequency components. For example, a filter that passes low frequencies is called a lowpass filter. The net effect produced by a lowpass filter is to blur (smooth) an image . We can accomplish a similar smoothing directly on the image itself by using spatial filters (also called spatial masks, kernels, templates, and windows). © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • The MATLAB IPT implements linear spatial filtering using function imfilter which has the following syntax: g=imfilter(f, w, filtering_mode, boundary_options, size_options) where f is the input image, w is the filter mask, g is the filtered result, and the other parameters are summarized in the table; The filtering_mode specifies whether to filter using correlation(‘corr’) or convolution (‘conv’); The boundary_options deal with the border-padding issue, with the size of the border being determined by the size of the filter. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • The size_options are either ‘same’ or ‘full’ . ‘full’: The output is of the same size as the extended (padded) image. ‘same’: The output is the same size as the input. This is the default. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering g=imfilter (f, w, ‘conv’, ‘replicate’) f=imread(‘squares.tiff’); w=ones(31); gd=imfilter(f,w); imshow(gd, [ ]); Where we used the default boundary option, which pads of the image with 0’s(black). As expected the edges between black and white in the filtered image are blurred. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • Non Linear Spatial Filtering: Order-statistic filters are nonlinear spatial filters whose response is based on filters whose response is based on ordering (ranking) the pixels contained in the image area. The best known filter in this category is the median filter, which as its name implies, replace the value of a pixel by median of the intensity values in the neighborhood of that pixel. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • Median Filter: Median filters are quite popular because, for certain types of random noise, they provide excellent noisereduction capabilities with considerably less blurring than linear smoothing filters of similar size. Median filters are particularly effective in the presence of impulsive noise also called salt and pepper noise. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • The syntax of function ordfilt2 is; g=ordfilt2(f, order, domain) This function creates the output image g by replacing each element of f by the order-th element in the sorted set of neighbor specified by the non zero elements in domain. Here, domain is an mxn matrix of 1s and 0s that specify the pixel locations in the neighborhood that are to be used in the computation. In this sense, domain acts like a mask. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • For example, to implement a min filter (order 1) of size mxn use the syntax g=ordfilt2(f, 1, ones(m,n)); In this formulation the 1 denotes the 1st sample in the ordered set of mn samples. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • For example, to implement a max filter (100th percentile) of size mxn use the syntax g=ordfilt2(f, m*n, ones(m,n)); © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • The best known order-statistic filter in digital image processing is the median filter, which corresponds to the 50th percentile. g=ordfilt2(f, median(1:m*n), ones(m,n)) • Where median(1:m*n) simply computes the median of the ordered sequence 1,2,…,mn. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • Sharpening Spatial Filters The principle objective of sharpening is to highlight transformations in intensity. We saw that image blurring could be accomplished in the spatial domain by pixel averaging in a neighborhood. Because averaging is analogous to integration, it is logical to conclude that sharpening can be accomplished by spatial differentiation. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • The derivatives of a digital function are defined in terms of differences. – First Derivative: 1. 2. 3. – must be zero in areas of constant intensity must be non zero at the onset of an intensity step or ramp Must be non zero along ramps Second Derivative: 1. 2. 3. Must be zero in constant areas Must be non zero at the on set and end of an intensity step or ramp Must be zero along ramps of constant slope. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • • Edges in digital images often are ramp-like transitions in intensity, in which case the first derivative of the image would result in thick edges because the derivative is nonzero along a ramp. On the other hand, the second derivative would produce a double edge on pixel thick, separated by zeros. From this, we conclude that the second derivative enhances fine details much better than the first derivative, a property that is ideally suited for sharpening images. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • Using Second Derivative for Image Sharpening-The Laplacian We are interested in isotropic filters, whose response is independent of the direction of the discontinuities in the image to which the filter is applied. In other words isotropic filters are rotation invariant, in the sense that rotating the image and then applying the filter gives the same result as applying the filter to the image first and then rotating the result. It can be shown (Rosenfeld and Kak [1982]) that the simplest isotropic derivative operator is the Laplacian, which, for a function(image) f(x,y) of two variables, is defined as © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering 2 f 2 f f 2 2 x y 2 2 f f ( x 1, y ) f ( x 1, y ) 2 f ( x, y ) 2 x 2 f f ( x, y 1) f ( x, y 1) 2 f ( x, y ) 2 y Therefore, it follows from the preceeding three equations that the discrete Laplacian of two variables is 2 f ( x, y) f ( x 1, y) f ( x 1, y) f ( x, y 1) f ( x, y 1) 4 f ( x, y) © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • Because the Laplacian is a derivative operator, its use highlights intensity discontinuities in an image and deemphasizes regions with slowly varying intensity levels. This will tend to produce images that have grayish edge lines and other discontinuities, all superimposed on a dark, featureless background. • Shapening effect is obtained by adding the Laplacian image to the original. g ( x, y) f ( x, y) c[2 f ( x, y)] Where f(x,y) and g(x,y) are the input and sharpened images, respectively. The constant c=-1if the Laplacian filter in Fig3.37.(a) or (b) are used, and c=1 if either of the other two filters is used. © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering Unsharp Masking and Highboost Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • Unsharp Masking and Highboost Filtering 1. 2. Blur the original image Subtract the blured image from the original (the resulting difference is called the mask) 3. Add the mask to the original • Letting f ’(x,y) denote the blured image, unsharp masking is expressed in equation form as follows. First obtain the mask: gmask(x,y)=f(x,y)-f ’(x,y) • Then we add a weighted portion of the mask back to the original image: g(x,y)=f(x,y)+k* gmask(x,y) When k=1, we have unsharp masking When k>1, the process is referred to as highboost filtering When k<1, de-emphasizes the contribution of the unsharp mask © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering M ( x, y ) m ag(f ) g x2 g y2 © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • We can extract and display vertical edges in the image, f, using commands as follows; [gv t]=edge (f, ‘sobel’,’vertical’); imshow(gv); t • We can clean up the weaker edges somewhat by specifying a higher threshold value [gv t]=edge (f, ‘sobel’, 0.15, ‘vertical’); or for the both direction [gv t]=edge (f, ‘sobel’, 0.15 ); © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering • Function edge does not compute Sobel edges at +/-45o. To compute such edges, we need to specify the mask and use imfilter; w45=[-2 -1 0; -1 0 1; 0 1 2]; g45=imfilter (double(f), w45, ‘replicate’); T=0.3*max(abs(g45( : ) )); g45=g45>=T; figure imshow(g45); • Similarly, using the mask below with the same sequence of commands resulted in the strong edges oriented at -45o. wm45=[0 1 2; -1 0 1; -2 -1 0] © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods Digital Image Processing, 3rd ed. Gonzalez & Woods www.ImageProcessingPlace.com Chapter 3 Intensity Transformations & Spatial Filtering © 1992–2008 R. C. Gonzalez & R. E. Woods