clc;clear;close all;
Image = im2double(imread("hudie.png"));
NoiseI = Image +0.05* randn(size(Image));
w =15;
sigma_s =6;
sigma_r =0.1;[X,Y]= meshgrid(-w:w,-w:w);
Gs = exp(-(X.^2+Y.^2)/(2*sigma_r^2));[hm,wn]= size(NoiseI);
result = zeros(hm,wn);for i =1:hm
for j =1:wn
temp = NoiseI(max(i-w,1):min(i+w,hm),max(j-w,1):min(j+w,wn));
Gr = exp(-(temp-NoiseI(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);
result(i,j)=sum(W(:).* temp(:))/sum(W(:));
end
end
imshow(result),title('双边滤波结果');