### 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.