Basic Filtering MATLAB


To start work in CV or IP we should have a input image to do some experiments on it. Go to google and type lena. You will get lena standard image like I added below.

Lena image


Some words about Miss Lena:

  • Lena is most famous image to perform compression, IP and CV.
  • Lena digitized picture was appear on playboy in 1972.
  • Lena Soderberg (ne Sjööblom) was last reported living in her native Sweden, happily married with three kids and a job. if you want to more about lena go through following article Lena .


Steps we are going to perform:

  1. Read the Lina image as input.
  2. Generate random noise.
  3. Add noise to input image.
  4. Apply average filter and gaussian filter to clean the noisy image



Figure shows the mask used to filter image.

  • Random noise is random in nature so if we take the average of random noise its tends to cancel out each other.
  • In above average filter we assign the equal weight to all 25 pixel that is 1/25 .
  • In Gaussian filter instead of providing equal weight we distribute the weights in gaussian manner so in center max weight will be assign.


Result of Average filter and Gaussian filter on noisy image.

MATLAB code:

%%%%%%%%%%%%%%%%%% Code Start %%%%%%%%%%%%%%%%%%%%%%%%%%

clc    % clear the screen

clear all  % clear all variables

close all % close all MATLAB windows

%% read Image

% set path to read image from system

ImagePath = ‘D:\DSUsers\uidp6927\image_processingCode\lena.jpg’

img_RGB = imread(ImagePath); % read image from path

img_RGB = im2double(img_RGB);  % Convert image to double precision

img_gray = rgb2gray(img_RGB);   % convert image to gray

[row,col,channel] = size(img_gray);    % get size of image

% select noise factor

% noise can be controlled by noise factor.

noise_factor = 0.7;

% generate noise

noise = noise_factor*rand(row,col);

% Generate noisy image by adding noise to input image.

noisy_img = 1/2*(img_gray + noise);

%generate average filter with help of fspecial command.

h_ave = fspecial(‘average’,11);

%apply average filter to noisy image

avg_filter = imfilter(noisy_img,h_ave,’replicate’);

%generate gaussian filter of size 11×11 with sigma 1

h_gauss = fspecial(‘gaussian’,11,1);

% apply gaussian filter to noisy image

gauss_filter = imfilter(noisy_img, h_gauss,’replicate’);

%plot all images for results




title(‘ Average filter mask’)



title(‘Gaussian filter mask’);




title(‘Input image’);



title(‘Noisy image’);



title(‘Average image’);



title(‘Gaussian image’);


%%%%%%%%%%%%%%%%%%%%%%%%Code End %%%%%%%%%%%%%%%%%%%%%%


  • fspecial is used to generate filter mask. Type help fspecial in Matlab command window for more detail.
  • imfilter command is used to convolve 2 D filter mask with image.
  • In next post will discuss filtering in frequency domain.




Happy Learning







Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s