signed

QiShunwang

“诚信为本、客户至上”

DPCM差分预测编码

2021/6/3 14:52:46   来源:

数据压缩实验四

实验目的:

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

实验原理:

1.DPCM

DPCM是差分预测编码调制,是比较典型的预测编码系统。在DPCM系统中,需要注意的是预测器的输入是已经解码以后的样本。之所以不用原始样本来做预测,是因为在解码端无法得到原始样本,只能得到存在误差的样本。因此,在DPCM编码器中实际内嵌了一个解码器,如编码器中虚线框中所示。
在这里插入图片描述
在这里插入图片描述

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

2.PSNR

PSNR是峰值信噪比,是一种度量图像失真的方式,与图像质量近似成正比关系。本实验使用PSNR作为图像质量评价的指标。
在这里插入图片描述

实验程序:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<math.h>
#include<iostream>
using namespace std;

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:/wsfw/sjys/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<width*height;i++)
	{
		float err;
		if(i%width==0)
		{
			err=float(y_buffer[i])-128;
			err_buffer[i]=(unsigned char)(err/2+128);
			re_y_buffer[i]=(unsigned char)(128+(err_buffer[i]-128)*2);
		}
		else
		{
			err=float(y_buffer[i]-re_y_buffer[i-1]);
			err_buffer[i]=(unsigned char)(err/2+128);
			re_y_buffer[i]=(unsigned char)(re_y_buffer[i-1]+(err_buffer[i]-128)*2);
		}
	}


	FILE *err;
	err=fopen("C:/wsfw/sjys/dpcm/dpcm/err.txt","wb");
	double frequency_[256]={0};
	GetFrequency(err_buffer,frequency_,height,width);
	for(int i=0;i<256;i++)
	{
		fprintf(err,"%d\t%f\n",i,frequency_[i]);
	}

	//计算PSNR
	cout<<"psnr="<<PSNR(y_buffer,re_y_buffer,height,width,8)<<endl;


	re_y_file=fopen(re_y_filename,"wb");
	err_file=fopen(err_filename,"wb");
	fwrite(re_y_buffer,width*height,1,re_y_file);
	fwrite(u_buffer,width*height/4,1,re_y_file);
	fwrite(v_buffer,width*height/4,1,re_y_file);
	fwrite(err_buffer,width*height,1,err_file);
	fwrite(u_buffer,width*height/4,1,err_file);
	fwrite(v_buffer,width*height/4,1,err_file);
	fclose(re_y_file);
	fclose(err_file);

}

实验结果:

在这里插入图片描述

在这里插入图片描述