clc, clear, close all;

tic

a=imread(‘cameraman.tif’); imshow(a);

nf=.5;

ws=3;

ll=floor(ws/2);

b=imnoise(a,’salt & pepper’,nf); % Generation of noisey input to algorithm

figure,imshow(b);

[m1 n1]=size(a);

b1=zeros(m1+ws-1);

b1=uint8(b1);

[m n]=size(b1);

b1(ll+1:m-ll,ll+1:n-ll)=b;

for i = 2:m-1

for j = 2:n-1

if(b1(i,j)<=0||b1(i,j)>=255)

y=b1(i-ll:i+ll,j-ll:j+ll);

p = sort(y(:,1:1));

q = sort(y(:,2:2));

r = sort(y(:,3:3));

b = [p,q,r];

S = sort(b(:));

flg = 1;

if S(1) < b1(i,j) && b1(i,j) < S(5) && 0 < S(1) && S(9) < 255

b1(i,j) = y(i,j);

else

b1(i,j) = S(5);

end

end

end

end

b2=b1(ll+1:m-ll,ll+1:n-ll);

figure,imshow(b2)

[psnr,mse]=psnr_mse_maxerr(a,b2);

display(psnr)

display(mse)

toc