一.实验目的
掌握DPCM编解码系统的基本原理。初步掌握实验用C/C++/Python等语言编程实现DPCM编码器,并分析其压缩效率。
二.实验原理
DPCM是差分预测编码调制的缩写,是比较典型的预测编码系统。在DPCM系统中,需要注意的是预测器的输入是已经解码以后的样本。之所以不用原始样本来做预测,是因为在解码端无法得到原始样本,只能得到存在误差的样本。因此,DPCM编码器中实际内嵌了一个解码器,如编码器中虚线框中所示。
可以由一个具体例子来进行解释说明。
三.实验所需结果
在DPCM编码器实现的过程中可同时输出预测误差图像和重建图像。将预测误差图像写入文件并将该文件输入Huffman编码器,得到输出码流、给出概率分布图并计算压缩比。将原始图像文件输入Huffman编码器,得到输出码流、给出概率分布图并计算压缩比。最后比较两种系统(1.DPCM+熵编码和2.仅进行熵编码)之间的编码效率(压缩比和图像质量)。压缩质量以PSNR进行计算。
四.PSNR(峰值信噪比)
对图片进行压缩后,会对图片进行一定的压缩损失,我们可以以PSNR作为比较损失的指标。**峰值信噪比与图像质量近似成正比关系。**计算公式如下。
五.实验代码
1.DPCM实现编码
由于原始YUV图像,我们只计算其Y值。Y值的取值范围为0~255,故预测误差Pn的取值范围为-255~255。8bit量化,将Pn除2可将Pn的范围变成-128~127,再整体加上128作为补偿,将范围变成0~255,刚好可以用8bit表示。
因为对于每行第一个像素无法进行预测估计,所以要将每行第一个像素值直接传入重建文件中。
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
void DPCM(short int height, short int width, unsigned char* yuvBuf, unsigned char* qBuf, unsigned char* reBuf)
{
int size = width * height;//256*256
for (int i = 0; i < size; i++) {
if (i % width == 0) { //判断是否是每行第一个像素,上方加粗字说明
qBuf[i] = yuvBuf[i];//误差值
reBuf[i] = yuvBuf[i];//重建值
}
else {
int er = yuvBuf[i] - reBuf[i - 1];//计算差值的范围是-255 — 255
er >>= 1;//-127 - 127
int temp = er + 128;//进行补偿将范围转化到0 - 255,可以用8bit量化
qBuf[i] = temp;
reBuf[i] = qBuf[i] * 2 + reBuf[i - 1];//计算重建值
}
}
}
2.PSNR的计算
#include<Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include<math.h>
double PSNR(unsigned char* standard, unsigned char* image, int height, int width, int bitNum)
{
double psnr , MSE ;
for (int i = 0; i < height * width; i++)
{
MSE += (long long)((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;
}
最后所得PSNR约为51dB
3.main.cpp
#include<Windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include "DPCM.h"
#include "PSNR.h"
int main(int argc, char** argv)
{
unsigned int Width ;
unsigned int Height ;
unsigned int i;
double psnr;
char* yuvFileName = NULL;
char* reFileName = NULL;
char* qFileName = NULL;
FILE* yuvFile = NULL;
FILE* reFile = NULL;
FILE* qFile = NULL;
unsigned char* fillBuf = NULL;
unsigned char* yBuf = NULL;
unsigned char* reBuf = NULL;
unsigned char* qBuf = NULL;
unsigned int videoFramesWritten = 0;
yuvFileName = argv[1];
qFileName = argv[2];
reFileName = argv[3];
Width = atoi(argv[4]);
Height = atoi(argv[5]);//atoi字符串转为数字
yuvFile = fopen(yuvFileName, "rb");
if (yuvFile == NULL)
{
printf("error\n");
exit(1);
}
else
{
printf("Open file\n", yuvFileName);
}
qFile = fopen(qFileName, "wb");
reFile = fopen(reFileName, "wb");
qBuf = (unsigned char*)malloc(Width * Height);
reBuf = (unsigned char*)malloc(Width * Height);
yBuf = (unsigned char*)malloc(Width * Height);
fillBuf = (unsigned char*)malloc(Width * Height / 2);
fread(yBuf, 1, Width * Height, yuvFile);
DPCM(Height, Width, yBuf, qBuf, reBuf);
for (int i = 0; i < Width * Height / 2; i++)
{
fillBuf[i] = 128;
}
fwrite(qBuf, 1, Width * Height, qFile);
fwrite(fillBuf, 1, Width * Height / 2, qFile);
fwrite(reBuf, 1, Width * Height, reFile);
fwrite(fillBuf, 1, Width * Height / 2, reFile);
psnr = PSNR(yBuf, reBuf, Height, Width, 8);
free(yBuf);
free(qBuf);
free(reBuf);
free(fillBuf);
fclose(qFile);
fclose(reFile);
fclose(yuvFile);
return 0;
}
六、实验结果
七.实验分析
原图像的概率分布图
残差的概率分布图
在CMD中输入
得到如下内容
对于残差文件进行熵编码
对于源文件直接进行熵编码
DPCM后的熵编码 | 直接进行熵编码 | |
---|---|---|
原图大小 | 96KB | 96KB |
压缩后大小 | 45.7 | 68.2KB |
压缩比 | 47.6% | 71% |