How to add Noise to Color Image - Opencv

2019-02-19 11:25发布

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;
mSource_Bgr= imread(FileName_S,1);

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 enter image description here

Output Image with Noise enter image description here

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! enter image description here

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!

2条回答
一夜七次
2楼-- · 2019-02-19 11:56

Thank you @Andrey Smorodov For your insights! I got it working! Here is my updated code for adding Noise in a Color Image. Hope this will be useful for someone!

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

#include <iostream>

using namespace cv;
using namespace std;

inline BYTE 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[])
{
    Mat mSource= imread("input.png",1); 
    imshow("Source Image",mSource);

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

    AddGaussianNoise(mSource,mColorNoise,0,10.0);

    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;
}  
查看更多
3楼-- · 2019-02-19 12:02

Looks like your noise matrix can't get negative values as it have unsigned char element type. Try operate with real valued matrices, it should help.

查看更多
登录 后发表回答