%读入图像(无噪声)并进行显示
f = imread('noise2.tif');
figure,imshow(f);
%用胡椒噪声污染
[M,N] = size(f);
R = imnoise2('salt & pepper',M,N,0.1,0);
c = find(R == 0);
gp = f;
gp(c) = 0;
figure,imshow(gp);
%使用盐粒噪声污染
R = imnoise2('salt & pepper',M,N,0,0.1);
c = find(R == 1);
gs = f;
gs(c) = 255;
figure,imshow(gs);
%过滤胡椒噪声,最有效的方法就算用Q为正值的反调和滤波器
fp = spfilt(gp,'chmean',3,3,1.5);
figure,imshow(fp);
%过滤胡椒噪声,若才有负值来调整的后果(对比效果)
fa = spfilt(gp,'chmean',3,3,-0.6);
figure,imshow(fa);
%过滤盐粒噪声,最有效的方法就算用Q为负值的反调和滤波器
fs = spfilt(gs,'chmean',3,3,-1.6);
figure,imshow(fs);
%过滤盐粒噪声,若采用正值来调整的后果(对比效果)
fb = spfilt(gs,'chmean',3,3,0.6);
figure,imshow(fb)
%采用最大值滤波器过滤胡椒噪声
fpmax = spfilt(gp,'max',3,3);
figure,imshow(fpmax);
%采用最大值滤波器过滤盐粒噪声的错误(对比效果)
fsmax = spfilt(gs,'max',3,3);
figure,imshow(fsmax);
%采用最小值滤波器过滤盐粒噪声
fsmin = spfilt(gs,'min',3,3);
figure,imshow(fsmin);
%采用最小值滤波器过滤胡椒噪声错误后果(对比效果)
fpmin = spfilt(gp,'min',3,3);
figure,imshow(fpmin);