%Sigma 西格玛平滑滤波 clc;clear *; F=rgb2gray(imread('imggirl.jpg')); figure,imshow(F); title('originImage'); F=imnoise(F,'salt & pepper',0.01); %Add salt noise figure,imshow(F); title('saltNoise'); F=double(F); [row,col]=size(F); for i=3:row-2 %use templet 5X5 for j=3:col-2 Templet=[F(i-2,j-2) F(i-2,j-1) F(i-2,j) F(i-2,j+1) F(i-2,j+2) F(i-1,j-2) F(i-1,j-1) F(i-1,j) F(i-1,j+1) F(i-1,j+2) F(i,j-2) F(i,j-1) F(i,j) F(i,j+1) F(i,j+2) F(i+1,j-2) F(i+1,j-1) F(i+1,j) F(i+1,j+1) F(i+1,j+2) F(i+2,j-2) F(i+2,j-1) F(i+2,j) F(i+2,j+1) F(i+2,j+2)]; Trows = reshape(Templet,5*5,1); %将模板转换成1列 theta=std2(Trows); delta=2*theta; count_1=0;count_0=0; for m=1:5 for n=1:5 A=Templet(m,n)-delta; B=Templet(m,n)+delta; if A<=Templet(3,3)<=B Sigma(m,n)=1; count_1=count_1+1; else Sigma(m,n)=0; count_0=count_0+1; end end end K=3;N=2; Sum=0;Num=0; if count_1>=K for m=1:5 for n=1:5 Sum=Sum+Sigma(m,n)*Templet(m,n); Num=Num+Sigma(m,n); end end F(i,j)=Sum/Num; else F(i,j)=mean(Trows); end end end figure,imshow(uint8(F)); title('Sigma');