题目:均值滤波和中值滤波
在自己的证件照中加入椒盐噪声、高斯白噪声。
分别用3*3、5*5、7*7的均值滤波器和中值滤波器进行滤波。 处理过程
1. 用imnoise函数在图像中分别加入椒盐噪声和高斯白噪声;
2. 均值滤波:用fspecial函数创建各模板大小的均值滤波器,并用imfilter函数进行
滤波。
中值滤波:直接用matlab提供的medfilt2中值滤波器进行滤波即可。 处理结果
程序清单
(1)均值滤波
rgb=imread('photo.jpg');
J1=imnoise(rgb,'salt & pepper',0.02); J2=imnoise(J1,'gaussian',0,0.01); h1=fspecial('average',[3,3]); h2=fspecial('average',[5,5]); h3=fspecial('average',[7,7]); rgb1=imfilter(J2,h1); rgb2=imfilter(J2,h2); rgb3=imfilter(J2,h3); figure;
subplot(2,3,1);imshow(rgb) title('原图像');
subplot(2,3,2);imshow(J2) title('加入噪声后的图像');
subplot(2,3,4);imshow(rgb1) title('3*3均值滤波图像');
subplot(2,3,5);imshow(rgb2) title('5*5均值滤波图像');
subplot(2,3,6);imshow(rgb3)
title('7*7均值滤波图像'); (2)中值滤波
rgb=imread('photo.jpg');
J1=imnoise(rgb,'salt & pepper',0.02); J2=imnoise(J1,'gaussian',0,0.01); J3=rgb2gray(J2);
rgb1=medfilt2(J3,[3 3]); rgb2=medfilt2(J3,[5 5]); rgb3=medfilt2(J3,[7 7]);
figure;
subplot(2,3,1);imshow(rgb) title('原图像');
subplot(2,3,2);imshow(J3) title('加入噪声后的图像');
subplot(2,3,4);imshow(rgb1) title('3*3中值滤波图像');
subplot(2,3,5);imshow(rgb2) title('5*5中值滤波图像');
subplot(2,3,6);imshow(rgb3) title('7*7中值滤波图像');