M=imread('C:\Users\Seven\Desktop\lena.jpg'); %打开图像“lena.jpg”
subplot(2,3,1);
imshow(M)%在一行一列显示原始图像
title('原始图像');
P1=imnoise(M,'gaussian',0.02);%加入高斯噪声
subplot(2,3,2);
imshow(P1)%在一行二列显示加入高斯噪声后的图像
title('加入高斯噪声');
P2=imnoise(M,'salt & pepper',0.02);%加入椒盐噪声
subplot(2,3,3);
imshow(P2)%在一行三列显示加入椒盐噪声后的图像
title('加入椒盐噪声');
P12=im2bw(P1,0.5);
P22=im2bw(P2,0.5);%将添加噪音后的图像转为二进制
P01=im2bw(M,0.5);
subplot(2,3,4);
imshow(P01)
title('原图像转为二进制');%将原图像转为二进制并于二行一列显示
P3=medfilt2(P12);%对高斯噪声中值滤波
subplot(2,3,5);
imshow(P3)%在二行二列显示高斯噪声中值滤波后的图像
title('对高斯噪声中值滤波');
P4=medfilt2(P22);%对椒盐噪声中值滤波
subplot(2,3,6);
imshow(P4)
title('对椒盐噪声中值滤波');%在二行三列显示对椒盐噪声中值滤波