欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

图像的 SNR 和 PSNR 的计算

程序员文章站 2023-12-31 19:20:16
...
#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;
}

上一篇:

下一篇: