clc;
clear;
close all;
%初始化
img=imread('tupian.png');
img1 = imnoise(img,'gaussian',0.05);%加入噪声密度:0.05的高斯噪声
img2 = imnoise(img,'salt & pepper',0.05);%加入噪声密度:0.05的椒盐噪声
img3=rgb2gray(img1);%灰度处理,灰度处理后的图像是二维矩阵
img4=rgb2gray(img2);%灰度处理,灰度处理后的图像是二维矩阵
%降噪处理
M1=medfilt2(img3,[5 5]);%对有高斯噪声图像进行3x3中值滤波
M2=medfilt2(img3,[9 9]);
M3=medfilt2(img3,[16 16]);
M4=medfilt2(img4,[5 5]);%对有椒盐噪声图像进行3x3中值滤波
M5=medfilt2(img4,[9 9]);
M6=medfilt2(img4,[16 16]);
%显示图像
subplot(3,3,1),imshow(img); title('原图');
subplot(3,3,2),imshow(img1); title('加入高斯噪声(密度:0.05)后效果');
subplot(3,3,3),imshow(img2); title('加入椒盐噪声(密度:0.05)后效果');
subplot(3,3,4),imshow(M1); title('对有高斯噪声图像进行5x5中值滤波');
subplot(3,3,5),imshow(M2); title('对有高斯噪声图像进行9x9中值滤波');
subplot(3,3,6),imshow(M3); title('对有高斯噪声图像进行16x16中值滤波');
subplot(3,3,7),imshow(M4); title('对有椒盐噪声图像进行5x5中值滤波');
subplot(3,3,8),imshow(M5); title('对有椒盐噪声图像进行9x9中值滤波');
subplot(3,3,9),imshow(M6); title('对有jiaoyan噪声图像进行16x16中值滤波');