
close all;
P=input('input picture data:');
d=size(P);
if(d(3)>1)
P=rgb2gray(P);
end
choice=1;
while(choice~=0)
P=imnoise(P,'salt & pepper',0.02);
subplot(221);imshow(P);
title('加入椒盐噪声后的图像');
P=double(P);
[m n]=size(P);
g=zeros(m,n);
m1=m+2;
n1=n+2;
PP=zeros(m1,n1);
for i=2:m1-1
for j=2:n1-1
PP(i,j)=P(i-1,j-1);
end
end
PP(1,2:n1-1)=P(2,1:n);
PP(m1,2:n1-1)=P(m-1,1:n);
PP(1:m1,1)=PP(1:m1,3);
PP(1:m1,n1)=PP(1:m1,n1-2);
disp('-----------------------------------------------');
disp(' 主菜单 ');
disp('The Number of Ngeighbourhood-Averageing Method ');
disp(' 4、 4邻域平均 ');
disp(' 8、 8邻域平均,不考虑点本身亮度 ');
disp(' 9、 8邻域平均,考虑点本身亮度 ');
disp('----------------------------------------------');
choice=input('Please input the number of method:(4,8,9),0表示结束\\n')
switch (choice)
case 4
for i=2:m1-1
for j=2:n1-1
g(i,j)=(PP(i-1,j)+PP(i+1,j)+PP(i,j-1)+PP(i,j+1))/4;
end
end
g=uint8(g);
subplot(222);imshow(g);
title('经4邻域平均去噪后的图片');
case 8
for i=2:m1-1
for j=2:n1-1
g(i,j)=(PP(i-1,j-1)+PP(i-1,j)+PP(i-1,j+1)+PP(i,j-1)+PP(i,j+1)+PP(i+1,j-1)+PP(i+1,j)+PP(i+1,j+1))/8;
end
end
g=uint8(g);
subplot(223);imshow(g);
title('经8邻域平均,不考虑点本身亮度去噪后的图片');
case 9
for i=2:m1-1
for j=2:n1-1
g(i,j)=(PP(i-1,j-1)+PP(i-1,j)+PP(i-1,j+1)+PP(i,j-1)+PP(i,j)+PP(i,j+1)+PP(i+1,j-1)+PP(i+1,j)+PP(i+1,j+1))/9;
end
end
g=uint8(g);
subplot(224);imshow(g);
title('经8邻域平均,考虑点本身亮度去噪后的图片');
otherwise
disp('error')
end
choice=input('是否继续?(1/0):');
P=uint8(P);
end
