Skip to main content

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

The Opencv library has build-in powerful denoising algorithm based on the non-local means. The method is based on the theory published in an article by Antoni Buades, Bartomu Coll, Jean-Michel Morel here. 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 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 mean value over that. 

If we have first window of some size and second the smaller just one pixel. We probably replace the original one with the most simular. If the most simular one is the noise. It will be noise. This is a great method for calculating the output image from the sequence of inputs image to achieve better results. 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 is clearly visible. No to a much-blured image, sharp edges and much less noisy. This is exactly what we want to achieve. 


Denoising opencv noise reduction
Denoising video sample in opencv 3,1 noise reduction
Denoising opencv noise reduction

iPhone sequence of images noise reduction

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



Denoising opencv noise reductionDenoising opencv noise reduction
Denoising opencv noise reduction


Denoising opencv noise reductionDenoising opencv noise reduction
ZOOM of one of the imput image

Denoising opencv noise reduction

Results of fastNl Means Denoising Colored Multi method

Denoising opencv noise reduction

Noise reduction Zoom ot the result


Denoising opencv noise reduction

What do you think? Let me know. 



Opencv denoising (noise reduction) c++ code

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


#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 argcchar** 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, 1248);
  namedWindow("Display window", WINDOW_AUTOSIZE);
  imshow("Display window", img);  
  imwrite("result.jpg", img);
  waitKey(20000);
  return 0;
}

Denoising Colored Video example




Comments

Post a Comment