# Denoising opencv image in C++, Video is the same. Image in the loop.

> The Opencv library has build-in a powerful denoising algorithm based on non-local means.

- Canonical URL: https://www.funvisiontutorials.com/blog/denoising-opencv-image-cpp-video-same-image/
- Author: Vladimir Kucera
- Published: 2016-10-31
- Updated: 2023-11-02
- Topics: Image processing, Installing & building OpenCV
- Videos: https://www.youtube.com/watch?v=mef6wpCtHQs

The Opencv library has build-in a powerful denoising algorithm based on non-local means. The method is based on the theory published in an article by Antoni Buades, Bartomu Coll, and Jean-Michel Morel [here](http://www.ipol.im/pub/art/2011/bcm_nlm/). The algorithm is relatively simple, but not so easy to implement. Basically, one single pixel is replaced by the average of the colors of the area around the most similar pixel. Let's say we want to replace pixel and algorithm finding the replacement in some window. It finds the most similar replacement and additionally takes a smaller window over that most similar replacement and calculates the mean value over that.

If we have the first window of some size and the second the smaller just one pixel. We probably replace the original one with the most similar one. If the most similar one is the noise. It will be noisy. This is a great method for calculating the output image from the sequence of input images to achieve better results. The video result is also impressive. Check the results and enjoy the simple code.

### fastNlMeansDenoisingColoredMulti  opencv denoising method results

These results are from my video. The upper part of the image is the denoised frame of the video calculated from the 4 surrounded frames. Original inputs are the lower part of the image. If you check the cropped part below the results are clearly visible. No to a much-blurred image, sharp edges, and much less noise. This is exactly what we want to achieve.

[Image: Denoising opencv noise reduction]

_Denoising video sample in opencv 3,1 noise reduction_

[Image: Denoising opencv noise reduction]

### iPhone sequence of images noise reduction

I just take the input 5 images by iPhone SE held in my hands. The result will be much better with 5 images using a tripod.

[Image: Denoising opencv noise reduction]**_[Image: Denoising opencv noise reduction]_**

[Image: Denoising opencv noise reduction]

[Image: Denoising opencv noise reduction]**_[Image: Denoising opencv noise reduction]_**

_ZOOM_ of one of the imput image

**_[Image: Denoising opencv noise reduction]_**

### Results of fastNl Means Denoising Colored Multimethod

**_[Image: Denoising opencv noise reduction]_**

### **_Noise reduction Zoom ot the result_**

[Image: Denoising opencv noise reduction]What do you think? Let me know.

### Opencv denoising **_(noise reduction) c++ code_**

There is nothing special. Fill the vector&lt;Mat> buffer(5); buffer with input images calculate from the buffer the method based on the templateWindowSize = 12 and searchWindowSize = 48. This is it.

```cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo.hpp>
#include <opencv2\videostab.hpp>
#include "opencv2/imgcodecs.hpp"
#include "opencv2\highgui.hpp"
#include "opencv2\imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
     vector buffer(5);
     Mat image;
     image = imread("i1.JPG", CV_LOAD_IMAGE_COLOR);
     buffer[0] = image;
     imshow("Display window", image);
     waitKey(2000);
     image = imread("i2.JPG", CV_LOAD_IMAGE_COLOR);
     buffer[1] = image;
     waitKey(2000);
     imshow("Display window", image);
     image = imread("i3.JPG", CV_LOAD_IMAGE_COLOR);
     buffer[2] = image;
     imshow("Display window", image);
     waitKey(2000);
     image = imread("i4.JPG", CV_LOAD_IMAGE_COLOR);
     buffer[3] = image;
     imshow("Display window", image);
     waitKey(2000);
     image = imread("i5.JPG", CV_LOAD_IMAGE_COLOR);
     buffer[4] = image;
     imshow("Display window", image);
     waitKey(2000);

     Mat img;
     int imgs_count = 5;
     fastNlMeansDenoisingColoredMulti(buffer, img, imgs_count / 2, imgs_count, 12, 48);
     namedWindow("Display window", WINDOW_AUTOSIZE);
     imshow("Display window", img);
     imwrite("result.jpg", img);
     waitKey(20000);
     return 0;
}
```

### **_Denoising Colored Video example_**

Video: https://www.youtube.com/watch?v=mef6wpCtHQs
