計算兩個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\n",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的范圍內,取值越高,代表兩張圖像越接近,反映出受損圖像質量越好。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM