Non-Local means is a great algorithm to denoise an image. It's quite straightforward, however it may be difficult to understand for people not familiar with image processing. I did my best to explain the algorithm without complex math equations.

I would highly recommend first watching the following video for an introduction to the algorithm by Prof. Fred Hamprecht:

The algorithm is based on the idea that every image has similar regions. I.e in the following image regions q1 and q2 are similar to region p.

If we take similar regions and average them, then we'll get a denoised version of that region. In the image above regions q1 and q2 are very similar to region p, however region q3 is very different, therefore regions q1 and q2 will have a higher weight, and region q3 will have a lower weight. The denoised version of p can be described by the following simple formula:

V() is the grayscale value of a central pixel of a certain region, and W() is the weight of the whole region (higher weight = more similar to the original region). The weight W() will depend on the similarity between corresponding pixels in the original and searched region as well as the distance between the original and search regions.

Example: Let's say that e is the original pixel that we want to denoise, a-i is the region around that pixel:

[indent=1]1 2 3[color=#fff0f5]_____[/color]a b c

[indent=1]4 5 6[color=#fff0f5]_____[/color]d e f

[indent=1]7 8 9[color=#fff0f5]_____[/color]g h i

1-9 is the searched region. The weight of the searched region 1-9 will depend on the distance between pixels 5 and e (central pixels) and on the sum of differences in the grayscale value of 1 and a, 2 and b, and so on.

The non-local means algorithm uses a region of 7x7 pixels (around the pixel to be denoised), and looks for similar patterns in a 21x21 pixels neighbourhood.

Search region weight W = ( ( F(1,a) + F(2,b) + ...) / Fn ) * G(1,a);

G is gaussian function and F is the non-local means function:

In the above equations sigma is the gaussian strength. In our algorithm it should be equal to 10 (radius of the 21x21 search neighbourhood). x and y is the distance vector of a searched region's center (pixel 5) from the pixel's to be denoised (pixel e) center. h is the denoising strength. Fn is the normalizing factor for the searched region: Fn = F1 + F2 + F3...

The following pseudo-code will calculate the weight of a certain window (c and d is the offset in the 21x21 search neighbourhood):for (int a = c; a < c + 7; a++) { for (int b = d; b < d + 7; b++) { int2 srcCoord = (int2)(coord.x + a - c - 3, coord.y + b - d - 3); int2 nhCoord = (int2)(coord.x + a - 10, coord.y + b - 10); //Value of a corresponding pixel surrounding the pixel to be denoised (a-i) float pValue = srcImg.GetPixel(srcCoord); //Value of a corresponding pixel in the neighbourhood (1-9) float nValue = srcImg.GetPixel(nhCoord); float deltaValue = pValue - nValue; float curWeight = exp( - (deltaValue * deltaValue)/ (h*h) ); windowWeight += curWeight; }} Now windowWeight will have the 0..1 weight of the current searched region.

Then we simply have to multiply the value of the searched region's (1-9) central pixel by window weight and add it to the totalValue:windowWeight *= gaussianWeight; totalWeight += windowWeight;totalValue += windowCenterPixel * windowWeight; totalWeight is the normalization factor that will be used later. totalValue will be used for calculating a denoised value of the pixel.

After going over all possible 7x7 search regions in the 21x21 neighbourhood we normalize the totalValue: totalValue /= totalWeight;

totalValue is the denoised pixel.

I hope this introduction was helpful. Here's an OpenCL kernel, that implements the ideas described in this article://This is free and unencumbered software released into the public domain.////Anyone is free to copy, modify, publish, use, compile, sell, or//distribute this software, either in source code form or as a compiled//binary, for any purpose, commercial or non-commercial, and by any//means.////In jurisdictions that recognize copyright laws, the author or authors//of this software dedicate any and all copyright interest in the//software to the public domain. We make this dedication for the benefit//of the public at large and to the detriment of our heirs and//successors. We intend this dedication to be an overt act of//relinquishment in perpetuity of all present and future rights to this//software under copyright law.////THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,//EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF//MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.//IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR//OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,//ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR//OTHER DEALINGS IN THE SOFTWARE.////For more information, please refer to int2 GetWrappedCoord(const int2 _input, const int2 _imageDimensions) { int2 c = _input; while (c.x < 0) { c.x += _imageDimensions.x; } while (c.x >= _imageDimensions.x) { c.x -= _imageDimensions.x; } while (c.y < 0) { c.y += _imageDimensions.y; } while (c.y >= _imageDimensions.y) { c.y -= _imageDimensions.y; } return c;}__kernel void denoiseNLM(__read_only image2d_t srcImg, __write_only image2d_t dstImg, const uint xOffset, const uint yOffset, const float filteringStrength){ const sampler_t smp = CLK_NORMALIZED_COORDS_FALSE | //Natural coordinates CLK_ADDRESS_CLAMP_TO_EDGE | //Clamp to zeros CLK_FILTER_NEAREST; int2 imageDim = get_image_dim( srcImg ); int2 coord = (int2)(get_global_id(0) + xOffset, get_global_id(1) + yOffset); float totalValue = 0.0f; float totalWeight = 0.0f; int searchArea = 21; int halfSearchArea = (searchArea - 1) / 2; int searchAreaMaxStart = searchArea - 7; float gaussSigmaSQR = convert_float(halfSearchArea); gaussSigmaSQR *= gaussSigmaSQR; for (int c = 0; c < searchAreaMaxStart; c++) { for (int d = 0; d < searchAreaMaxStart; d++) { float windowWeight = 0.0f; float windowValue = 0.0f; for (int a = c; a < c + 7; a++) { for (int b = d; b < d + 7; b++) { int2 pWrapped = GetWrappedCoord( (int2)(coord.x + a - c - 3, coord.y + b - d - 3), imageDim ); int2 nhWrapped = GetWrappedCoord( (int2)(coord.x + a - halfSearchArea, coord.y + b - halfSearchArea), imageDim ); //Point float pValue = native_divide(convert_float( read_imageui(srcImg, smp, pWrapped).z ), 255.0f); //Neighbourhood float nValue = native_divide(convert_float( read_imageui(srcImg, smp, nhWrapped).z ), 255.0f); float deltaValue = pValue - nValue; float curWeight = native_exp( - native_divide( (deltaValue * deltaValue), (filteringStrength * filteringStrength) ) ); windowWeight += curWeight; } } //int2 windowCenterCoord = GetWrappedCoord( (int2)(coord.x + c - 7, coord.y + d - 7), imageDim ); int2 windowCenterCoord = GetWrappedCoord( (int2)(coord.x + c - halfSearchArea + 3, coord.y + d - halfSearchArea + 3), imageDim ); float windowCenterPixel = native_divide(convert_float( read_imageui(srcImg, smp, windowCenterCoord).z ), 255.0f); //Add distance factor to initial patch float2 diffVector = convert_float(windowCenterCoord - coord); float gaussianWeight = native_exp( - native_divide( dot(diffVector, diffVector), (2.0f * gaussSigmaSQR) ) ); gaussianWeight = native_divide(gaussianWeight, 2.0f * M_PI * gaussSigmaSQR); windowWeight *= gaussianWeight; totalWeight += windowWeight; totalValue += windowCenterPixel * windowWeight; } } totalValue = native_divide(totalValue, totalWeight); uint4 bgra; bgra.x = bgra.y = bgra.z = (uint) (totalValue * 255.0f); bgra.w = 255; write_imageui(dstImg, coord, bgra);}