# 积分图像的应用（二）：非局部均值去噪（NL-means）

$S_{t}(x)=\sum_{z1\leq{x1},z1\leq{x2}}s_{t}(z),x=(x1,x2)$

$||V(x)-V(y)||^{2}=\frac{1}{d^{2}}(S_{t}(x_{1}+ds,x_{2}+ds)+S_{t}(x_{1}-ds-1,x_{2}-ds-1)-S_{t}(x_{1}+ds,x_{2}-ds-1)-S_{t}(x_{1}-ds-1,x_{2}+ds))$

close all;
clear all;
clc
I=I+10*randn(size(I));
tic
O1=NLmeans(I,2,5,10);
toc
tic
O2=fastNLmeans(I,2,5,10);
toc
figure;
imshow([I,O1,O2],[]);
function DenoisedImg=fastNLmeans(I,ds,Ds,h)
%I:含噪声图像
%ds:邻域窗口半径
%Ds:搜索窗口半径
%h:高斯函数平滑参数
%DenoisedImg：去噪图像
I=double(I);
[m,n]=size(I);
average=zeros(m,n);
sweight=average;
wmax=average;
h2=h*h;
d2=(2*ds+1)^2;
for t1=-Ds:Ds
for t2=-Ds:Ds
if(t1==0&&t2==0)
continue;
end
w=zeros(m,n);
for i=1:m
for j=1:n
i1=i+ds+1;
j1=j+ds+1;
Dist2=St(i1+ds,j1+ds)+St(i1-ds-1,j1-ds-1)-St(i1+ds,j1-ds-1)-St(i1-ds-1,j1+ds);
Dist2=Dist2/d2;
w(i,j)=exp(-Dist2/h2);
sweight(i,j)=sweight(i,j)+w(i,j);
average(i,j)=average(i,j)+w(i,j)*v(i,j);
end
end
wmax=max(wmax,w);
end
end
average=average+wmax.*I;
sweight=sweight+wmax;
DenoisedImg=average./sweight;

%Ds:搜索窗口半径
%(t1,t2):偏移量
%Sd:积分图像
m1=m-2*Ds;
n1=n-2*Ds;
Sd=zeros(m1,n1);
for i=1:m1
for j=1:n1
if i==1 && j==1
Sd(i,j)=Dist2(i,j);
elseif i==1 && j~=1
Sd(i,j)=Sd(i,j-1)+Dist2(i,j);
elseif i~=1 && j==1
Sd(i,j)=Sd(i-1,j)+Dist2(i,j);
else
Sd(i,j)=Dist2(i,j)+Sd(i-1,j)+Sd(i,j-1)-Sd(i-1,j-1);
end
end
end

function DenoisedImg=fastNLmeans2(I,ds,Ds,h)
I=double(I);
[m,n]=size(I);
average=zeros(m,n);
wmax=average;
sweight=average;
h2=h*h;
d=(2*ds+1)^2;
for t1=-Ds:Ds
for t2=-Ds:Ds
if(t1==0&&t2==0)
continue;
end
SqDist2=Sd(2*ds+2:end-1,2*ds+2:end-1)+Sd(1:end-2*ds-2,1:end-2*ds-2)...
-Sd(2*ds+2:end-1,1:end-2*ds-2)-Sd(1:end-2*ds-2,2*ds+2:end-1);
SqDist2=SqDist2/d;
w=exp(-SqDist2/h2);
average=average+w.*v;
wmax=max(wmax,w);
sweight=sweight+w;
end
end
average=average+wmax.*I;
average=average./(wmax+sweight);
DenoisedImg = average;

Sd = cumsum(Dist2,1);
Sd = cumsum(Sd,2);


[1]FromentJ. Parameter-Free Fast Pixelwise Non-Local Means Denoising[J]. Image ProcessingOn Line, 2014, 4: 300-326

08-26 1472

08-01 2万+

07-03 3万+

10-25 1万+

07-14 9470

#### Non-Local Means 非局部均值去噪滤波

©️2020 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。