Matlab分别用均值滤波和中值滤波处理高斯噪声和椒盐噪声

I=imread('D:\TuPian\lion.png');
subplot(3,3,1);imshow(I);title('原始图像');
gray=rgb2gray(I);
subplot(3,3,3);imshow(gray);title('灰度图像');

G=imnoise(gray,'gaussian',0,0.02);    %添加高斯噪声,均值为0,方差为0.02
subplot(3,3,4);imshow(G);title('高斯噪声图像');

j1 = fspecial('average',[3,3]);  %均值滤波,3*3的模板
filters1 = imfilter(G,j1);
subplot(3,3,5);imshow(filters1);title('均值滤波');


K1=medfilt2(G,[3,3]);   %中值滤波,3*3的模板
subplot(3,3,6);imshow(K1);title('中值滤波');

J=imnoise(gray,'salt & pepper',0.2);  %添加椒盐噪声,密度为0.2
subplot(3,3,7);imshow(J);title('椒盐噪声图像');


j2 = fspecial('average',[3,3]);
filters2 = imfilter(J,j2);
subplot(3,3,8);imshow(filters2);title('均值滤波');


K2=medfilt2(J,[3,3]);
subplot(3,3,9);imshow(K2);title('中值滤波');

fspecial()为指定滤波方法的函数,如fspecial(‘average’,[3,3])是模板为33的均值滤波。
medfilt2()是中值滤波函数,如K2=medfilt2(J,[3,3])是模板为3
3的中值滤波,J是要处理的图像,K2是处理后的图像。

你可能感兴趣的:(matlab)