1

I'm trying to to add noise to an Image & then denoised to see the difference in my object detection algorithm. So I developed OpenCV code in C++ for detection some objects in the image. I would like to test the robustness of the code, so tried to add some noises. In that way would like to check how the object detection rate changed when add noises to the image. So , first added some random Gaussian Noises like this

cv::Mat noise(src.size(),src.type());
 float m = (10,12,34);
 float sigma = (1,5,50);
 cv::randn(noise, m, sigma); //mean and variance
 src += noise;

I got this images:

The original: enter image description here The noisy oneenter image description here

So is there any better model for noises? Then how to Denoise it. Is there any DeNoising algorithms?

bob
  • 363
  • 3
  • 8
  • 21
  • 3
    Just a small hint: in C++17, ```float m = (10,12,34);``` evaluates m to 34. –  Sep 13 '19 at 14:32
  • 1
    For those trying to copy this code, it would be better to use `float m = 34` and `float sigma = 50`. The code provided here does not follow proper syntax, as explained here: https://stackoverflow.com/questions/76862615/error-left-operand-of-comma-operator-has-no-effect-werror-unused-value-with?noredirect=1#comment135502526_76862615 – Ruo Aug 08 '23 at 19:39
  • The syntax is correct, but the semantics are weird and redundant. Without an explanation of what the OP intended here, we can only speculate. It _looks_ like they want to generate three sets of noises and add each one to the image. – paddy Aug 08 '23 at 19:44

2 Answers2

1

Check this tutorial it might help you.

http://docs.opencv.org/trunk/d5/d69/tutorial_py_non_local_means.html

Specially this part:

OpenCV provides four variations of this technique.

cv2.fastNlMeansDenoising() - works with a single grayscale images

cv2.fastNlMeansDenoisingColored() - works with a color image.

cv2.fastNlMeansDenoisingMulti() - works with image sequence captured in short period of time (grayscale images)

cv2.fastNlMeansDenoisingColoredMulti() - same as above, but for color images.

Common arguments are:

h : parameter deciding filter strength. Higher h value removes noise better, but removes details of image also. (10 is ok)

hForColorComponents : same as h, but for color images only. (normally same as h)

templateWindowSize : should be odd. (recommended 7)

searchWindowSize : should be odd. (recommended 21)

And to add gaussian noise to image, maybe this thread will be helpful:

How to add Noise to Color Image - Opencv

Community
  • 1
  • 1
pb772
  • 539
  • 1
  • 3
  • 14
  • I cant add opencv_photo249.lib. – bob May 12 '17 at 09:05
  • Im using OpenCV 2.4. I add libopencv_photo2413 to the MinGW C++ library . but still error ..\main.cpp:141:73: error: 'fastNlMeansDenoisingColored' was not declared in this scope – bob May 12 '17 at 09:31
1

OpenCV comes with Photo package in which you can find an implementation of Non-local Means Denoising algorithm. The documentation can be found here: http://docs.opencv.org/3.0-beta/modules/photo/doc/denoising.html

As far as I know it's the only suitable denoising algorithm both in OpenCV 2.4 and OpenCV 3.x

I'm not aware of any other noise models in OpenCV than randn. It shouldn't be a problem however to add a custom function that does that. There are some nice examples in python (you should have no problem rewriting it to C++ as the OpenCV API remains roughly identical) How to add noise (Gaussian/salt and pepper etc) to image in Python with OpenCV

There's also one thing I don't understand: If you can generate noise, why would you denoise the image using some algorithm if you already have the original image without noise?

Community
  • 1
  • 1
Max Walczak
  • 433
  • 2
  • 12
  • Well, Im doing airplane door detection. As the algorithm should work by all weather conditions I assume will have images with rain or sun reflection or different light conditions(sun, dark... ). So , first I tried to add some random noises and see how my detection rate drop . Then I have to apply some algorithm that denoise the image(means denoise it from rain, sun reflection, dak or any kind of noises can occur). Hope is clear now – bob May 12 '17 at 07:59