### Read **Part 2**

Spatial filtering techniques refer to those operations where the resulting value of a pixel at a given coordinate is a function of the original pixel value at that point as well as the original pixel value of some of its neighbours. These operations are classified into linear filtering operations and non-linear filtering operations.

In linear filters, the resulting output pixel is computed as a sum of products of the pixel values and mask coefficients in the pixelâ€™s neighbourhood in the original image. In other words, 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 referred to as a linear filter. Mean filter is an example of linear filter.

In non-linear filters, the resulting output pixel is selected from an ordered sequence of pixel values in the pixelâ€™s neighbourhood in the original image. Median filter is an example of non-linear filter.

## Linear filters

Convolution and correlation are the two fundamental mathematical operations involved in linear filters based on neighbourhood-oriented image processing algorithms.

### Convolution

Convolution processes an image by computing, for each pixel, a weighted sum of the values of that pixel and its neighbours. Depending on the choice of weights, a wide variety of image processing operations can be implemented.

Different convolution masks produce different results when applied to the same input image. These operations are referred to as filtering operations and the masks as spatial filters. Spatial filters are often named based on their behaviour in the spatial frequency. Low-pass filters (LPFs) are those spatial filters whose effect on the output image is equivalent to attenuating the high-frequency components (fine details in the image) and preserving the low-frequency components (coarser details and homogeneous areas in the image). These filters are typically used to either blur an image or reduce the amount of noise present in the image. Linear low-pass filters can be implemented using 2D convolution masks with non-negative coefficients.

High-pass filters (HPFs) work in a complementary way to LPFs, that is, these preserve or enhance high-frequency components with the possible side-effect of enhancing noisy pixels as well. High-frequency components include fine details, points, lines and edges. In other words, these highlight transitions in intensity within the image.

There are two in-built functions in MATLABâ€™s Image Processing Toolbox (IPT) that can be used to implement 2D convolution: conv2 and filter2.

conv2 computes 2D convolution between two matrices. For example, C=conv2(A,B) computes the two-dimensional convolution of matrices A and B. If one of these matrices describes a two-dimensional finite impulse response (FIR) filter, the other matrix is filtered in two dimensions.

filter2 function rotates the convolution mask, that is, 2D FIR filter, by 180Â° in each direction to create a convolution kernel and then calls conv2 to perform the convolution operation.

Y=filter2(h,X) filters the data in â€˜Xâ€™ with the two-dimensional FIR filter in the matrix â€˜h.â€™ It computes the result â€˜Yâ€™ using two-dimensional correlation, and returns the central part of the correlation that is of the same size as â€˜X.â€™

Y=filter2(h,X,shape) returns the part of â€˜Yâ€™ specified by the shape parameter. Shape is a string with one of these values:

1. â€˜fullâ€™ returns the full two-dimensional correlation. In this case, â€˜Yâ€™ is larger than â€˜X.â€™

2. â€˜sameâ€™ (default) returns the central part of the correlation. In this case, â€˜Yâ€™ is of the same size as â€˜X.â€™

3. â€˜validâ€™ returns only those parts of the correlation that are computed without zero-padded edges. In this case, â€˜Yâ€™ is smaller than â€˜X.â€™

There is no single best approach to use the filter2 function. However, the method is dictated by the problem at hand.

One can create filter by hand or by using the fspecial function. fspecial is an IPT function designed to simplify the creation of common 2D image filters. Its syntax is:

*h = fspecial(type, parameters)*

where:

1. â€˜hâ€™ is the filter mask

2. â€˜typeâ€™ is one of these: â€˜averageâ€™â€”averaging filter, â€˜diskâ€™â€”circular averaging filter, â€˜gaussianâ€™â€”Gaussian low-pass filter, â€˜laplacianâ€™â€”2D Laplacian operator, â€˜logâ€™â€”Laplacian of Gaussian (LoG) filter, â€˜motionâ€™â€”approximates the linear motion of a camera, â€˜prewittâ€™ and â€˜sobelâ€™â€”horizontal edge-emphasising filters, â€˜unsharpâ€™â€”unsharp contrast-enhancement filter)

3. â€˜parametersâ€™ are optional and vary depending on the type of filter; for example, mask size, standard deviation (for â€˜gaussianâ€™ filter) and so on

imfilter is another command for implementing linear filters in MATLAB.

The syntax for imfilter is:

*g = imfilter(f, h, mode, boundary_*

* options, size_options);*

Where â€˜fâ€™ is the input image, â€˜hâ€™ is the filter mask, and â€˜modeâ€™ can be either â€˜convâ€™ or â€˜corr,â€™ indicating whether filtering will be done using convolution or correlation (which is the default), respectively.

boundary_options refer to how the filtering algorithm should treat border values. There are four possibilities:

1.** X.** The boundaries of the input array (image) are extended by padding with a value â€˜X.â€™ This is the default option (with X=0).

where is part 4?

Thank you for your comment. We have updated the article with 4th part link. You can access the 4th article here.