Skip to content

Non-local means

Fact Value
Also called NL-means, Non-local means denoising, NLM
Status Implemented and tested
Family Nonlinear spatial filters
Engine Science engine
Type Nonlinear, statistical, patch similarity averaging
Output Image
Operator shown as algorithm (exact)
Support radius patchDistance + patchSize
Code engine/src/pixelnexus_engine/filters/edge_preserving.py, nlm
Operator code engine/src/pixelnexus_engine/filters/edge_preserving.py, nlm_representation
Packages scikit-image
Channels RGB, Brightness only, Color only

Overview

Non-local means replaces each pixel with a weighted mean of pixels whose surrounding patches look alike, after Buades, Coll and Morel. Unlike a blur, it does not only average neighbors. It averages pixels anywhere in a search window whose surroundings match. Noise does not repeat, but edges and textures do, so they average with copies of themselves and stay sharp. The filter runs in the PixelNexus engine, with scikit-image.

Four panels. A noisy bright disk and gray square on a dark ground. With the defaults, the disk, the square and the ground are smooth and the edges sharp. With a strength of 10, much of the noise is left. With a strength of 16 and a noise sigma of 20, the result is smooth and sharp again. Four panels. A noisy bright disk and gray square on a dark ground. With the defaults, the disk, the square and the ground are smooth and the edges sharp. With a strength of 10, much of the noise is left. With a strength of 16 and a noise sigma of 20, the result is smooth and sharp again.
A disk and a square with noise of sigma 20. Then non-local means with the defaults, with a strength h of 10, and with h 16 and a noise sigma of 20.

How it works

  1. Take the patch around each pixel.
  2. Compare it with the patch around every pixel of the search window.
  3. Weigh each pixel by how alike the patches are.
  4. Replace the pixel with the weighted mean.

Mathematical foundation

\[ u(x) = \frac{1}{C(x)} \sum_{y \in W(x)} e^{-\frac{d(x, y)^2}{h^2}} f(y), \qquad d(x, y)^2 = \frac{1}{\lvert P \rvert} \lVert P(x) - P(y) \rVert^2 \]

The formula shows the default parameter values.

where

  • \(P(x)\): The patch around pixel x
  • \(d(x, y)\): The distance between the patches of x and y
  • \(h\): The filter strength
  • \(W(x)\): The search window around x
  • \(C(x)\): The sum of the weights

P(x) is the patch around pixel x and d the distance between two patches, the mean squared difference of their pixels. Each pixel y in the search window W(x) gets a weight that falls with the patch distance, with the strength h. C(x) is the sum of the weights. So pixels whose surroundings look like the surroundings of x count the most, wherever they are in the window. Noise does not repeat, while edges and textures do. So averaging similar patches removes noise and keeps structure. With the noise sigma set, scikit-image subtracts the noise expected in the patch distance first.

The weights compare whole patches, not single pixels, so one noisy pixel cannot fool them. A strength h well below the noise leaves most weights small, and little gets averaged. Telling the filter the noise sigma helps, since scikit-image then subtracts the distance that the noise alone adds to every patch pair.

Kernel or operator

There is no fixed kernel. The Inspector asks the engine for the stages with the current settings.

Parameters

Parameter Default Values Meaning
Filter strength h 25.5 1 to 100 levels The cut-off distance h of the patch weights. scikit-image uses 0.1 of the range, which is 25.5 levels.
Noise sigma sigma 0 0 to 100 levels The standard deviation of the noise, if you know it. Above 0, scikit-image subtracts the expected noise from the patch distances. scikit-image uses 0.
Patch size patchSize 7 3 to 15 px The side of the square patches that are compared. scikit-image uses 7.
Search distance patchDistance 11 1 to 30 px How far from the pixel, in each direction, patches are searched. scikit-image uses 11.
Fast mode fastMode on On or off The fast mode of Darbon et al. weighs every pixel of a patch the same. Off, scikit-image weighs the patch with a Gaussian, as Buades, Coll and Morel did. scikit-image uses the fast mode.
Channels channelScope RGB RGB, Brightness only, Color only Which part of the color image the filter changes.

Implementation

nlm in engine/src/pixelnexus_engine/filters/edge_preserving.py passes each plane of the channel scope, scaled from 0 to 1, to skimage.restoration.denoise_nl_means. The strength and the noise sigma are given in levels and divided by 255. The fast mode is the one of Darbon et al., which weighs every pixel of a patch the same and is much quicker.

Worked example

Strength h Sigma PSNR (dB)
10 0 26.34
10 20 41.29
16 0 40.08
16 20 43.81
25.5 0 43.18
25.5 20 43.30

The image of the figure, with the other defaults. The PSNR compares the result with the image before the noise. The noisy input scores 22.22 dB.

At the default strength of 25.5 levels, the noise of sigma 20 drops from 22.22 dB to 43.18 dB. A strength of 10, half the noise, leaves most of it. With the noise sigma set, a strength of 16 does best here, close to the 0.8 sigma that scikit-image suggests.

Complexity

  • Time: \(O(N D^2)\)
  • Memory: \(O(N)\)

N is the number of pixels and D the side of the search window. The fast mode does not depend on the patch size.

Artifacts and limitations

Limitations

  • Slow for large search windows.
  • Unique detail, with no similar patch, stays noisy or is smoothed away.

Artifacts

  • A slightly painted look at high strength.
  • Faint noise left around edges, which find few similar patches.

Tests

Each plane must match denoise_nl_means with the strength and sigma divided by 255. On a noisy step, the error must fall below 0.6 of the input's. The representation must have its three stages.

References

  • Buades, A., Coll, B., and Morel, J.-M. A non-local algorithm for image denoising. IEEE Conference on Computer Vision and Pattern Recognition (CVPR), volume 2, 60 to 65, 2005. DOI 10.1109/CVPR.2005.38.
  • Darbon, J., Cunha, A., Chan, T. F., Osher, S., and Jensen, G. J. Fast nonlocal filtering applied to electron cryomicroscopy. IEEE International Symposium on Biomedical Imaging, 1331 to 1334, 2008. DOI 10.1109/ISBI.2008.4541250.
  • scikit-image documentation, skimage.restoration.denoise_nl_means.