所加的噪声可以是随机噪声、高斯噪声、椒盐噪声,本次实验只分析随机噪声
% 读取图像
Image = imread('lena.jpg');
Image=rgb2gray(Image);
% 使用imresize函数进行填充
[m,n]=size(Image);
result=zeros(m+2,n+2);
for i=2:m+1
for j=2:n+1
result(i,j)=Image(i-1,j-1);
end
end
result=mat2gray(result);
%利用双边滤波器对一幅图像实现填充后,并完成双边滤波,并对均值、高斯、
%双边滤波器的实验结果进行综合分析
result=im2double(result);
result=result+0.05*randn(size(result));
w=15;sigma_s=6;sigma_r=0.1;
[X,Y]=meshgrid(-w:w,-w:w);
Gs=exp(-(X.^2+Y.^2)/(2*sigma_s^2));
[hm,wn]=size(result);
result1=zeros(hm,wn);
for i=1:hm
for j=1:wn
temp=result(max(i-w,1):min(i+w,hm),max(j-w,1):min(j+w,wn));
Gr=exp(-(temp-result(i,j)).^2/(2*sigma_r^2));
W=Gr.*Gs((max(i-w,1):min(i+w,hm))-i+w+1,(max(j-w,1):min(j+w,wn))-j+w+1);
result1(i,j)=sum(W(:).*temp(:))/sum(W(:));
end
end
disp(fspecial('average',3))
disp(1.0/9.*[1,1,1;1,1,1;1,1,1]);
avg_result1=imfilter(result,1.0/9.*[1,1,1;1,1,1;1,1,1],'conv');
gaussian_result1=imfilter(result,fspecial('gaussian',[3,3],0.6),'conv');
figure;
subplot(2,2,1);imshow(result);title('随机噪声图像');
subplot(2,2,2);imshow(result1);title('双边滤波图像');
subplot(2,2,3);imshow(avg_result1);title('均值滤波图像');
subplot(2,2,4);imshow(gaussian_result1);title('高斯滤波图像');
先对原图进行padding,并且padding=1,然后对padding之后的图片进行双边滤波、均值滤波和高斯滤波,运行结果为:
从运行结果可以看到,相比于均值滤波和高斯滤波,双边滤波的结果应该是比较好的,均值滤波和高斯滤波的结果则差不多,但是均值滤波的结果会有模糊的问题。