图像评价方法程序-PSNR

本文主要展示图像的有参考评价方式PSNR的程序部分,其数学原理部分参考这片文章

以下为主程序部分:

#include "common.h"

int main() {
    
    

    Mat img1 = imread("图片地址");

    Mat img2 = imread("图片地址");


    getPSNR(img1, img2);

    return 0;
}

以下部分是PSNR函数部分:

#include "common.h"



void getPSNR(const Mat& img1, const Mat& img2)
{
    
    
    Mat temp;
    absdiff(img1, img2, temp);       // |I1 - I2|AbsDiff函数是 OpenCV 中计算两个数组差的绝对值的函数
    temp.convertTo(temp, CV_32F);  // 这里我们使用的CV_32F来计算,因为8位无符号char是不能进行平方计算
    temp = temp.mul(temp);           // |I1 - I2|^2

    Scalar temp_sum = sum(temp);         //对每一个通道进行加和

    double sse = temp_sum.val[0] + temp_sum.val[1] + temp_sum.val[2]; // sum channels

    if( sse <= 1e-10) // 对于非常小的值我们将约等于0
        cout << "两张图像一样" <<endl;

    else
    {
    
    
        double  MSE =sse /(double)(img1.channels() * img1.total());//计算MSE

        cout << "MSE: " << MSE << endl;

        double PSNR = 10.0*log10((255*255)/MSE);

        cout << "PSNR:" << PSNR<< endl;
    }
}

以下是头文件:

#include <opencv2/opencv.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>


using namespace cv;
using namespace std;

void getPSNR(const Mat& img1, const Mat& img2);

猜你喜欢

转载自blog.csdn.net/weixin_45718019/article/details/117822743
今日推荐