当前位置:   article > 正文

视频质量评价PSNR_视频质量 np

视频质量 np
        PSNR(Peak Signal to Noise Ratio,峰值信噪比)是最基础的视频质量评价方法。它的取值一般在20-50之间,值越大代表受损图片越接近原图片。PSNR通过对原始图像和失真图像进行像素的逐点对比,计算两幅图像像素点之间的误差,并由这些误差最终确定失真图像的质量评分。该方法由于计算简便、数学意义明确,在图像处理领域中应用最为广泛。

一幅MxN尺寸的图像的PSNR的计算公式如下所示:


其中xij 和yij 分别表示失真图像和原始图像对应像素点的灰度值;

i,j 分别代表图像的行和列;

L 是图像灰度值可到达的动态范围,8位的灰度图像的L=2^8-1=255。

如果已知SSD,MxN尺寸图像的PSNR公式如下所示。

MSE=SSD*1/(M*N)

PSNR=10*lg(255^2/MSE)

    例如下图两张1080图片(左边是原图,右边是编码之后的图片,QP为17)的PSNR对比的结果 Y PSNR is 40.632022, U PSNR is 44.596545,V PSNR is 45.759277


PSNR对比测试:
1080P视频测试:BasketballDrive_1920x1080_25_250.yuv
dp:17
 Y PSNR is 40.632022, U PSNR is 44.596545,V PSNR is 45.759277
dp:28
 Y PSNR is 38.834869, U PSNR is 42.445172,V PSNR is 42.977148
dp:32
 Y PSNR is 38.144906, U PSNR is 41.265455,V PSNR is 41.705576
dp:36
 Y PSNR is 37.527187, U PSNR is 40.110563,V PSNR is 40.551498
        我们知道量化和反量化过程中,量化步长QP决定量化器的编码压缩率及图像精度。如果QP比较大,则量化值FQ动态范围越小,其相应的编码长度越小,但反量化是损失较多的图像细节,导致PSNR值越小。

代码:

  1. // PSNR_CAL.cpp : 定义控制台应用程序的入口点。
  2. //PSNR (Peak Signal to Noise Ratio)
  3. //峰值信噪比PSNR衡量图像失真或是噪声水平的客观标准。2个图像之间PSNR值越大,则越相似。普遍基准为30dB,30dB以下的图像劣化较为明显。
  4. #include "stdafx.h"
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <time.h>
  8. #include <math.h>
  9. #define VIDEO_WIDTH 1280
  10. #define VIDEO_HEIGHT 720
  11. #define VIDEO_FRAME_NUM 10 //frame number
  12. //编码解码YUV
  13. #define DEC_YUV_PATH "E:\\yuv\\\\bs3.yuv"
  14. //原YUV
  15. #define REF_YUV_PATH "E:\\yuv\\BasketballDrive_1920x1080_25_250.yuv"
  16. #define REF_YUV_422 0 //1:reference yuv is 422 format, 0:reference yuv is 420 format
  17. #define VIDEO_SIZE_Y VIDEO_HEIGHT*VIDEO_WIDTH
  18. #define VIDEO_SIZE_UV (VIDEO_HEIGHT*VIDEO_WIDTH)>>1
  19. #define VIDEO_SIZE_YUV (VIDEO_SIZE_Y + VIDEO_SIZE_UV)
  20. #define CONV422 0
  21. #define CAL_PSNR 1
  22. int main()
  23. {
  24. FILE *fp_dec;
  25. FILE *fp_ref;
  26. int i, j, k, comp;
  27. #if CAL_PSNR
  28. unsigned char line_dec[5000];
  29. unsigned char line_ref[5000];
  30. int idiff;
  31. unsigned long diff_sum;
  32. int width, height;
  33. double psnr_frame;
  34. double psnr_sum[VIDEO_FRAME_NUM][3];
  35. double psnr_total[3];
  36. fp_dec = fopen(DEC_YUV_PATH, "rb");
  37. fp_ref = fopen(REF_YUV_PATH, "rb");
  38. if (fp_dec == NULL)
  39. {
  40. printf("\n DEC YUV file not found\n");
  41. return 0;
  42. }
  43. if (fp_ref == NULL)
  44. {
  45. printf("\n REF YUV file not found\n");
  46. return 0;
  47. }
  48. for (i = 0; i < VIDEO_FRAME_NUM; i++)
  49. { //Y
  50. for (comp = 0; comp < 3; comp++)
  51. {
  52. diff_sum = 0;
  53. if(comp ==0)
  54. {
  55. width = VIDEO_WIDTH;
  56. height = VIDEO_HEIGHT;
  57. }
  58. else
  59. {
  60. width = VIDEO_WIDTH /2;
  61. height = VIDEO_HEIGHT /2;
  62. }
  63. for (j = 0; j < height; j++)
  64. {
  65. fread(line_dec, 1, width, fp_dec);
  66. fread(line_ref, 1, width, fp_ref);
  67. //fwrite(line_ref, 1, width, fp_ref_422);
  68. //if(comp != 0) //UV
  69. // fwrite(line_ref, 1, width, fp_ref_422);
  70. for (k = 0; k < width; k++)
  71. {
  72. idiff = (int)(line_dec[k] - line_ref[k]);
  73. diff_sum += idiff*idiff;
  74. //if (k == 0 && j == 5 )
  75. // printf("stop at %d", k);
  76. }
  77. //if (comp != 0 && REF_YUV_422 == 1) // if 422 format, skip one chroma line
  78. // fread(line_dec, 1, width, fp_dec);
  79. }
  80. psnr_frame = (double)255 * 255 * width* height;
  81. psnr_sum[i][comp] = 10.0 * log10(psnr_frame / (double)diff_sum);
  82. }
  83. printf("frame %d, Y PSNR is %f, Cb PSNR is %f,Cr PSNR is %f \n", i, psnr_sum[i][0], psnr_sum[i][1], psnr_sum[i][2]);
  84. }
  85. psnr_total[0] = 0;
  86. psnr_total[1] = 0;
  87. psnr_total[2] = 0;
  88. for (i = 0; i<VIDEO_FRAME_NUM; i++)
  89. {
  90. psnr_total[0] += psnr_sum[i][0];
  91. psnr_total[1] += psnr_sum[i][1];
  92. psnr_total[2] += psnr_sum[i][2];
  93. }
  94. psnr_total[0] = psnr_total[0] / (VIDEO_FRAME_NUM);
  95. psnr_total[1] = psnr_total[1] / (VIDEO_FRAME_NUM);
  96. psnr_total[2] = psnr_total[2] / (VIDEO_FRAME_NUM);
  97. printf("average PSNR of sequence is:\n Y PSNR is %f, U PSNR is %f,V PSNR is %f\n", psnr_total[0], psnr_total[1], psnr_total[2]);
  98. fclose(fp_dec);
  99. fclose(fp_ref);
  100. system("pause");
  101. #endif
  102. #if CONV422
  103. //convert ref 4:2:0 yuv to interleave 4:2:2 yuv
  104. unsigned char Y_plane[VIDEO_SIZE_YUV];
  105. unsigned char U_plane[VIDEO_SIZE_Y / 4];
  106. unsigned char V_plane[VIDEO_SIZE_Y / 4];
  107. unsigned char YUV_plane[2 * VIDEO_SIZE_Y];
  108. unsigned char *pt0;
  109. unsigned char *pt1;
  110. unsigned char *pt2;
  111. unsigned char *pt3;
  112. fp_ref = fopen(REF_YUV_PATH, "rb");
  113. fp_ref_422 = fopen(REF_YUV422_ITLV_PATH, "wb");
  114. if (fp_ref == NULL)
  115. {
  116. printf("\n REF YUV file not found\n");
  117. return 0;
  118. }
  119. if (fp_ref_422 == NULL)
  120. {
  121. printf("\n REF 422 YUV file not found\n");
  122. return 0;
  123. }
  124. for (i = 0; i < VIDEO_FRAME_NUM_CONV; i++)
  125. {
  126. fread(Y_plane, 1, VIDEO_SIZE_Y, fp_ref);
  127. fread(U_plane, 1, VIDEO_SIZE_Y / 4, fp_ref);
  128. fread(V_plane, 1, VIDEO_SIZE_Y / 4, fp_ref);
  129. pt0 = Y_plane;
  130. pt1 = U_plane;
  131. pt2 = V_plane;
  132. pt3 = YUV_plane;
  133. for (j = 0; j < VIDEO_HEIGHT / 2; j++)
  134. {
  135. for (k = 0; k < VIDEO_WIDTH / 2; k++) //the row size of YUV_plane is 2*VIDEO_WIDTH
  136. {
  137. *pt3++ = *pt0++; //Y0
  138. *pt3++ = *pt1++; //U0
  139. *pt3++ = *pt0++; //Y1
  140. *pt3++ = *pt2++; //V0
  141. }
  142. pt1 = pt1 - VIDEO_WIDTH / 2;
  143. pt2 = pt2 - VIDEO_WIDTH / 2;
  144. for (k = 0; k < VIDEO_WIDTH / 2; k++) //the row size of YUV_plane is 2*VIDEO_WIDTH
  145. {
  146. *pt3++ = *pt0++; //Y(WIDTH+0)
  147. *pt3++ = *pt1++; //U0
  148. *pt3++ = *pt0++; //Y(WIDTH+1)
  149. *pt3++ = *pt2++; //V0
  150. }
  151. }
  152. fwrite(YUV_plane, 1, 2* VIDEO_SIZE_Y, fp_ref_422);
  153. }
  154. #endif
  155. }

       但是PSNR仅仅计算了图像像素点间的绝对误差,没有考虑像素点间的视觉相关性,更没顾及人类视觉系统的感知特性,所以其评价结果与主观感受往往相差较大(SSIM就是一种典型的与人类视觉系统特性结合的质量评价方法)。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/462141
推荐阅读
相关标签
  

闽ICP备14008679号