www.pudn.com > Lee.rar > Leefilter.m, change:2013-04-02,size:1583b

```function [RI] = Leefilter(I)
% Lee滤波
% I 输入灰度图像
% RI 输出Lee滤波后的灰度图像
% 输入窗口大小，并计算滤波前图像的等效视数
[height,width] = size(I); % height, width:图像的高和宽
N = 3; % N*N的窗口
nr = floor(N/2); % N = 2*nr+1; radius of the window
%使用局域窗口的方差系数
I = double(I);
I2 = double(I);
sum = 0;
num = 0;
sum2=0;
for i = 1:height
for j = 1:width
sum=sum+I(i,j);
num=num+1;
sum2=sum2+I(i,j)*I(i,j);
end
end
u=sum/num;
v=sum2/num-u*u;
L=u*u/v;%滤波前图像的等效视数
delta = 1/sqrt(L); % 噪声标准差
% filtering
for i = 1:height
for j = 1:width

% calculate the mean and variace in the N*N window
sum = 0;
sum2 = 0;
num = 0;
for k = -nr:nr
for m = -nr:nr
if( (i+k >=1) && (i+k <= height) && (j+m >= 1) && (j+m <= width) )  % 避免数组越界
num = num + 1;
sum = sum + I(i+k,j+m);
sum2 = sum2 + I(i+k,j+m)*I(i+k,j+m);
end
end
end

u = sum/num; % mean
v = sum2/num - u*u; % variance

% calculate the value of the centre pixel
if(v == 0)
k = 0;
else
k =(1 - u*u*delta*delta/v);%Lee滤波器权重计算，观测值方差系数和相干斑方差系数
end

if(k<0)
k = 0;
end
I2(i,j) = u + k*( I(i,j) - u );
end
end

RI = I2;
%
% % show the original image and filtered image
% imshow(RI);```