• 计算两个YUV420P像素数据的PSNR---高等算法


    PSNR是最基本的视频质量评价方法。本程序中的函数可以对比两张YUV图片中亮度分量Y的PSNR。函数的代码如下所示。

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. /** 
    2.  * Calculate PSNR between 2 YUV420P file 
    3.  * @param url1     Location of first Input YUV file. 
    4.  * @param url2     Location of another Input YUV file. 
    5.  * @param w        Width of Input YUV file. 
    6.  * @param h        Height of Input YUV file. 
    7.  * @param num      Number of frames to process. 
    8.  */  
    9. int simplest_yuv420_psnr(char *url1,char *url2,int w,int h,int num){  
    10.     FILE *fp1=fopen(url1,"rb+");  
    11.     FILE *fp2=fopen(url2,"rb+");  
    12.     unsigned char *pic1=(unsigned char *)malloc(w*h);  
    13.     unsigned char *pic2=(unsigned char *)malloc(w*h);  
    14.   
    15.     for(int i=0;i<num;i++){  
    16.         fread(pic1,1,w*h,fp1);  
    17.         fread(pic2,1,w*h,fp2);  
    18.   
    19.         double mse_sum=0,mse=0,psnr=0;  
    20.         for(int j=0;j<w*h;j++){  
    21.             mse_sum+=pow((double)(pic1[j]-pic2[j]),2);  
    22.         }  
    23.         mse=mse_sum/(w*h);  
    24.         psnr=10*log10(255.0*255.0/mse);  
    25.         printf("%5.3f ",psnr);  
    26.   
    27.         fseek(fp1,w*h/2,SEEK_CUR);  
    28.         fseek(fp2,w*h/2,SEEK_CUR);  
    29.   
    30.     }  
    31.   
    32.     free(pic1);  
    33.     free(pic2);  
    34.     fclose(fp1);  
    35.     fclose(fp2);  
    36.     return 0;  
    37. }  


    调用上面函数的方法如下所示。

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. simplest_yuv420_psnr("lena_256x256_yuv420p.yuv","lena_distort_256x256_yuv420p.yuv",256,256,1);  


    对于8bit量化的像素数据来说,PSNR的计算公式如下所示。

    上述公式中mse的计算公式如下所示。

    其中M,N分别为图像的宽高,xij和yij分别为两张图像的每一个像素值。PSNR通常用于质量评价,就是计算受损图像与原始图像之间的差别,以此来评价受损图像的质量。本程序输入的两张图像的对比图如下图所示。其中左边的图像为原始图像,右边的图像为受损图像。

    经过程序计算后得到的PSNR取值为26.693。PSNR取值通常情况下都在20-50的范围内,取值越高,代表两张图像越接近,反映出受损图像质量越好。

  • 相关阅读:
    产品经理的十宗罪,你犯了几宗?
    产品经理的10大顾虑
    【FastAPI 学习 七】GET和POST请求参数接收以及验证
    【FastAPI 学习 六】异常处理
    【FastAPI 学习 五】统一响应json数据格式
    前端展示(三)
    前端展示(二)
    前端设计(一)
    后端流程分析
    生成词云图
  • 原文地址:https://www.cnblogs.com/alexhg/p/6565117.html
Copyright © 2020-2023  润新知