PSNR&SSIM

全参考视频质量评价方法

常用的方法有两种
1.PSNR(Peak Signal to Noise Ratio)峰值信噪比:一种评价图像的客观标准。
2.SSIM(structural similarity index),结构相似性,是一种衡量两幅图像相似度的指标。SSIM使用的两张图像中,一张为未经压缩的无失真图像,另一张为失真后的图像。

PSNR_ C语言实现:
void PSNR_compute()
{
 int width  = //宽;
  int height = //高;

  uint8_t* p = (uint8_t*)malloc(width*height*3/2);
  if (p == NULL) {
    return;
  }

  size_t toread = width*height*3/2;
  if (fread(p,1,toread,reference_file) != toread) {
    free(p);
    return;
  }

  const uint8_t* yptr  = //y分量;
  const uint8_t* cbptr = //cb分量;
  const uint8_t* crptr = //cr分量;

  int stride = Y的stride.
  int cstride = U或者V的Stride;

  double img_mse_y  = MSE( yptr,  stride, p, width,   width, height);
  double img_mse_cb = MSE(cbptr, cstride, p+width*height,      width/2, width/2,height/2);
  double img_mse_cr = MSE(crptr, cstride, p+width*height*5/4,  width/2, width/2,height/2);

  mse_frames++;

  mse_y  += img_mse_y;
  mse_cb += img_mse_cb;
  mse_cr += img_mse_cr;

  printf("%5d %6f %6f %6f %6f\n",
         framecnt,
         PSNR(img_mse_y), PSNR(img_mse_cb), PSNR(img_mse_cr),
         ssimSum);

  free(p);
}

double MSE(const uint8_t* img, int imgStride,
           const uint8_t* ref, int refStride,
           int width, int height)
{
  double sum=0.0;

  const uint8_t* iPtr = img;
  const uint8_t* rPtr = ref;

  for (int y=0;y<height;y++) {
    uint32_t lineSum=0;

    for (int x=0;x<width;x++) {
      int diff = iPtr[x] - rPtr[x];
      lineSum += diff*diff;
    }

    sum += ((double)lineSum)/width;

    iPtr += imgStride;
    rPtr += refStride;
  }

  return sum/height;
}
double PSNR(double mse)
{
  if (mse==0) { return 99.99999; }

  return 10*log10(255.0*255.0/mse);
}

void main () {

  while () {
     //计算所有。
     PSNR_compute()
  }
    //最终结果
    printf("#total  %6f %6f %6f %6f\n",
           PSNR(mse_y /mse_frames),
           PSNR(mse_cb/mse_frames),
           PSNR(mse_cr/mse_frames),
           ssim_y/ssim_frames);
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容