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

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

            We looked at the salt and pepper noise and the median filter Matlab program (with inbuilt and without inbuilt function) in my previous post. In that post, I gave the 'medfilt2' program without using the inbuilt function. While viewing the output of the medfilt2 program function, there is some noise pixel on the filtered image. And the overall image looks blurred, too, because it also modifies the noiseless pixel intensity values. This problem can be corrected to some extent by making some adjustments to the median filter. We're going to see it in this post. 

Modification:

1. Instead of changing all pixel values, only the noise-affected pixel is being changed here. The noise detection stage is used to detect the noise pixel.
2. While computing median, we consider only noiseless pixels instead of taking all pixel values. i.e. we neglecting noise pixel 0 and 255

Improved Median filter 

Steps : 

  1. Noise detection stage
  2. Noise correction stage i.e filtering stage

                        Figure 1. (a) Noisy image pixel, (b) stage 1 outcome, and (c) stage 2 outcome


1. Noise detection stage 

       The image affected by salt and pepper noise has a pixel value of 0 and 255, respectively. So if the pixel value in the image is 0 or 255, we can assume that the pixel is affected by the noise.

Steps:

for i to row size
    for j to column size
            if  noise affected image (i,j) is equal to 0 or 255 means
                    noisedetectedimage(i,j)=1
            else
                     noisedetectedimage(i,j)=0
            end
    end
end

In step 1. respectively, we check all the pixel intensities. if pixel intensity has a value of 0 or 255 is considered a noise pixel. The noise pixel is considered to be 1 and 0 is considered to be the noiseless pixel, based on which we form a matrix.

2.  Noise correction or filtering

Steps: 

for i to row size
    for j to column size
            if  noisedetectedimage(i,j) is equal to 1 means
                    take that pixel and surrounded region 
                    remove 0 and 255
                    finalimage(i,j)=take the median for the rest of the array
            else
                    finalimage(i,j)=noise affected image(i,j)
            end
    end
end

So in step 2, based on the noise detection matrix we performing the filtering. we checking the all pixels one by one. Take the area around the noise pixel, where 0 and 255 are neglected. Then take the median of the sequence and assign it to the final intensity value. Sometimes the area around the pixel may be full 0 and 255. In that kind of situation we can assign 0 or 255. Model calculation given below.

For second-row fourth column (figure 1 a):

1.      1.Taking surrounded pixel

 [ 71, 69, 74, 70, 255, 66, 66, 66, 0]

 2.      Remove 0 and 255

 [71, 69, 74, 70, 66, 66, 66]

 3.      Median filtering

 [66, 66, 66, 69, 70, 71, 74] 

Click here to download the full code

MATLAB Code: Improved median filter 

% 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

% step 1 noise detection stage
for ii=1:size(noise_added_image,1)
    for jj=1:size(noise_added_image,2)
        if(noise_added_image(ii,jj)==0 || noise_added_image(ii,jj)==255)
            noise_detected_image(ii,jj)=1;
        else
            noise_detected_image(ii,jj)=0;
        end
    end
end

% step 2 noise correction stage

% 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
        if (noise_detected_image(ii,jj)==1)
            % taking 3 x 3 window
            temp1=noise_added_image(ii-1:ii+1,jj-1:jj+1);
            temp1(temp1==0)=[];
            temp1(temp1==255)=[];
            if isempty(temp1)
                temp1=0;
            end
            % performing median filtering
            noise_removed_image(ii,jj)=median(median(temp1));
        else
            noise_removed_image(ii,jj)=noise_added_image(ii,jj);
        end
    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


Figure 2. (a) Resized gray input image, (b) 20 % salt and pepper noise added image, (c) Both noise and filter are inbuild, (d) noise user-defined; filter inbuilt, (e) improved filter user-defined; noise inbuilt and (f) both are user-defined function improved filtered outcome

From figure 2 we can say improved median filter (e and f) provide better performance when compared to inbuild command medfilt2 (c,d). Figure 2. e and  f  atmost looks like input image (a).

 

MSE

SSIM

PSNR

Both noise and filter are inbuild function

 

17.9642

 

0.7254

 

27.9956

 

noise user-defined; filter inbuilt

 

7.8526

 

0.8982

 

29.0505

 

filter user-defined; noise inbuilt

 

21.1973

 

0.6574

 

24.696

 

both are a user-defined function

 

7.9840

 

0.8988

 

29.0542

 



Conclusion:

        The improved median filter provides better performance result compared to inbuilt command and previous post result. 

Note: This is a small type of change in the median filtering. Most research scholars have made some changes to the median filtering in order to achieve better performance. Let's look at each of them in my next post.

Thank you so much for being here 🙏 ..... Please provide your feedback 😊




Comments

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