数据压缩实验四

实验原理

实验步骤

首先读取一个256级的灰度图像

采用自己设定的预测方法计算预测误差

并对预测误差进行8比特均匀量化再进行反量化得到重建图像

对得出的重建图像进行Huffman编码,计算压缩比

关键代码及分析

。。。
//	open bmp & yuv file	
		if ((bmpFile = fopen(argv[1], "rb")) == NULL)  //一帧
		{
			printf("bmp file open failed!");
			exit(0);
		}
		if ((yuvFile = fopen(argv[2], "wb")) == NULL)  
		{                                               
			printf("yuv file failed!");
			exit(0);
		}	
			fwrite(yBuff, 1, width * height, yuvFile);//只提取y分量
	//		fwrite(uBuff, 1, (width * height) / 4, yuvFile);
	//		fwrite(vBuff, 1, (width * height) / 4, yuvFile);	
		//DPCM
		FILE* lhFile = NULL;
		FILE* cjFile = NULL;
		unsigned char* lhBuff = NULL;  //量化
		unsigned char* cjBuff = NULL; //重建
		if ((cjFile = fopen(argv[3], "wb+")) == NULL) 
		{
			printf("yuv chongjian file failed!");
			exit(0);
		}
		if ((lhFile = fopen(argv[4], "wb+")) == NULL)  
		{
			printf("yuv lianghua file failed!");
			exit(0);
		}
		
		lhBuff = (unsigned char *)malloc(height*width);   //开辟空间
		cjBuff = (unsigned char *)malloc(height*width);
		int a = 0;//向左预测
		for (int i = 0; i<height; i++) 
		{
			for (j = 0; j<width; j++)
			{
				if (j==0)  //第一列
				{//第一列先对128进行预测,8bit量化除2,而后用灰度图像输出
					a = (*(yBuff + i*width + j) - 128) / 2 + 128;
					*(lhBuff + i*width + j) = (unsigned char)a; 
					k = ((*(lhBuff + i*width + j )-128) * 2) + 128; //反量化
					*(cjBuff + i*width + j) = (unsigned char)a; 
				}
				else
				{    //对左侧的图像重建值进行预测
					a = (*(yBuff + i*width + j) - *(cjBuff + i*width + j - 1)) / 2 + 128;
					*(lhBuff + i*width + j) = (unsigned char)a;
					a = ((*(lhBuff + i*width + j )-128) * 2) + *(cjBuff + i*width + j - 1);//反量化之后与上一个重建值相加
					*(cjBuff + i*width + j) = (unsigned char)a;
				}
			}
		}
		fwrite(cjBuff, 1, width * height, cjFile);  //输出
		fwrite(lhBuff, 1, width * height, lhFile);
}



结果分析

 birdscammanclownfruitlenanoiseodiezone424
原文件大小(kb)39366666666666666415415
压缩后文件大小(kb)3555463626256105252291
压缩比1.111.221.051.061.061.186.61.277.981.43
量化预测误差文件大小(kb)39366666666666666415415
压缩后量化预测误差文件(kb)150323934386396254136
压缩比2.622.061.691.941.741.057.331.067.683.05
一般情况下,通过量化预测之后的文件压缩比更大,压缩效果更好,根据柱状图也能看出来,概率更加集中,更容易压缩。























  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值