/* 程序说明: 计算两幅图像的SNR值,两幅图像需要位于项目的根目录下面,输入文件名 加后缀名 2015年3月27日 by sea */ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <memory.h> #include <string.h> //#define INPUT1 "C:\\Users\\Administrator\\Desktop\\test psnr\\原图.yuv" //#define INPUT2 "C:\\Users\\Administrator\\Desktop\\test psnr\\原图第16帧.yuv" //#define VIDEOMODE CIF #define uint8_t unsigned char char INPUT1[256],INPUT2[256],VIDEOMODE[256]; float psnr(uint8_t* p1, uint8_t* p2, int size); main(int argc, char* argv[]) { FILE* in_file1, *in_file2; int frame_no; unsigned char * buf1, *buf2; int size, luma_size, chroma_size, tt; int height,width; float y = 0.0, u = 0.0, v = 0.0; char t[256]; printf("please input the first filename:\n"); scanf("%s",INPUT1); printf("please input the second filename:\n"); scanf("%s",INPUT2); printf("please input the yuv file mode:\n"); scanf("%s",VIDEOMODE); sprintf(t,"CIF"); tt = strcmp(VIDEOMODE,t); if(tt == 0) { height = 288; width = 352; } else { height = 640; width = 480; } size = height * width + (height * width >> 1); luma_size = height * width; chroma_size = height * width / 4; buf1 = (unsigned char *)malloc(size); buf2 = (unsigned char *)malloc(size); in_file1 = fopen(INPUT1, "rb"); if (!in_file1) { printf("cannot open input file."); return 0; } in_file2 = fopen(INPUT2, "rb"); if (!in_file2) { printf("cannot open input file."); return 0; } for(frame_no = 0; frame_no < 100; frame_no++ ) { if (fread(buf1, size, 1, in_file1) <= 0) { printf("cannot read from input file."); goto END; } if (fread(buf2, size, 1, in_file2) <= 0) { printf("cannot read from input file."); goto END; } y += psnr(buf1, buf2, luma_size); u += psnr(buf1 + luma_size, buf2 + luma_size, chroma_size); v += psnr(buf1 + luma_size + chroma_size, buf2 + luma_size + chroma_size, chroma_size); } END: printf("total frames: %d \n", frame_no); printf(" The luma psnr is: %.4f \n", y/frame_no); printf(" The chroma_u psnr is: %.4f \n", u/frame_no); printf(" The chroma_v psnr is: %.4f \n", v/frame_no); fclose(in_file1); fclose(in_file2); free(buf1); free(buf2); return 0; } float psnr(uint8_t* p1, uint8_t* p2, int size) { float sad = 0; int i; for (i = 0 ; i < size ; i ++) { int tmp; tmp = (p1[i] - p2[i]); sad += tmp * tmp; } return (float)(10 * log10(65025.0f * size / (sad + 1))); }