From Avisynth wiki
Jump to: navigation, search
Author tritical, pinterf
Version 1.1
Download TNLMeans_v1.1.7z
Category Spatial-Temporal Denoisers
License GPLv2
Discussion Doom9 Thread


[edit] Description

TNLMeans is an implementation of the NL-means denoising algorithm. Aside from the original method, TNLMeans also supports extension into 3D, a faster, block based approach, and a multiscale version.

[edit] Requirements

[edit] Theory Of Operation:

The NL-means algorithm works in the following manner. For each pixel in the image define a search window in which to look for similar pixels. The search window is defined by the parameters Ax and Ay, which set the x-axis radius and y-axis radius. For each pixel in the window determine a weight based on the similarity of that pixel's gray level neighborhood to the center pixel's gray level neighborhood. The neighborhood is defined by the Sx and Sy parameters, which set the x-axis radius and y-axis radius. The similarity between two neighborhoods is measured using gaussian weighted (as a function of distance, the standard deviation is set by the "a" parameter) sum of squared differences. The final weight for a pixel is computed as:


If the parameter 'sse' is set to false, then sum of absolute differences is used instead of sum of squared differences. In that case, the final weight for a pixel is computed as:


Once a weight for each pixel in the window is acquired, the final pixel value is simply the weighted average of all the pixels. In order for the center pixel to not be too heavily weighted, it is assigned a weight equal to the largest weight given to another pixel in the search window.

The block based modification changes the base step (or base window) from 1 pixel to blocks with size Bx and By where Bx and By set the x-axis radius and y-axis radius. The support and search windows still work the same way, but now whole blocks are computed/averaged at once instead of individual pixels. This modification cuts the computation time down by Bx*2+1)*(By*2+1) times.

The 3D extension allows extending the search window into neighbor frames. The parameter Az sets the temporal (z-axis) radius. With Az=1 frames n-1 and n+1 would be included.

The multiscale version works by running the normal algorithm with Ax/Ay = 2 on the original image, and then running the algorithm again on a downsampled (width/2,height/2) version of the original image with Ax/Ay/Bx/By/Sx/Sy all divided by 2. The weights from the two scales are then combined to form the final image. This process can greatly speed up processing for large search windows but sacrifices quality (especially around edges/lines/fine details). The type of downsampling that is used is set by the 'rm' parameter.

For more information see the following papers:

[edit] Syntax and Parameters

TNLMeans (clip, int "Ax", int "Ay", int "Az", int "Sx", int "Sy", int "Bx", int "By", bool "ms", int "rm", float "a", float "h", bool "sse")

clip   =
Input clip; filter works all 8-16 bits Y and planar YUV/RGB color spaces and YUY2.
Alpha channel - if exists - is simply copied.

int  Ax = 4
int  Ay = 4
int  Az = 0
These set the x-axis, y-axis, and z-axis radii of the search window. These must be greater than or equal to 0. The full window size will be:
(Ax*2+1) x (Ay*2+1) x (Az*2+1)
Generally, the larger the search window the better the result of the denoising. Of course, the larger the search window the longer the denoising takes.

int  Sx = 2
int  Sy = 2
These set the x-axis and y-axis radii of the support (similarity neighborhood) window. These must be greater than or equal to 0. A larger similarity window will retain more detail/texture but will also cause less noise removal. Typical values for Sx/Sy are 2 or 3. The full window size will be: (Sx*2+1) x (Sy*2+1)

int  Bx = 1
int  By = 1
These set the x-axis and y-axis radii of the base window. In the original NL-means algorithm the base was a single pixel (Bx=0 and By=0). Using blocks larger than a single pixel will sacrifice some quality for speed. Note that Sx must be greater than or equal to Bx and Sy must be greater than or equal to By. It is recommended that Sx/Sy be larger than Bx/By.

bool  ms = false
Controls whether or not the multiscale version is used. The multiscale version is faster but lower quality. The larger ax/ay are the greater the speed increase from using the multiscale version will be. If Ax/Ay are less than or equal to 2 then the multiscale version will not give any speed up and will make things slower. The multiscale version requires mod 8 input (width divisible by 8).

int  rm = 4
If ms = true, then rm sets the type of resizing used for downsampling. Possible options:
  • 0 - Point
  • 1 - Bilinear
  • 2 - Bicubic
  • 3 - Lanczos3
  • 4 - Spline16
  • 5 - Spline36

float  a = 1.0
Sets the standard deviation of the Gaussian used for weighting the difference calculation used for computing neighborhood similarity. Smaller values will result in less noise removal but will retain more detail/texture.

float  h = 1.8
Controls the strength of the filtering (blurring). Larger values will remove more noise but will also destroy more detail. 'h' should typically be set equal to the standard deviation of the noise in the image when using sse=true and assuming the noise fits the zero mean, Gaussian model.
  • h defaults to 0.5 when sse=false

bool  sse = true
Controls whether sum of squared differences or sum of absolute differences is used when computing neighborhood similarity. sse is slightly slower but retains fine detail/texture better. sad typically works better for cartoons/anime. The 'h' parameter usually needs to be set about 4 times lower when using sad than when using sse.
  • true - use sse
  • false - use sad

[edit] Examples

TNLMeans with all default settings:

TNLMeans(Ax=4, Ay=4, Az=0, Sx=2, Sy=2, Bx=1, By=1, ms=false, rm=4, a=1.0, h=1.8, sse=true)

[edit] Changelog

Version          Date(D/M/Y)      Changes
v1.1.0 04/10/2021 - Changes by pinterf - project moved to github: - project update to Visual Studio 2019 - Change to Avisynth v2.6 interface (from 2.5) - pass frame properties for AviSynth+ interface version >= 8 - Source code syntax updates: gcc, clang - Add planar Y, YUV and RGB formats (but kept YUY2 by on-the-fly pre-post conversion) - Add x64 config - linux/gcc build, CMake build environment - 10-16 bit support
v1.0.3 08/28/2007 - Removed fast exp() approximation that was used for sse=false. Turns out it was quite inaccurate and had overflow problems resulting in artifacts.
v1.0.2 07/30/2006 - Fixed a problem with small weights causing artifacts
v1.0.1 06/19/2006 - Fixed a bug that caused a crash when ms=true was used with yuy2 input
v1.0 Final 05/31/2006 - Fixed always creating the downsampled clip unless ms=false was explicitly specified
v1.0 Beta 2 05/25/2006 + Added multiscale version (parameters ms/rm) + Added sse parameter + Optimized non-block based routines by buffering (100% speed increase) - Removed b parameter - Fixed a bug in the block based routines that caused some blocks in the search window not to be tested - Changed defaults for ax/ay/sx/sy/h
v1.0 Beta 1 05/17/2006 - Initial Release

[edit] Archived Downloads

Version Download Mirror
v1.0.3 (x86/x64) TNLMeans103_ICL.7z TNLMeans103_ICL.7z /// MediaFire

[edit] External Links

  • GitHub - Source code repository.
  • GitHub - VapourSynth port of TNLMeans.

Back to External Filters

Personal tools