# Non Local Means¶


This numerical tour study image denoising using non-local means. This algorithm has been introduced for denoising purposes in BuaCoMoA05

In [2]:
using PyPlot
using NtToolBox
# arequire("NtToolBox")


## Patches in Images¶

This numerical tour is dedicated to the study of the structure of patches in images.

Size $N = n \times n$ of the image.

In [3]:
n = 128;


We load a noisy image $f_0\in \RR^N$.

In [4]:
c = [100, 200]
f0 = rescale(f0[c[1] - Base.div(n, 2) + 1 : c[1] + Base.div(n, 2), c[2] - Base.div(n, 2) + 1 : c[2] + Base.div(n, 2)]);


Display $f_0$.

In [4]:
figure(figsize = (5, 5))
imageplot(f0)


Noise level $\si$.

In [5]:
sigma = .04;


Generate a noisy image $f=f_0+\epsilon$ where $\epsilon \times \Nn(0,\si^2\text{Id}_N)$.

In [6]:
using Distributions
f = f0 .+ sigma.*rand(Normal(), n, n);


Display $f$.

In [7]:
figure(figsize = (5,5))
imageplot(clamP(f))


We denote $w$ to be the half width of the patches, and $w_1=2w+1$ the full width.

In [8]:
w = 3
w1 = 2*w + 1;


We set up large $(n,n,w_1,w_1)$ matrices to index the the X and Y position of the pixel to extract.

Location of pixels to extract.

In [9]:
include("NtToolBox/src/ndgrid.jl")

(Y, X, dX, dY) = ndgrid(1 : n, 1 : n, -w : w, -w : w)

X = X + dX
Y = Y + dY;


We handle boundary condition by reflexion

In [10]:
X[X .< 1] = 2 .- X[X .< 1]
Y[Y .< 1] = 2 .- Y[Y .< 1]
X[X .> n] = 2*n .- X[X .> n]
Y[Y .> n] = 2*n .- Y[Y .> n];


Patch extractor operator.

In [11]:
I = X .+ (Y .- 1)*n

for k in 1 : Base.div(n, w)
for l in 1 : Base.div(n, w)
I[k, l, :, :] = transpose(I[k, l, :, :])
end
end

function patch(f)
return f'[I]
end;


Define the patch matrix $P$ of size $(n,n,w_1,w_1)$. Each $P(i,j,:,:)$ represent an $(w_1,w_1)$ patch extracted around pixel $(i,j)$ in the image.

In [12]:
P = patch(f);


Display some example of patches.

In [13]:
figure(figsize = (5,5))
for i in 1:16
x = rand(1 : n)
y = rand(1 : n)
imageplot(P[x, y, :, :], "", [4, 4, i])
end


## Dimensionality Reduction with PCA¶

Since NL-means type algorithms require the computation of many distances between patches, it is advantagous to reduce the dimensionality of the patch while keeping as much as possible of information.

Target dimensionality $d$.

In [14]:
d = 25;


A linear dimensionality reduction is obtained by Principal Component Analysis (PCA) that projects the data on a small number of leading direction of the covariance matrix of the patches.

Turn the patch matrix into an $(w_1*w_1,n*n)$ array, so that each $P(:,i)$ is a $w_1*w_1$ vector representing a patch.

In [15]:
resh = P -> transpose((reshape(P, (n*n,w1*w1))));


Operator to remove the mean of the patches to each patch.

In [16]:
remove_mean = Q -> Q - repeat(mean(Q, 1), inner = (w1*w1, 1));


Compute the mean and the covariance of the points cloud representing the patches.

In [17]:
P1 = remove_mean(resh(P))
C = P1*transpose(P1);


Extract the eigenvectors, sorted by decreasing amplitude.

In [18]:
(D, V) = eig(C)
D = D[end : -1 : 1]
V = V[:, end : -1 : 1];


Display the decaying amplitude of the eigenvalues.

In [19]:
plot(D, linewidth = 2)
ylim(0, maximum(D))
show()


Display the leading eigenvectors - they look like Fourier modes.

In [20]:
figure(figsize = (5,5))
for i in 1 : 16
imageplot(abs(reshape(V[:, i], (w1,w1))'), "", [4, 4, i])
end


Patch dimensionality reduction operator.

In [21]:
iresh = Q -> reshape(Q', (n, n, d))
descriptor = f -> iresh(V[: , 1 : d]'*remove_mean(resh(P)));


Each $H(i,j,:)$ is a $d$-dimensional descriptor of a patch.

In [22]:
H = descriptor(f);


## Non-local Filter¶

NL-means applies, an adaptive averaging kernel is computed from patch distances to each pixel location.

We denote $H_{i} \in \RR^d$ the descriptor at pixel $i$. We define the distance matrix $$D_{i,j} = \frac{1}{w_1^2}\norm{H_i-H_j}^2.$$

Operator to compute the distances $(D_{i,j})_j$ between the patch around $i=(i_1,i_2)$ and all the other ones.

In [23]:
distance = i -> sum((H - repeat(reshape(H[i[1], i[2], :], (1, 1, 25)), inner = [n, n, 1])).^2, 3)./(w1*w1);


The non-local mean filter computes a denoised image $\tilde f$ as :

$$\tilde f_i = \sum_j K_{i,j} f_j$$

where the weights $K$ are computed as : $$K_{i,j} = \frac{ \tilde K_{i,j} }{ \sum_{j'} \tilde K_{i,j'} } \qandq \tilde K_{i,j} = e^{-\frac{D_{i,j}}{2\tau^2}} .$$

The width $\tau$ of the Gaussian is very important and should be adapted to match the noise level.

Compute and normalize the weight.

In [24]:
normalize = K -> K./sum(K)
kernel = (i, tau) -> normalize(exp(-distance(i)./(2*tau^2)));


Compute a typical example of kernel for some pixel position $(x,y)$.

In [25]:
tau = .05
i = [84, 73]
D = distance(i)
K = kernel(i, tau);


Display the squared distance and the kernel.

In [26]:
figure(figsize = (10,10))
imageplot(D[:, :], "D", [1, 2, 1])
imageplot(K[:, :], "K", [1, 2, 2])

Out[26]:
PyObject <matplotlib.text.Text object at 0x000000002E345F98>

## Localizing the Non-local Means¶

We set a "locality constant" $q$ that set the maximum distance between patches to compare. This allows to speed up computation, and makes NL-means type methods semi-global (to avoid searching in all the image).

In [27]:
q = 14;


Using this locality constant, we compute the distance between patches only within a window. Once again, one should be careful about boundary conditions.

In [28]:
selection = i -> [clamP(i[1] - q : i[1] + q, 1, n)'; clamP(i[2] - q : i[2] + q, 1, n)'];


Compute distance and kernel only within the window.

In [29]:
function distance_0(i,sel)
H1 = (H[sel[1, :], :, :])
H2 = (H1[:, sel[2, :], :])
return sum((H2 - repeat(reshape(H[i[1], i[2], :], (1, 1, length(H[i[1], i[2], :]))), inner = [length(sel[1, :]), length(sel[1, :]), 1])).^2, 3)/w1*w1
end

distance = i -> distance_0(i, selection(i))
kernel = (i, tau) -> normalize(exp(-distance(i)./(2*tau^2)));


Compute a typical example of kernel for some pixel position $(x,y)$.

Display the squared distance and the kernel.

In [30]:
sel = selection(i)
D = distance(i)
K = kernel(i, tau);

In [31]:
figure(figsize = (10,10))

imageplot(D[:, :], "D", [1, 2, 1])
imageplot(K[:, :], "K", [1, 2, 2])

Out[31]:
PyObject <matplotlib.text.Text object at 0x000000002E31E240>

The NL-filtered value at pixel $(x,y)$ is obtained by averaging the values of $f$ with the weight $K$.

In [32]:
function NLval_0(K,sel)
f_temp = f[sel[1, :], :]
return sum(K.*f_temp[:, sel[2, :]])
end

NLval = (i, ta) -> NLval_0(kernel(i, tau), selection(i));


We apply the filter to each pixel location to perform the NL-means algorithm.

In [33]:
(Y, X) = meshgrid(0 : n - 1, 0 : n - 1)

function arrayfun(f, X, Y)
n = size(X)[1]
p = size(Y)[1]
R = zeros(n, p)
for k in 1:n
for l in 1:p
R[k,l] = f(k,l)
end
end
return R
end

NLmeans = tau -> arrayfun((i1, i2) -> NLval([i1,i2], tau), X, Y);


Display the result for some value of $\tau$.

In [34]:
tau = .03

figure(figsize = (5,5))
imageplot(NLmeans(tau))

In [35]:
tau = .03;


Exercise 1

Compute the denoising result for several values of $\tau$ in order to determine the optimal denoising that minimizes $\norm{\tilde f - f_0}$.

In [36]:
include("NtSolutions/denoisingadv_6_nl_means/exo1.jl")

In [36]:
## Insert your code here.


Display the best result.

In [37]:
figure(figsize = (5,5))
imageplot(clamP(fNL))


Exercise 2

Explore the influence of the $q$ and $w$ parameters.

In [38]:
include("NtSolutions/denoisingadv_6_nl_means/exo2.jl")

WARNING: Method definition patch(Any) in module Main at In[11]:14 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:34.
WARNING: Method definition distance_0(Any, Any) in module Main at In[29]:2 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:58.
WARNING: Method definition NLval_0(Any, Any) in module Main at In[32]:2 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:69.
WARNING: Method definition patch(Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:34 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:34.
WARNING: Method definition distance_0(Any, Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:58 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:58.
WARNING: Method definition NLval_0(Any, Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:69 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:69.
WARNING: Method definition patch(Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:34 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:34.
WARNING: Method definition distance_0(Any, Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:58 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:58.
WARNING: Method definition NLval_0(Any, Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:69 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:69.
WARNING: Method definition patch(Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:34 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:34.
WARNING: Method definition distance_0(Any, Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:58 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:58.
WARNING: Method definition NLval_0(Any, Any) in module Main at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:69 overwritten at C:\Users\Ayman\.julia\v0.5\Exos\denoisingadv_6_nl_means\exo2.jl:69.

In [39]:
## Insert your code here.