对原图分别加上salt和pepper噪声:
f=imread('lena.jpg');
f=rgb2gray(f);
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,
subplot(121);
imshow(gp);
title('salt noise');
R = imnoise2('salt & pepper',M,N,0,0.1);
c = find(R == 1);
gs = f;
gs(c) = 255;
subplot(122);
imshow(gs);
title('pepper noise');
先生成pepper噪声,再加上salt噪声
R = imnoise2('salt & pepper',M,N,0,0.1);
c = find(R == 1);
gs = gp;
gs(c) = 255;
subplot(122);
imshow(gs);
title('<span style="font-family: Arial, Helvetica, sans-serif;">salt & pepper </span>noise');
过滤椒盐噪声:
fp = spfilt(gp,'chmean',3,3,1.5);
fs = spfilt(gs,'chmean',3,3,-1.5);
fpmax = spfilt(gp,'max',3,3);
fsmin = spfilt(gs,'min',3,3);
subplot(221);imshow(fp);title('Q+ for remove pepper');
subplot(222);imshow(fs);title('Q- for remove salt');
subplot(223);imshow(fpmax);title('max for remove pepper');
subplot(224);imshow(fsmin);title('min for remove salt');
自适应的空间滤波器
%% 自适应空间滤波器
f=imread('lena.jpg');
f=rgb2gray(f);
figure,imshow(f);
g = imnoise(f,'salt & pepper',0.25);
f1 = medfilt2(g,[7 7],'symmetric');
f2 = adpmedian(g,7);
subplot(221),imshow(f),title('lena');
subplot(222),imshow(g),title('noise');
subplot(223),imshow(f1),title('medfilt');
subplot(224),imshow(f2),title('adpmedian');
可以看到,自适应不像中值滤波那样模糊失真。