# Matlab – Spatial Filtering

Filtering is a technique for modifying or enhancing an image. As it name stated, spatial filtering use filter or also know as mask, kernel, template or window. Values of filter sub image are referred to coefficients rather than pixel intensities. Masks are always in odd size such as 3×3, 3×5 or etc. You won’t be able to find the center of the mask if it’s in even size. The process is simply moving the filter mask from point to point in an image. The filter oat each point is calculated using predefined relationship.

There are various methods of Spatial Filtering approaches. There are Linear Filtering, Non-linear Filtering and Adaptive Filtering. Take note that Order-Statistics Filtering and Morphological Filtering is fall under Non-Linear Filtering. Linear filtering of an image can be done through an operation called convolution or correlation.

Smoothing Spatial Filters use for blurring images and noise reduction. Noise reduction can be accomplished by blurring with linear filters or non-linear filters. Smoothing Linear Filters also known as averaging filters or lowpass filters. There also exist weighted averaging filter. Instead of averaging all the pixel values in the window, it give the closer by pixels higher weighting, and far away pixels lower weighting. Another popular smoothing filter is Gaussian filter.

Order-Statistics filtering as mention earlier is part of Non-Linear Filtering. The response is based on ranking or ordering the pixels contained in the image area encompassed by the filter.

Now we take a look on example of applying Spatial Filter on Matlab

%create image x

x=[140 160 180 180 10 11 12 10;…
180 250 160 170 11 13 12 11;…
165 150 160 165 13 12 11 12;…
165 175 175 160 13 11 10 12;…
90 50 90 50 90 50 90 50;…
50 90 50 90 50 90 50 90;…
90 50 90 50 90 50 90 50;…
50 90 50 90 50 90 50 90];

%Create averaging filter
a=ones(3,3)/9;
%Read image x, filter with filter ‘a’ (averaging filter) using zero padding
aa=round(filter2(a,x,’same’));
%filter only inside portion of the image
bb=round(filter2(a,x,’valid’));

%%Another example using Laplacian mask
%create image c
c=[0 1 0;…
1 -4 1;…
0 1 0];

%Read image x, filter with filter ‘c’ using zero padding
cc=round(filter2(c,x,’same’));

%filter only inside portion of the image
dd=round(filter2(c,x,’valid’));

Have a good look on the Matlab code. That’s All. Thank You