% 此文件为主文件,另外两个为自己编写的均值滤波和中值滤波函数
% 运行此文件即可
clc,clear;
imag = imread('flower.jpg');
%添加椒盐噪声
imag_s2 = imnoise(imag,'salt & pepper',0.05); % g = imnoise(f,'salt & pepper',d),其中d是噪声密度,默认0.05
h1 = fspecial('average');% 滤波模块默认3*3
N1 = filter2(h1,imag_s2);% 均值滤波椒盐噪声
N2 = avg_filt(imag_s2,3);% 自编均值滤波椒盐噪声
N3 = medfilt2(imag_s2);% 中值滤波椒盐噪声
N4 = media_filt(imag_s2,3);% 自编中值滤波椒盐噪声
subplot(3,2,1), imshow(imag), title('原图像');
subplot(3,2,2), imshow(imag_s2), title('加椒盐噪声图像');
subplot(3,2,3), imshow(N1, [ ]), title('Matlab均值滤波噪声后图像');
subplot(3,2,4), imshow(N2, [ ]), title('自编均值滤波噪声后图像');
subplot(3,2,5), imshow(N3, [ ]), title('Matlab中值滤波噪声后图像');
subplot(3,2,6), imshow(N4, [ ]), title('自编中值滤波噪声后图像');