# 自适应图像降噪滤波器的设计与实现

g(x,y)=h(x,y)f(x,y)+η(x,y)

G(u,v)=H(u,v)F(u,v)+N(u,v)

g(x,y)=f(x,y)+η(x,y)

f̂ (x,y)=g(x,y)σ2gσ̂ 2L[g(x,y)μ̂ L]

• 如果σ2g=0$\sigma^2_g=0$，即整幅图像的噪声方差为零，其实也就相当于噪声为0，而此时公式表示f̂ (x,y)=g(x,y)$\hat f(x,y)=g(x,y)$，恰好符合。
• 如果σ̂ 2L>>σ2g$\hat\sigma^2_L>>\sigma^2_g$，这其实暗示这个局部包含的图像边缘、纹理、细节较多（因为大于整体噪声方差），而这部分内容是应该被保护的。此时公式变为f̂ (x,y)=g(x,y)$\hat f(x,y)=g(x,y)$，也就表示不需要做过多处理，也符合我们details preserving的需求。
• 如果σ̂ 2Lσ2g$\hat\sigma^2_L\approx\sigma^2_g$，则有f̂ (x,y)=μ̂ L$\hat f(x,y)=\hat\mu_L$，这表明(x,y)$(x,y)$处在一个普通的区域，此时便可以应用普通的均值滤波来做处理。

μL=1MN(x,y)Wg(x,y)

σ2L=1MN(x,y)Wg2(x,y)μ2L

f̂ (x,y)=μL+σ2Lv2σ2L[g(x,y)μL]

μL+σ2Lv2σ2L[g(x,y)μL]=μ̂ L+[g(x,y)μ̂ L]σ2gσ̂ 2L[g(x,y)μL]=g(x,y)σ2gσ̂ 2L[g(x,y)μL]

function [f,noise] = mywiener2(g, nhood, noise)

if (nargin<3)
noise = [];
end

% Estimate the local mean of f.
localMean = filter2(ones(nhood), g) / prod(nhood);

% Estimate of the local variance of f.
localVar = filter2(ones(nhood), g.^2) / prod(nhood) - localMean.^2;

% Estimate the noise power if necessary.
if (isempty(noise))
noise = mean2(localVar);
end

% Compute result
% f = localMean + (max(0, localVar - noise) ./ ...
%           max(localVar, noise)) .* (g - localMean);
%
% Computation is split up to minimize use of memory for temp arrays.
f = g - localMean;
g = localVar - noise;
g = max(g, 0);

f = localMean + ((f ./ max(localVar, noise)) .* g);

RGB = imread('saturn.png');
I = rgb2gray(RGB);
I = I(601:1000,1:600);

J = imnoise(I,'gaussian',0,0.005);
J = im2double(J);

K = mywiener2(J,[5 5]);

figure;
imshow(I), title('original image');

figure;
subplot(1,2,1), subimage(J), title('noised image');
subplot(1,2,2), subimage(K), title('denoised image');