You are on page 1of 5

clc; clear all; close all;

img= imread('l.tif');

[M,N]=size(img);
pic= zeros(M,N);
n = M*N;

a = 1;b = 256;
for z=1:255:256
% z
Pa= .1;
nza = round(Pa*n);
Pb= .9;
nzb = round(Pb*n);
for i=1:n
temp = fix(M*rand(1));
if (temp==0)
(temp==0)
temp=temp+1;
x = temp;
else
x = temp;
end

temp = fix(N*rand(1));
if (temp==0)
temp=temp+1;
y = temp;
else
y = temp;
end

if (pic(x,y)==0)
pic(x,y) = pic(x,y) + z;
else

i=ii=i-1;
end
end
end
subplot(211)
imshow(img)
title('Original Image')
figure
subplot(221)
imshow(uint8(pic));
title('Salt & Pepper Noise');

imgnew= double(img).*(pic);
double(img).*(pic);
subplot(222)
imshow(uint8(imgnew))
title('Noisy Image')

out_med= med_filt(imgnew);% 7x7 median filter


subplot(223)
imshow(uint8(out_med))
title('Filtered Image Median Filter')

out_am= arith_mean(imgnew,3);% 3x3 Arithmatic Mean filter


subplot(224)
imshow(uint8(out_am))
imshow(uint8(out_am))
title('Filtered Image Arithmatic Mean Filter')

function S = med_filt(R)
I = double(R);
M= ones(3);
[m1,m2]= size(M);
[x y]= size(I);
a= [zeros(x,m1)];
b= [zeros(m2,y+(2*m1))];
J= [b; a,I,a;b];
S= [zeros(x,y)];
Mask= [ones(7)];
for i= 1:x
for j= 1:y
for k=0:7-1
for l=0:7-1
if(i+k<x && j+l<y)
Z(k+1,l+1)= J(i+k,j+l);
end
end
end
K= Mask.*Z;
s=
[K(1,(1:7)),K(2,(1:7)),K(3,(1:7)),K(4,(1:7)),K(5,(1:7)),K(6,(1:7)),K(7,(1:7))
];
X=sort(s);
med= ceil(length(X)/2);
S(i,j)= X(1,med);
end
end

function S= arith_mean(R,n)
I = double(R);
M= ones(1);
[m1,m2]= size(M);
[x y]= size(I);
% Size to find dimension
a= [zeros(x,m1)];
% zeros col
b= [zeros(m2,y+(2*m1))];
% zeros row
J= [b; a,I,a;b];
% appended image
S= [zeros(x,y)];
Mask= (1/(n*n))*[ones(n)]; % average
for i= 1:x
for j= 1:y
for k=0:n-1
for l=0:n-1
if(i+k<x && j+l<y)
Z(k+1,l+1)= J(i+k,j+l);
end
end
end
K= Mask.*Z;
sm=sum(K);
s=sum(sm);
S(i,j)= s;
end
end

You might also like