数据压缩实验四—DPCM编码

1 实验目的

掌握DPCM编解码系统的基本原理。初步掌握实验用C/C++/Python等语言编程实现DPCM编码器,并分析其压缩效率。

2 实验原理

2.1 DPCM

DPCM:差分预测编码调制
数据压缩实验四—DPCM编码_第1张图片

输入图像与上一个图像的预测值做差,将差值进行编码。
编码后的差值有两个去向,一是直接输出,二是通过解码器解出差值,再与上一帧的预测值相加,就得到了当前图像的预测值,为下一帧图像到来时做好准备。

2.2 PSNR

PSNR:峰值信噪比
数据压缩实验四—DPCM编码_第2张图片

3 实验内容

3.1 代码

#include
#include
#include
#include
#include
using namespace std;


//计算PSNR
double PSNR( unsigned char* standard, unsigned char* image,int height,int width,int bitNum )
{
    double psnr = 0, MSE = 0;
    for( int i = 0; i < height * width; i++ ) 
	{
		MSE += 1ll* (image[i] - standard[i]) * (image[i] - standard[i]);
	}
    MSE /= height * width;
    long long t = 1 << bitNum; t -= 1; t *= t;
    psnr = 10 * log10( t / MSE);
    return psnr;
}
//得到频率分布
void GetFrequency( unsigned char* buffer, double* frequency,int height,int width )
{
    int length = height * width;
    for( int i = 0; i < length; i++ ) frequency[buffer[i]] ++;
    for( int i = 0; i < 256; i++ ) frequency[i] /= length;
}


int main(int argc,char** argv)
{
	char *yuvfilename=NULL;
	char *re_y_filename=NULL;
	char *err_filename=NULL;
	FILE* yuvfile=NULL;
	FILE* re_y_file=NULL;
	FILE* err_file=NULL;
	int width,height;
	yuvfilename=argv[1];
	re_y_filename=argv[2];
	err_filename=argv[3];
	width=atoi(argv[4]);
	height=atoi(argv[5]);
	unsigned char* u_buffer=NULL;
	unsigned char* v_buffer=NULL;
	unsigned char* y_buffer=NULL;//原始图像
	unsigned char* re_y_buffer=NULL;//重建值
	unsigned char* err_buffer=NULL;//预测误差

	yuvfile=fopen(yuvfilename,"rb");
	if(yuvfile==0)
	{
		printf("cannot find yuv file.\n");
	}
	else
	{
		printf("open yuv file successfully!\n");
	}

	y_buffer=(unsigned char*)malloc(width*height);
	u_buffer=(unsigned char*)malloc(width*height/4);
	v_buffer=(unsigned char*)malloc(width*height/4);
	err_buffer=(unsigned char*)malloc(width*height*1.5);
	re_y_buffer=(unsigned char*)malloc(width*height*1.5);
	
	fread(y_buffer,1,width*height,yuvfile);
	fread(u_buffer,1,width*height/4,yuvfile);
	fread(v_buffer,1,width*height/4,yuvfile);

	//得到原始图像的概率分布
	FILE *orig;
	orig=fopen("C:/Users/15643/Desktop/DPCM/DPCM/DPCM/orig.txt","wb");
	double frequency[256]={0};
	GetFrequency(y_buffer,frequency,height,width);
	for(int i=0;i<256;i++)
	{
		fprintf(orig,"%d\t%f\n",i,frequency[i]);
	}
	
	//DPCM编码
	for(int i=0;i

3.2 后续工作

你可能感兴趣的:(数据压缩实验四—DPCM编码)