Image Noise Removal by Median filter using MATLAB (with inbuilt and without inbuilt function)

 Image Salt and pepper (Impulse) noise removal by Median filter using Matlab

Introduction:

        Noise removal technique is a strategy that helps to remove or minimize noise from the image. Usually, noise occurs during the acquisition of the image. And externally, noise is also added to measure the efficiency of the algorithm. Various types of noise and noise filter methods are available. Here we going to discuss salt and pepper noise and median filter.


What is salt and pepper noise?

Definition: 

    Salt and pepper noise images look like black and white pixels in the image at random. To obtain the salt and pepper noise image from the original image, black (0) and white (255) were added randomly to the original image.

What is a median filter?

Definition:

    The median is determined by first sorting all pixel values from the nearby area (window area) in ascending order and then exchanging the pixel being viewed with the center image pixel value.



Figure 1. Median filter 

Click here to download the full code 

MATLAB Code: Salt and pepper and median filtering using inbuilt function


% clearing command window
clc
% cleaning all variable
clear 
% closing all figure
close all

% read inbuilt image from matlab
input_image=imread('football.jpg');

% display function
figure,imshow(input_image);
title('Original RGB image')

% Convert RGB image to gray image
gray_image=rgb2gray(input_image);

% display function
figure,imshow(gray_image);
title('Gray image')

% resize the image
image_resize=imresize(gray_image,[256,256]);

% display function
figure,imshow(image_resize);
title('Resized image')

% adding salt and pepper noise using 'imnoise' inbuilt command
noise_added_image=imnoise(image_resize,'salt & pepper',0.2);

% display function
figure,imshow(noise_added_image);
title('Salt and pepper noise added image')

% Noise removal using 'medfilt2' median filter inbuilt command
noise_removed_image=medfilt2(noise_added_image,[3,3]);

% display function
figure,imshow(noise_removed_image);
title('Salt and pepper noise removed image')

% Median filter Performance
% Mean squared error
mse_value=mse(image_resize,noise_removed_image)
% Structural Similarity Index
ssim_value=ssim(image_resize,noise_removed_image)
% Peak Signal-To-Noise Ratio
psnr_vale=psnr(image_resize,noise_removed_image)

MATLAB Code: Salt and pepper user-defined and median filtering inbuilt function


% clearing command window
clc
% cleaning all variable
clear 
% closing all figure
close all

% read inbuilt image from matlab
input_image=imread('football.jpg');

% display function
figure,imshow(input_image);
title('Original RGB image')

% Convert RGB image to gray image
gray_image=rgb2gray(input_image);

% display function
figure,imshow(gray_image);
title('Gray image')

% resize the image
image_resize=imresize(gray_image,[256,256]);

% display function
figure,imshow(image_resize);
title('Resized image')

% Noise added using a user-defined function

% generating random values with respect to image size
q = rand(size(image_resize));
% noise percentage [0.1 to 0.9]
noise_per=0.2;
for i=1:256
    for j=1:256
            if (q(i,j) < noise_per)
            valueset = [0, 255];
            temp=valueset(randi(numel(valueset)));
            % if generate random noise is 255 means, add white noise
            if(temp==255)
                noise_added_image(i,j)=255;
            else
            % if generate random noise is 0 means, add black noise
                noise_added_image(i,j)=0;
            end
        else
            noise_added_image(i,j)=image_resize(i,j);            
        end
    end
end

% display function
figure,imshow(noise_added_image);
title('Salt and pepper noise added image')

% Noise removal using median filter
noise_removed_image=medfilt2(noise_added_image,[3,3]);

% display function
figure,imshow(noise_removed_image);
title('Salt and pepper noise removed image')
% Median filter Performance
% Mean squared error
mse_value=mse(image_resize,noise_removed_image)
% Structural Similarity Index
ssim_value=ssim(image_resize,noise_removed_image)
% Peak Signal-To-Noise Ratio
psnr_vale=psnr(image_resize,noise_removed_image)

MATLAB Code: Salt and pepper inbuilt and median filtering user-defined function

% clearing command window
clc
% cleaning all variable
clear 
% closing all figure
close all

% read inbuilt image from matlab
input_image=imread('football.jpg');

% display function
figure,imshow(input_image);
title('Original RGB image')

% Convert RGB image to gray image
gray_image=rgb2gray(input_image);

% display function
figure,imshow(gray_image);
title('Gray image')

% resize the image
image_resize=imresize(gray_image,[256,256]);

% display function
figure,imshow(image_resize);
title('Resized image')

% adding salt and pepper noise using 'imnoise' inbuilt command
noise_added_image=imnoise(image_resize,'salt & pepper',0.2);

% display function
figure,imshow(noise_added_image,[]);
title('Salt and pepper noise added image')

% Noise removal using a user-defined function

% window size
window_size=3;
noise_removed_image=zeros(256,256);
% median filering
for ii=window_size-1:size(noise_added_image,1)-1
    for jj=window_size-1:size(noise_added_image,2)-1
        
        % taking 3 x 3 window 
        temp1=noise_added_image(ii-1:ii+1,jj-1:jj+1);
        
        % performing median filtering
        noise_removed_image(ii,jj)=median(median(temp1));
    end
end

% display function
figure,imshow(noise_removed_image,[]);
title('Salt and pepper noise removed image')
% Median filter Performance
% Mean squared error
mse_value=mse(image_resize,uint8(noise_removed_image))
% Structural Similarity Index
ssim_value=ssim(image_resize,uint8(noise_removed_image))
% Peak Signal-To-Noise Ratio
psnr_vale=psnr(image_resize,uint8(noise_removed_image))

MATLAB Code: Both  Salt and pepper and median filtering user-defined function

% clearing command window
clc
% cleaning all variable
clear 
% closing all figure
close all

% read inbuilt image from matlab
input_image=imread('football.jpg');

% display function
figure,imshow(input_image);
title('Original RGB image')

% Convert RGB image to gray image
gray_image=rgb2gray(input_image);

% display function
figure,imshow(gray_image);
title('Gray image')

% resize the image
image_resize=imresize(gray_image,[256,256]);

% display function
figure,imshow(image_resize);
title('Resized image')

% Noise added using a user-defined function

% generating random values with respect to image size
q = rand(size(image_resize));
% noise percentage [0.1 to 0.9]
noise_per=0.2;
for i=1:256
    for j=1:256
        if (q(i,j) < noise_per)
            valueset = [0, 255];
            temp=valueset(randi(numel(valueset)));
            % if generate random noise is 255 means, add white noise
            if(temp==255)
                noise_added_image(i,j)=255;
            else
                % if generate random noise is 0 means, add black noise
                noise_added_image(i,j)=0;
            end
        else
            noise_added_image(i,j)=image_resize(i,j);
        end
    end
end

% display function
figure,imshow(noise_added_image,[]);
title('Salt and pepper noise added image')

% Noise removal using a user-defined function

% window size
window_size=3;
noise_removed_image=zeros(256,256);
% median filering
for ii=window_size-1:size(noise_added_image,1)-1
    for jj=window_size-1:size(noise_added_image,2)-1
        
        % taking 3 x 3 window
        temp1=noise_added_image(ii-1:ii+1,jj-1:jj+1);
        
        % performing median filtering
        noise_removed_image(ii,jj)=median(median(temp1));
    end
end

% display function
figure,imshow(noise_removed_image,[]);
title('Salt and pepper noise removed image')
% Median filter Performance
% Mean squared error
mse_value=mse(image_resize,uint8(noise_removed_image))
% Structural Similarity Index
ssim_value=ssim(image_resize,uint8(noise_removed_image))
% Peak Signal-To-Noise Ratio
psnr_vale=psnr(image_resize,uint8(noise_removed_image))

Performance Results:





 

MSE

SSIM

PSNR

Both noise and filter are inbuild function

 

17.9642

 

0.7254

 

27.9956

 

noise user-defined; filter inbuilt

 

18.1121

 

0.7205

 

27.7493

 

filter user-defined; noise inbuilt

 

21.1973

 

0.6574

 

24.696

 

both are user-defined function

 

21.6199

 

0.653

 

24.7408

 



Conclusion: 

From Figure 2, we can see that there is some appearance of noise after filtering. I will do some improvements in my next post. kindly see that.



Please provide feedback in the Command section



Comments

Post a Comment

Popular posts from this blog

MATLAB Tutorials class 4 : MATLAB command diag, det, trace

MATLAB Tutorials class 5: MATLAB inbuild command repmat,repelem,cat,horzcat, and vertcat

Improved Median filter MATLAB code and explanation ( without inbuilt function and with inbuilt function)