图像的 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

    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());
        double psnr = 10.0*log10((255*255)/mse);
        return psnr;
    }
}

 

SNR 不太好搜。

http://cg2010studio.com/2014/12/10/opencv-snr-%E8%88%87-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");
 
    long long int sigma = 0;
    long long int squre = 0;
    double MSE = 0.0;
    double SNR = 0.0;
    double PSNR = 0.0;
    int frameSize = src1->height*src1->width*3;
    int blue1=0, blue2=0;
    int green1=0, green2=0;
    int red1=0, red2=0;
 
    // width x height -> [height][width]
    for(int i=0;i<src1->height;i++){
        for(int j=0;j<src1->widthStep;j=j+3){
            blue1=(int)(uchar)src1->imageData[i*src1->widthStep+j];//Blue
            green1=(int)(uchar)src1->imageData[i*src1->widthStep+j+1];//Green
            red1=(int)(uchar)src1->imageData[i*src1->widthStep+j+2];//Red
            blue2=(int)(uchar)src2->imageData[i*src2->widthStep+j];//Blue
            green2=(int)(uchar)src2->imageData[i*src2->widthStep+j+1];//Green
            red2=(int)(uchar)src2->imageData[i*src2->widthStep+j+2];//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=10*log10(255*255/MSE);
    SNR = 10*log10(squre/sigma);
 
    cout<<"sigma: "<<sigma<<endl;;
    cout<<"MSE: "<<MSE<<endl;;
    cout<<"PSNR: "<<PSNR<<endl;;
    cout<<"SNR: "<<SNR<<endl;;
 
    system("pause");
    cvWaitKey(0);
    return EXIT_SUCCESS;
}

 

posted @ 2016-03-31 10:39  rldts  阅读(47089)  评论(0编辑  收藏  举报