Search code examples
image-processingopencv

Impulse, gaussian and salt and pepper noise with OpenCV


I'm studying Image Processing on the famous Gonzales "Digital Image Processing" and talking about image restoration a lot of examples are done with computer-generated noise (gaussian, salt and pepper, etc). In MATLAB there are some built-in functions to do it. What about OpenCV?


Solution

  • As far as I know there are no convenient built in functions like in Matlab. But with only a few lines of code you can create those images yourself.

    For example additive gaussian noise:

    Mat gaussian_noise = img.clone();
    randn(gaussian_noise,128,30);
    

    Salt and pepper noise:

    Mat saltpepper_noise = Mat::zeros(img.rows, img.cols,CV_8U);
    randu(saltpepper_noise,0,255);
    
    Mat black = saltpepper_noise < 30;
    Mat white = saltpepper_noise > 225;
    
    Mat saltpepper_img = img.clone();
    saltpepper_img.setTo(255,white);
    saltpepper_img.setTo(0,black);