Ask Your Question

Revision history [back]

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

Mat mSource_Bgr;
mSource_Bgr= imread("c:/Users/Laurent.PC-LAURENT-VISI/Downloads/ReaMu.png",CV_LOAD_IMAGE_ANYCOLOR);

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();

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

Mat mSource_Bgr;
mSource_Bgr= imread("c:/Users/Laurent.PC-LAURENT-VISI/Downloads/ReaMu.png",CV_LOAD_IMAGE_ANYCOLOR);

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();

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

Mat mSource_Bgr;
mSource_Bgr= imread("c:/Users/Laurent.PC-LAURENT-VISI/Downloads/ReaMu.png",CV_LOAD_IMAGE_ANYCOLOR);

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;

mSource_Bgr= imread("ReaMu.png",CV_LOAD_IMAGE_ANYCOLOR);
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();