%邻域滤波
I=imread('cameraman.tif'); %读取图像
J=imnoise(I,'gaussian',0,0.007);%加入均值为 0,方差为 0.007 的高斯噪声
subplot(2,3,1);imshow(I); title('原始图像');
subplot(2,3,2); imshow(J); title('加入高斯噪声之后的图像');
%采用 MATLAB 中的函数 filter2 对受噪声干扰的图像进行均值滤波
K1=filter2(fspecial('average',3),J)/255; %模板尺寸为 3
K2=filter2(fspecial('average',5),J)/255;% 模板尺寸为 5
K3=filter2(fspecial('average',7),J)/255; %模板尺寸为 7
K4= filter2(fspecial('average',9),J)/255; %模板尺寸为 9
subplot(2,3,3);imshow(K1); title('3*3 滤波效果');
subplot(2,3,4); imshow(K2); title('5*5 滤波效果');
subplot(2,3,5);imshow(K3); title('7*7 滤波效果');
subplot(2,3,6);imshow(K4); title('9*9 滤波效果');
均值滤波在滤除噪声的同时也模糊了图像且尺寸窗口越大输出越不理想
%中值滤波
I=imread('cameraman.tif'); %读取图像
J=imnoise(I,'salt & pepper',0.02);
subplot(2,3,1);imshow(I); title('原始图像');
subplot(2,3,2); imshow(J); title('加入椒盐噪声之后的图像');
%采用 MATLAB 中的函数 medfilt2 对受噪声干扰的图像进行中值滤波