图像的 SNR 和 PSNR 的计算

PSNR 的公式很容易搜到。

http://www.360doc.com/content/12/0605/21/4129998_216244993.shtml
http://blog.sina.com.cn/s/blog_455c7a600101ytgo.html

峰值信噪比经常用作图像压缩等领域中信号重建质量的测量方法,它常简单地通过均方差(MSE)进行定义。两个m×n单色图像IK,如果一个为另外一个的噪声近似,那么它们的的均方差定义为:

峰值信噪比定义为:

代码实现(参考:http://stackoverflow.com/questions/29428308/snr-of-an-image-in-c-using-opencv)

double getPSNR(const Mat& I1, const Mat& I2){    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    ] + s.val[] + s.val[]; // sum channels    ) // for small values return zero        ;    else    {        double  mse =sse /(double)(I1.channels() * I1.total());        *)/mse);        return psnr;    }}

SNR 不太好搜。

[OpenCV] SNR 與 PSNR

http://blog.csdn.net/lien0906/article/details/30059747

SNR (Signal to Noise Ratio):訊號雜訊比,簡稱訊雜比。

PSNR (Peak Signal to Noise Ratio):也是訊雜比,只是訊號部分的值通通改用該訊號度量的最大值。以訊號度量範圍為 0 到 255 當作例子來計算 PSNR 時,訊號部分均當成是其能夠度量的最大值,也就是 255,而不是原來的訊號。

代码实现(参考:http://cg2010studio.com/2014/12/10/opencv-snr-%E8%88%87-psnr/)

/**    Theme: SNR (Signal to Noise Ratio) & PSNR (Peak Signal to Noise Ratio)    compiler: Dev C++ 4.9.9.2    Library: OpenCV 2.0    Date: 103/12/10    Author: HappyMan    Blog: https://cg2010studio.wordpress.com/*/#include <cv.h>#include <highgui.h>#include<iostream>using namespace std;int main(){    IplImage *src1= cvLoadImage("moon_o.BMP");    IplImage *src2= cvLoadImage("moon_m.BMP");    ;    ;    double MSE = 0.0;    double SNR = 0.0;    double PSNR = 0.0;    ;    , blue2=;    , green2=;    , red2=;    // width x height -> [height][width]    ;i<src1->height;i++){        ;j<src1->widthStep;j=j+){            blue1=(int)(uchar)src1->imageData[i*src1->widthStep+j];//Blue            green1=(];//Green            red1=(];//Red            blue2=(int)(uchar)src2->imageData[i*src2->widthStep+j];//Blue            green2=(];//Green            red2=(];//Red            sigma+=(blue1-blue2)*(blue1-blue2)+            (green1-green2)*(green1-green2)+            (red1-red2)*(red1-red2);            squre += blue1*blue1 + green1*green1 + red1*red1;        }    }    MSE=sigma/(double)frameSize;    PSNR=*log10(*/MSE);    SNR = *log10(squre/sigma);    cout<<"sigma: "<<sigma<<endl;;    cout<<"MSE: "<<MSE<<endl;;    cout<<"PSNR: "<<PSNR<<endl;;    cout<<"SNR: "<<SNR<<endl;;    system("pause");    cvWaitKey();    return EXIT_SUCCESS;}