|  | 
| 复制代码I=imread('luo.jpg'); 
figure;imshow(I);title('原图像'); 
I1=imnoise(I,'salt & pepper',0.02); 
figure;imshow(abs(I1));title('加入椒盐噪声后的图像'); 
I2=imnoise(I,'gaussian',0,0.005); 
figure;imshow(I2);title('加入高斯噪声后的图像'); 
%对噪声污染的图像做均值滤波: 
%---------对椒盐噪声做均值滤波 ---------% 
II1=rgb2gray(I1); 
a=1/9.*[1   1    1 
         1   1    1 
         1   1    1]; 
b=conv2(a,II1); 
figure;imshow(b,[0 255]);title('对椒盐噪声的均值滤波图像'); 
%---------对高斯噪声做均值滤波---------% 
II2=rgb2gray(I2); 
a=1/9.*[1   1    1 
         1   1    1 
         1   1    1]; 
b=conv2(a,II2); 
figure;imshow(b,[0 255]);title('对高斯噪声的均值滤波图像');  
h=fspecial('average',3)/255;
I3=filter2(h,II1);
figure;imshow(I3);
title('对高斯噪声的均值滤波后的图像');
%对噪声污染的图像做中值滤波: 
%---------对椒盐噪声做中值滤波---------% 
c=medfilt2(II1,[3 3]); 
figure;imshow(c);title('对椒盐噪声的中值滤波图像'); 
%---------对高斯噪声做中值滤波---------% 
c=medfilt2(II2,[3 3]); 
figure;imshow(c);title('对高斯噪声的中值滤波图像'); 
 
B=rgb2gray(I1);  
C=B; 
xsize=size(B); 
for k=2:(xsize(1)-1) 
    for j=2:(xsize(2)-1) 
        t=B(k-1:k+1,j-1:j+1); 
        C(k,j)=median(t(1:9)); 
    end; 
end; 
imshow(C); 
title('中值处理后的图'); 
 %采用高斯滤波
a=1/1151.*[1  4  7  10 7  4   1    
         4  12 26 33 26 12  4
         7  26 55 71 55 26  7
         10 33 71 91 71 33 10
         7  26 55 71 55 26  7
         4  12 26 33 26 12  4
         1  4  7  10 7  4   1 ]; 
b=conv2(a,II2); 
figure;imshow(b,[0 255]);title('对椒盐噪声的高斯滤波图像'); 
 h=fspecial('gaussian',[3 3],0.5);
 I4=imfilter(I3,h);
 figure;imshow(I4);
 title('高斯滤波后的图像');
 
 | 
 |