# Non-Local Means算法原理：

Non-Local Means顾名思义，这是一种非局部平均算法。何为局部平均滤波算法呢？那是在一个目标像素周围区域平滑取均值的方法，所以非局部均值滤波就意味着它使用图像中的所有像素，这些像素根据某种相似度进行加权平均。滤波后图像清晰度高，而且不丢失细节。

w代表权重。衡量相似度的方法有很多，最常用的是根据两个像素亮度差值的平方来估计。由于有噪声，单独的一个像素并不可靠，所以使用它们的邻域，只有邻域相似度高才能说这两个像素的相似度高。衡量两个图像块的相似度最常用的方法是计算他们之间的欧氏距离：

# Non-Local Means算法实现：

function [output]=NLmeansfilter(input,t,f,h)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%  input: image to be filtered
%  t: radio of search window
%  f: radio of similarity window
%  h: degree of filtering
%
%  Author: Jose Vicente Manjon Herrera & Antoni Buades
%  Date: 09-03-2006
%
%  Implementation of the Non local filter proposed for A. Buades, B. Coll and J.M. Morel in
%  "A non-local algorithm for image denoising"
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Size of the image
[m n]=size(input);

% Memory for the output
Output=zeros(m,n);

% Replicate the boundaries of the input image

% Used kernel
kernel = make_kernel(f);
kernel = kernel / sum(sum(kernel));

h=h*h;

for i=1:m
for j=1:n

i1 = i+ f;
j1 = j+ f;

W1= input2(i1-f:i1+f , j1-f:j1+f);

wmax=0;
average=0;
sweight=0;

rmin = max(i1-t,f+1);
rmax = min(i1+t,m+f);
smin = max(j1-t,f+1);
smax = min(j1+t,n+f);

for r=rmin:1:rmax
for s=smin:1:smax

if(r==i1 && s==j1) continue; end;

W2= input2(r-f:r+f , s-f:s+f);

d = sum(sum(kernel.*(W1-W2).*(W1-W2)));

w=exp(-d/h);

if w>wmax
wmax=w;
end

sweight = sweight + w;
average = average + w*input2(r,s);
end
end

average = average + wmax*input2(i1,j1);
sweight = sweight + wmax;

if sweight > 0
output(i,j) = average / sweight;
else
output(i,j) = input(i,j);
end
end
end

function [kernel] = make_kernel(f)

kernel=zeros(2*f+1,2*f+1);
for d=1:f
value= 1 / (2*d+1)^2 ;
for i=-d:d
for j=-d:d
kernel(f+1-i,f+1-j)= kernel(f+1-i,f+1-j) + value ;
end
end
end
kernel = kernel ./ f;       

%% 测试函数
clc,clear all,close all;
[wid,len,channels]=size(ima);
sigma=10;
rima=ima+sigma*randn(size(ima));

% denoise
fima=rima;
if channels>2
fori=1:channels
fima(:,:,i)=NLmeansfilter(rima(:,:,i),5,2,sigma);
end
end

% show results
subplot(1,3,1),imshow(uint8(ima)),title('original');
subplot(1,3,2),imshow(uint8(rima)),title('noisy');
subplot(1,3,3),imshow(uint8(fima)),title('filtered');

$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))$

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);

• 《a non-local algorithm for image denoising》[J].IEEE
• https://en.wikipedia.org/wiki/Non-local_means
• http://wenhuix.github.io/research/denoise.html
• http://blog.csdn.net/piaoxuezhong/article/details/78317861
• http://blog.csdn.net/tuyang120428941/article/details/7052487
• http://blog.csdn.net/u010839382/article/details/48241929

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客