欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

均值滤波

程序员文章站 2022-05-21 13:48:41
...

噪声原理

1.高斯噪声是指它的概率密度函数服从高斯分布(即正态分布)的一类噪声。
2.椒盐噪声,椒盐噪声又称脉冲噪声,它随机改变一些像素值,是由图像传感器,传输信道,解码处理等产生的黑白相间的亮暗点噪声。椒盐噪声往往由图像切割引起。

滤波原理

均值滤波是一种线性滤波器,将一个窗口区域中的像素计算平均值,然后将窗口中计算得到的均值设置为锚点上的像素值。均值滤波用卷积实现,得到的像素值需除以窗口尺寸大小。此次实验中的卷积模板如下:
均值滤波

滤波结果

原图:
均值滤波
滤波结果:
每个滤波结果对应于该图正上方所加噪声
均值滤波
均值滤波

代码

%Write  by 长安 Rjex
clc ,clear all;
I = imread('myImage.jpg');
A = rgb2gray(I);

J = imnoise(A,'gaussian',0,0.02); %加高斯噪声
J1 = imnoise(A,'gaussian',0,0.04); 
J2 = imnoise(A,'gaussian',0,0.06); 
J3 = imnoise(A,'salt & pepper',0.2); %加椒盐噪声
J4 = imnoise(A,'salt & pepper',0.4); 
J5 = imnoise(A,'salt & pepper',0.6); 

C = double(J);  
C1 = double(J1);  
C2 = double(J2);  
C3 = double(J3);  
C4 = double(J4);  
C5 = double(J5);   

N = 3;     %确定均值滤波模板
flag = (N-1)/2;
for i = flag+1:size(J,1)-flag
    for j = flag+1:size(J,2)-flag      
          C(i,j)=sum(sum(J(i-flag:i+flag,j-flag:j+flag)/(N*N)));    
    end
end
C = uint8(C); 

N1 = 3;     %确定均值滤波模板
flag = (N1-1)/2;
for i = flag+1:size(J1,1)-flag 
    for j = flag+1:size(J1,2)-flag       
         C1(i,j)=sum(sum(J1(i-flag:i+flag,j-flag:j+flag)/(N1*N1)));    
    end
end
C1 = uint8(C1); 

N2 = 3;     %确定均值滤波模板
flag = (N2-1)/2;
for i = flag+1:size(J2,1)-flag
    for j = flag+1:size(J2,2)-flag      
          C2(i,j)=sum(sum(J2(i-flag:i+flag,j-flag:j+flag)/(N2*N2)));    
    end
end
C2 = uint8(C2); 

N3 = 3;     %确定均值滤波模板
flag = (N3-1)/2;
for i = flag+1:size(J3,1)-flag 
   for j = flag+1:size(J3,2)-flag      
        C3(i,j)=sum(sum(J3(i-flag:i+flag,j-flag:j+flag)/(N3*N3)));    
   end
end
C3 = uint8(C3); 

N4 = 3;     %确定均值滤波模板
flag = (N4-1)/2;
for i = flag+1:size(J4,1)-flag
    for j = flag+1:size(J4,2)-flag       
         C4(i,j)=sum(sum(J4(i-flag:i+flag,j-flag:j+flag)/(N4*N4)));    
    end
end
C4 = uint8(C4); 

N5 = 3;    %确定均值滤波模板
flag = (N5-1)/2;
for i = flag+1:size(J5,1)-flag
    for j = flag+1:size(J5,2)-flag       
         C5(i,j)=sum(sum(J5(i-flag:i+flag,j-flag:j+flag)/(N5*N5)));
    end
end
C5 = uint8(C5); 

figure(1);
imshow(A),title('原图像'); 

figure(2);
subplot(2,3,1),imshow(J),title('a.加高斯噪声(0,0.02)');
subplot(2,3,4),imshow(C),title('b.均值滤波3*3');
subplot(2,3,2),imshow(J1),title('c.加高斯噪声(0,0.04)');
subplot(2,3,5),imshow(C1),title('d.均值滤波3*3');
subplot(2,3,3),imshow(J2),title('e.加高斯噪声(0,0.06)');
subplot(2,3,6),imshow(C2),title('f.均值滤波3*3'); figure(3);
subplot(2,3,1),imshow(J3),title('a.加椒盐噪声(0.02)');
subplot(2,3,4),imshow(C3),title('b.均值滤波3*3');
subplot(2,3,2),imshow(J4),title('c.加椒盐噪声(0.04)');
subplot(2,3,5),imshow(C4),title('d.均值滤波3*3');
subplot(2,3,3),imshow(J5),title('e.加椒盐噪声(0.06)');
subplot(2,3,6),imshow(C5),title('f.均值滤波3*3');
相关标签: 数字图像处理