# Image Enhancement by Filtering in the spatial domain – Average Filters | MATLAB Tutorial

### Image Enhancement

When it comes to images, we are usually bothered by two different domains – spatial domain and frequency domain. Spatial filtering derives its name from the term “space”. One misconception that most people have is that the filters in frequency domain and spatial domain are different from each other. The truth is that any spatial filter can be transformed into a frequency domain filter as we will see in a later tutorial.

Spatial filtering term is the filtering operations that are performed directly on the pixels of an image.

### How do spatial filters work?

Spatial filters are also referred to as mask filters because they work like a mask. Basically, just like a mask, it covers a region of the image (called sub-image) and then transforms it.

Consider a grid like an image and a mask of size 3 x 3. This means that at a time, this mask can cover only 3×3 size sub-image.

Figure – 1: Pixels of image

Mathematically, the spatial filters change the value of the central pixel to the new value which usually depends on the whole sub-image it covers. For example, consider the following filter:

Figure – 2: Coefficients of the filter

This filter (assuming it to be a linear filter) will change the value of the central filter to:

If it would have been a non-linear filter, then there won’t be any linear combination of coefficients and pixel values.

### Types of spatial filters

In general, spatial filters can be divided into the following categories:

1. Smoothing filters – They can cause the blurring effect in images. Also known as low pass filters.

2.Sharpening filters – They can be used for edge detection. High pass filters come under this category.

Filters can also be classified as linear and non-linear filters.

### Linear filters

In general, linear filtering of an image f of size MxN with a filter mask of size mxn is given by the expression:

As a general rule, we take M and N as odd numbers only. This makes sure that we locate the central pixel without any issue.

### Non-linear filters

Nonlinear spatial filters also operate on neighborhoods, and the mechanics of sliding a mask past an image are the same as was just outlined. The filtering operation is based conditionally on the values of the pixels in the neighborhood under consideration.

### Smoothing filters

They are used for:

- Blurring
- Noise reduction

### Averaging filters

These type of filters basically substitute the central pixel value with the average of the sub-image. The common averaging filters are – standard average filter, and weighted average filter.

Let’s implement both of these filters in MATLAB.

### Standard Average Filter

%% Weighted average filter % Program for weighted average filter clear all close all %% Load the image img = imread('cameraman.tif'); %% Convert the image to double img = double(img); %% Show the image figure; subplot(211); imshow(img,[]); title('Original image'); %% Get size of image PQ = size(img); M = PQ(1); N = PQ(2); %% Specify the mask mask = [1, 1, 1; 1, 1, 1; 1, 1, 1]; %% Apply the filter for i = 2:M-1 for j = 2:N-1 subImg = img(i-1:i+1,j-1:j+1); img(i,j) = 1/9 * sum(sum(mask.*subImg)); end end %% Show the filtered image subplot(212); imshow(img,[]); title('Filtered image');

Figure – 5: Result of standard average filter

## Weighted average filter

%% Weighted average filter % Program for weighted average filter clear all close all clc %% Load the image img = imread('cameraman.tif'); %% Convert the image to double img = double(img); %% Show the image figure; subplot(211); imshow(img,[]); title('Original image'); %% Get size of image PQ = size(img); M = PQ(1); N = PQ(2); %% Specify the mask mask = [1, 2, 1; 2, 4, 2; 1, 2, 1]; %% Apply the filter for i = 2:M-1 for j = 2:N-1 subImg = img(i-1:i+1,j-1:j+1); img(i,j) = 1/16 * sum(sum(mask.*subImg)); end end %% Show the filtered image subplot(212); imshow(img,[]); title('Filtered image');

Figure – 6: Result of weighted average filter

## Design your own average filter

Notice carefully the similarity between the standard average filter and the weighted average filter. You will see that the masks are being multiplied by a constant. Observe that the constant is nothing but the reciprocal of the sum of all coefficients of the mask.

Keeping this in mind, you can design your own weighted average filter.

%% Custom Weighted average filter % Program for custom average filter function customAvgFil(imgName,mask) close all clc %% Load the image img = imread(imgName); %% Convert the image to double img = double(img); %% Show the image figure; subplot(211); imshow(img,[]); title('Original image'); %% Get size of image PQ = size(img); M = PQ(1); N = PQ(2); %% Apply the filter for i = 2:M-1 for j = 2:N-1 subImg = img(i-1:i+1,j-1:j+1); img(i,j) = (1/sum(sum(mask))) * sum(sum(mask.*subImg)); end end %% Show the filtered image subplot(212); imshow(img,[]); title('Filtered image'); end

Sample run: customAvgFil(‘cameraman.tif’,[1,2,3;5,7,11;13,17,19]);

Figure – 7: Result for **customAvgFil(‘cameraman.tif’,[1,2,3;5,7,11;13,17,19]);**