以灰度图像eight.tif为例,向原始图像中加入高斯噪声,再对噪声图像调用均值滤波函数avefilt进行去噪。
I=imread('eight.tif');
G=imnoise(I,'gaussian');
after=avefilt(G,3);
subplot(1,3,1);
imshow(I);
subplot(1,3,2);
imshow(G);
subplot(1,3,3);
imshow(after);
function d=avefilt(x,n)
a=ones(n);
[M,N]=size(x);
x1=double(x);
x2=x1;
for i=1:M-n+1
for j=N-n+1
c=x1(i:i+n-1,j:j+n-1).*a;
s=sum(sum(c));
x2(i+fix((n-1)/2),j+fix((n-1)/2))=s/(n*n);
end
end
d=uint8(x2);