# Adding Noise to Image - Opencv

Hi everyone!

I'm trying to to add noise to an Image & then Denoise it to test my DeNoising algorithm! So for benchmark i'm referring this Online Test samples. I'm trying to replicate the Noise model.

With reference to this threads 1 , 2 I'm adding noise to image like this!

Mat mSource_Bgr;

double m_NoiseStdDev=10;

Mat mNoise_Bgr = mSource_Bgr.clone();
Mat mGaussian_noise = Mat(mSource_Bgr.size(),CV_8UC3);

randn(mGaussian_noise,0,m_NoiseStdDev);
mNoise_Bgr += mGaussian_noise;

normalize(mNoise_Bgr,mNoise_Bgr,0, 255, CV_MINMAX, CV_8UC3);

imshow("Output Window",mNoise_Bgr);
//imshow("Gaussian Noise",mGaussian_noise);


My Input Image

Output Image with Noise

Problem:

Adding Noise to the image alters overall brightness of the Image which in turn alters my final results PSNR!

I want to get the results as much as closer to this one!

What i have tried so far!

I have tried to add the noise only in the color channel.

1. Convert the Input image into YUV Color space
2. Add the Noise only in the UV Color Channels & Keep the Y channel unaltered.

Results are very bad & the overall color of the image is getting altered! Will add the code if needed!

So any advice regarding this is much appreciated! May be give me some formulas for adding Noise to the image!

edit retag close merge delete

2

Input and output images seem to have roughly same brightness to me. What ranges are you talking about? Anyway, I don't think it's possible to get exact brightness between the two images, because the noise is by definition unwanted strange pixels values, so they will definitely alter the image statistics. Moreover, gaussian noise is a random process, so you will never get same output twice, and therefore you will probably never get same brightness for all outputs

( 2015-08-14 03:17:42 -0500 )edit

@LorenaGdL you are right by the way! But if you look at the samples provided in the benchmark images, you can see that the noise is getting added only in the color channel & the overall image brightness is not getting affected!

( 2015-08-14 05:05:29 -0500 )edit

Sort by » oldest newest most voted

I have found the possible bug in my code which is the Noise Mat type(Unsigned char). This will trim the Negative values from the Noise Mat, so that image will look brighter! Here is my updated code!

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

inline uchar Clamp(int n)
{
n = n>255 ? 255 : n;
return n<0 ? 0 : n;
}

bool AddGaussianNoise(const Mat mSrc, Mat &mDst,double Mean=0.0, double StdDev=10.0)
{
if(mSrc.empty())
{
cout<<"[Error]! Input Image Empty!";
return 0;
}

Mat mGaussian_noise = Mat(mSrc.size(),CV_16SC3);
randn(mGaussian_noise,Scalar::all(Mean),Scalar::all(StdDev));

for (int Rows = 0; Rows < mSrc.rows; Rows++)
{
for (int Cols = 0; Cols < mSrc.cols; Cols++)
{
Vec3b Source_Pixel= mSrc.at<Vec3b>(Rows,Cols);
Vec3b &Des_Pixel= mDst.at<Vec3b>(Rows,Cols);
Vec3s Noise_Pixel= mGaussian_noise.at<Vec3s>(Rows,Cols);

for (int i = 0; i < 3; i++)
{
int Dest_Pixel= Source_Pixel.val[i] + Noise_Pixel.val[i];
Des_Pixel.val[i]= Clamp(Dest_Pixel);
}
}
}

return true;
}

bool AddGaussianNoise_Opencv(const Mat mSrc, Mat &mDst,double Mean=0.0, double StdDev=10.0)
{
if(mSrc.empty())
{
cout<<"[Error]! Input Image Empty!";
return 0;
}
Mat mSrc_16SC;
Mat mGaussian_noise = Mat(mSrc.size(),CV_16SC3);
randn(mGaussian_noise,Scalar::all(Mean), Scalar::all(StdDev));

mSrc.convertTo(mSrc_16SC,CV_16SC3);
addWeighted(mSrc_16SC, 1.0, mGaussian_noise, 1.0, 0.0, mSrc_16SC);
mSrc_16SC.convertTo(mDst,mSrc.type());

return true;
}

int main(int argc, const char* argv[])
{
imshow("Source Image",mSource);

Mat mColorNoise(mSource.size(),mSource.type());

imshow("Source + Color Noise",mColorNoise);

AddGaussianNoise_Opencv(mSource,mColorNoise,0,10.0);//I recommend to use this way!

imshow("Source + Color Noise OpenCV",mColorNoise);

waitKey();
return 0;
}

more

Thanks a lot for your prompt update

( 2016-06-07 04:34:43 -0500 )edit

I have added noise to color image in this way. But when I am calculating the MSE, it is very low compared to the added noise. For std=20, the MSE is below 200. But I think MSE should be close to square of std of the random noise(if std=20 then MSE around 400). How to add noise so that the MSE becomes square of the standard deviation of the noise?

( 2016-06-07 06:15:02 -0500 )edit

@Hossain Md Shakhawat BYTE is equalent to uchar

( 2016-06-07 06:20:16 -0500 )edit

Another fact is I am adding same amount of noise to different color images. But when calculating MSE, their values are different for different images. Why is that? According to my understanding it should same for all images, if the amount of noise being added is same.

( 2016-06-17 08:37:26 -0500 )edit

Indeed your issue was that you were not working with signed images. And in the following code you can find an alternative in order to avoid looping through the image pixels, which my guess would be that it is more time consuming (I have not test it to be honest) than using the internal functions of opencv:

void addGaussianNoise(Mat &image, double average=0.0, double standard_deviation=10.0)
{
// We need to work with signed images (as noise can be
// negative as well as positive). We use 16 bit signed
// images as otherwise we would lose precision.
Mat noise_image(image.size(), CV_16SC3);
randn(noise_image, Scalar::all(average), Scalar::all(standard_deviation));
Mat temp_image;
image.convertTo(temp_image,CV_16SC3);
addWeighted(temp_image, 1.0, noise_image, 1.0, 0.0, temp_image);
temp_image.convertTo(image,image.type());
}

more

This solved my problem

( 2015-12-29 21:22:31 -0500 )edit

As you gaussian noise is ranging 0 to 255 you add energy to image I think you can substract mean to your result and it should be OK

Mat mSource_Bgr;

double m_NoiseStdDev=10;

Mat mNoise_Bgr = mSource_Bgr.clone();
Mat mGaussian_noise = Mat(mSource_Bgr.size(),CV_8UC3);

randn(mGaussian_noise,0,m_NoiseStdDev);
double minIm,maxIm;

std::vector<Mat> plan;
split(mGaussian_noise,plan);
for (int i = 0; i < mGaussian_noise.channels(); i++)
{
minMaxIdx(plan[i],&minIm,&maxIm);
cout << minIm << "\t";
cout << maxIm << "\n";
}
mNoise_Bgr += mGaussian_noise;
Scalar x=mean(mGaussian_noise);
cout << x << "\n";
subtract(mNoise_Bgr,x,mNoise_Bgr);

imshow("Output Window",mNoise_Bgr);
waitKey();


Following remark from @LorenaGdL and @Balaji R I think this code is better (negative value at the end are not process and it could be a problem) :

Mat mSource_Bgr;
double minIm,maxIm;
std::vector<Mat> plan;
Scalar x;

cout << "Original image\n";
split(mSource_Bgr,plan);
for (int i = 0; i < mSource_Bgr.channels(); i++)
{
minMaxIdx(plan[i],&minIm,&maxIm);
cout << minIm << "\t";
cout << maxIm << "\n";
}
x=mean(mSource_Bgr);
cout << x << "\n";

double m_NoiseStdDev=10;

Mat mNoise_Bgr ;
mSource_Bgr.convertTo( mNoise_Bgr,CV_32FC3, 1,0);

Mat mGaussian_noise = Mat(mSource_Bgr.size(),CV_32FC3);

randn(mGaussian_noise,0,m_NoiseStdDev);
mNoise_Bgr += mGaussian_noise;

cout << "Gaussian noise\n";
split(mGaussian_noise,plan);
for (int i = 0; i < mGaussian_noise.channels(); i++)
{
minMaxIdx(plan[i],&minIm,&maxIm);
cout << minIm << "\t";
cout << maxIm << "\n";
}
x=mean(mGaussian_noise);
cout << x << "\n";

cout << "Result image+ gausssian noise\n";
split(mNoise_Bgr,plan);
for (int i = 0; i < mNoise_Bgr.channels(); i++)
{
minMaxIdx(plan[i],&minIm,&maxIm);
cout << minIm << "\t";
cout << maxIm << "\n";
}
x=mean(mNoise_Bgr);
cout << x << "\n";

Mat result;
mNoise_Bgr.convertTo(result,CV_8UC3,1,0);
imshow("Output Window",result);
cout << "Image with noise\n";
split(result,plan);
for (int i = 0; i < mGaussian_noise.channels(); i++)
{
minMaxIdx(plan[i],&minIm,&maxIm);
cout << minIm << "\t";
cout << maxIm << "\n";
}
x=mean(result);
cout << x << "\n";

waitKey();

more

I don't agree. That will alter those pixels that were not affected by the noise. That's not what happens in real scenarios, and a denoising algorithm shouldn't be based on that.

( 2015-08-14 04:48:59 -0500 )edit

@LorenaGdL I think that question was about brightness. In code example given by @Balaji R gaussian noise was not zero mean. Adding this noise increases image brightness. After what do you mean by real scenarios ? So many noises exist in real world...

( 2015-08-14 05:05:54 -0500 )edit

@Balaji R It depends which process is added (or multiplied or ...) to your signal.

For example you can an aquarium between your book and your camera. After in this aquarium you 've got small particles and a temperature gradient and a flow and some fishes....

IMHO There is no general algorithm for denoising (and simulate noise)

( 2015-08-14 05:14:08 -0500 )edit

@LBerger: just saying that in my opinion a denoising algorithm shouldn't be based on a noise that generates an output image with the very same brightness as the input image. Want to use a zero mean noise? Perfect, but the mean will probably be slightly different from zero each time the noise is added, and the deviation shouldn't be important to the denoising algorithm. Subtracting the exact mean of each channel in the noise matrices seems too artificial to me. Does that noise exist in the world? Sure. Is a good way to generalize? I don't think so.

( 2015-08-14 05:32:34 -0500 )edit

Moreover I don't like the code itself. The reason is you're using a random Gaussian noise of mean zero and standard deviation 10, but then you're storing it in a 8UC3 Mat, so you're just using the positive part of the probability function (negative values are discarded). Of course when you then subtract the means you end up with a zero mean noise (same brightness), but that's not gaussian noise and therefore the standard deviation loses its meaning.

( 2015-08-14 05:53:29 -0500 )edit

@LorenaGdL Ok you are right I Change my code and you can give your opinion how to process negative value or greater than 255. It could be threshold or changed dynamic or ...

( 2015-08-14 07:00:29 -0500 )edit

Official site

GitHub

Wiki

Documentation