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:
- Read the Lina image as input.
- Generate random noise.
- Add noise to input image.
- 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
figure,
subplot(2,1,1),
surf(h_ave);
title(‘ Average filter mask’)
subplot(2,1,2),
surf(h_gauss);
title(‘Gaussian filter mask’);
figure,
subplot(2,2,1)
imshow(img_gray,[])
title(‘Input image’);
subplot(2,2,2)
imshow(noisy_img,[]);
title(‘Noisy image’);
subplot(2,2,3)
imshow(avg_filter,[])
title(‘Average image’);
subplot(2,2,4)
imshow(gauss_filter,[])
title(‘Gaussian image’);
%%%%%%%%%%%%%%%%%%%%%%%%Code End %%%%%%%%%%%%%%%%%%%%%%
Note*
- 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
Cheers