clear all;
close all;
figure;
I1=imread('cameraman.jpg');
subplot(221);
imshow(I1);title('原图像');
I2=imnoise(I1,'salt & pepper');
subplot(222);
imshow(I2);title('加胡椒盐噪声');
M=fspecial('average',3*3);
I3=imfilter(I2,M);
subplot(223);
imshow(I3);title('工具箱均值滤波');
[m,n]=size(I1);
J=double(I2);
for i=2:m-2
for j=2:n-2
s = J(i-1,j-1)+J(i-1,j)+J(i-1,j+1)+ J(i,j-1)+J(i,j)+J(i,j+1)+J(i+1,j-1)+J(i+1,j)+J(i+1,j+1);
I4(i,j)=s/9;
end
end
subplot(224);
imshow(uint8(I4));title('自编均值滤波');