图像的 SNR 和 PSNR 的计算

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weifenglin1997/article/details/79938863
#include <cv.h>
#include <highgui.h>
#include<iostream>
 
using namespace std;

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;
    }
}
 
int main(){
    IplImage *src1= cvLoadImage("001.jpg");
    IplImage *src2= cvLoadImage("1.png");
 
    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;
}

猜你喜欢

转载自blog.csdn.net/weifenglin1997/article/details/79938863
今日推荐