您的位置:首页 > 其它

计算两个YUV视频序列的PSNR

2017-06-29 15:17 483 查看
/**
* Calculate PSNR between 2 YUV420P file
* @param url1     Location of first Input YUV file.
* @param url2     Location of another Input YUV file.
* @param w        Width of Input YUV file.
* @param h        Height of Input YUV file.
* @param num      Number of frames to process.
*/
#include<iostream>
using namespace std;
int simplest_yuv420_psnr(char *url1,char *url2,int w,int h,int num){
FILE *fp1=fopen(url1,"rb+");
FILE *fp2=fopen(url2,"rb+");
FILE *fp=fopen("Video_PSNR.txt","w");         //创建存取PSNR值信息的txt文件

unsigned char *pic1=(unsigned char *)malloc(w*h);
unsigned char *pic2=(unsigned char *)malloc(w*h);

float Sum_PSNR=0;    //定义所有帧的PSNR和
float average_PSNR;  //定义平均PSNR
for(int i=0;i<num;i++){
fread(pic1,1,w*h,fp1);
fread(pic2,1,w*h,fp2);
double mse_sum=0,mse=0,psnr=0;
for(int j=0;j<w*h;j++){
mse_sum+=pow((double)(pic1[j]-pic2[j]),2);
}
mse=mse_sum/(w*h);
psnr=10*log10(255.0*255.0/mse);
printf("the %d frame PSNR is %5.3f\n",i,psnr);
fprintf(fp,"the %d frame PSNR is %5.3f\n",i,psnr);      //存取每一帧的PSNR值到txt
Sum_PSNR+=psnr;
fseek(fp1,w*h/2,SEEK_CUR);
fseek(fp2,w*h/2,SEEK_CUR);

}
average_PSNR=Sum_PSNR/num;
printf("the average PSNR is %5.3f\n",average_PSNR);
fprintf(fp,"the average PSNR is %5.3f\n",average_PSNR);     //存取所有帧的平均PSNR值到txt
free(pic1);
free(pic2);
fclose(fp1);
fclose(fp2);
return 0;
}

int main()
{
//参数分别为(原始视频序列,压缩后视频序列,宽,高,帧数);
simplest_yuv420_psnr("BasketballDrill_832x480_50.yuv","BQMall_832x480_60.yuv",832,480,5);
//注意视频序列所在路径,默认的是跟当前工程在一个目录下,当然也可以自己指定
system("pause");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息