set(0,'defaultfigurecolor','w')
a=imread('爱因斯坦.jpg');
figure(1)
subplot(4,4,1)
imshow(a)
title('Original')
count=2;
for L=[5,9,15,21,25]
for sima=[0.5,1,3]
K=zeros(L,L);
for x=-round(L/2)+1:round(L/2)-1
for y=-round(L/2)+1:round(L/2)-1
K(x+round(L/2),y+round(L/2))=1/(2*pi*sima*sima)*exp(-(x*x+y*y)/(2*sima*sima));
end
end
% a = rgb2gray(a);
b=zeros(size(a,1),size(a,2));
for i=round(L/2):size(a,1)-(round(L/2)-1)
for j=round(L/2):size(a,2)-(round(L/2)-1)
b(i,j)=round(Convolution(a(i-round(L/2)+1:i+round(L/2)-1,j-round(L/2)+1:j+round(L/2)-1),K));
end
end
b=uint8(b);
subplot(4,4,count)
imshow(b)
title(['Size=',num2str(L),',σ=',num2str(sima)])
count=count+1;
end
end
saveas(gcf,'高斯滤波','jpg')
Convolution函数
function s=Convolution(A,B)
s=0;
m=size(A,1);
for i=-floor(m/2):floor(m/2)
for j=-floor(m/2):floor(m/2)
s=s+A(floor(m/2)+1-i,floor(m/2)+1-j)*B(i+floor(m/2)+1,j+floor(m/2)+1);
end
end
end