%读取图像并展示
I=imread("./work/Lenna.bmp");
subplot(2,2,1);
imshow(I);
title("原图");
%加入椒盐噪声
Hsp=imnoise(I,'salt & pepper');
subplot(2,2,2);
imshow(Hsp);
title("椒盐噪声图像");
%用均值滤波器去除图像中的噪声
Kaverage = imfilter(Hsp,fspecial('average',3));
subplot(2,2,3);
imshow(Kaverage);
title("均值滤波去噪后图像");
%用中值滤波器去除图像中的噪声
Kmedian=medfilt3(Hsp);
subplot(2,2,4);
imshow(Kmedian);
title("中值滤波去噪后图像");
实验结果图如下: