实验原理
实验步骤
首先读取一个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);
}
结果分析
birds | camman | clown | fruit | lena | noise | odie | zone | 4 | 24 | |
原文件大小(kb) | 393 | 66 | 66 | 66 | 66 | 66 | 66 | 66 | 415 | 415 |
压缩后文件大小(kb) | 355 | 54 | 63 | 62 | 62 | 56 | 10 | 52 | 52 | 291 |
压缩比 | 1.11 | 1.22 | 1.05 | 1.06 | 1.06 | 1.18 | 6.6 | 1.27 | 7.98 | 1.43 |
量化预测误差文件大小(kb) | 393 | 66 | 66 | 66 | 66 | 66 | 66 | 66 | 415 | 415 |
压缩后量化预测误差文件(kb) | 150 | 32 | 39 | 34 | 38 | 63 | 9 | 62 | 54 | 136 |
压缩比 | 2.62 | 2.06 | 1.69 | 1.94 | 1.74 | 1.05 | 7.33 | 1.06 | 7.68 | 3.05 |