PSNR的计算

/*
程序说明:
计算两幅图像的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)));
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值