/**
* 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");
}
计算两个YUV视频序列的PSNR
最新推荐文章于 2022-08-21 18:25:03 发布