Ask Your Question

Revision history [back]

Here is a Sample Program to reproduce the same!

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

#include <iostream>

using namespace cv;
using namespace std;

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 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;
}

double GetPSNR(const Mat& I1, const Mat& I2,double &MSE) 
{ 
    Mat s1; 
    absdiff(I1, I2, s1);       // |I1 - I2| 
    s1.convertTo(s1, CV_32F);  // cannot make a square on 8 bits 
    s1 = s1.mul(s1);           // |I1 - I2|^2 
    Scalar s = sum(s1);         // sum elements per channel 
    double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels 

    if( sse <= 1e-10) // for small values return zero 
        return 0; 
    else 
    { 
        double  mse =sse /(double)(I1.channels() * I1.total()); 
        MSE= mse;        
        double psnr = 10.0*log10((255*255)/mse); 

        return psnr; 
    } 
} 


int main(int argc, const char* argv[])
{
    Mat mScene= imread("input.png",1);  
    imshow("Source Image",mScene);

    Mat mNoise(mScene.size(),mScene.type());
    double Mean=0.0,StdDev=20.0;

    AddGaussianNoise(mScene,mNoise,Mean,StdDev);

    imshow("mNoise",mNoise);

    double PSNR=0.0,MSE=0.0;

    PSNR= GetPSNR(mScene,mNoise,MSE);

    cout<< "PSNR : "<<PSNR<<" \n MSE :"<<MSE <<"\n";

    waitKey();
    return 0;
}