目录
一、DPCM系统
1.DPCM编解码原理
2.量化误差
计算量化误差时需要计算其均方误差MSE。若图像大小为M×N,I与K分别为原图像和重建图像,则其均方误差计算公式为:
对于图像数据的量化,实验使用峰值信噪比PSNR进行描述。对像素Y分量最大值为255的灰度图像来说,其峰值信噪比(单位为dB)的计算公式如下:
PSNR值越大,代表效果越理想。
3.DPCM编解码系统设计
在本次实验中采用固定预测器和均匀量化器。
- 预测器采用左侧、上方预测均可。
- 量化器采用8比特均匀量化。
本实验的目标是验证验证DPCM的编码效率。
- 首先读取一个256级的灰度图像,采用自己设定的预测方法计算预测误差,并对预测误差进行8比特均匀量化。
- 在DPCM编码器实现的过程中可同时输出预测误差图像和重建图像。
- 将预测误差图像写入文件并将该文件输入Huffman编码器,得到输出码流,给出概率分布图并计算压缩比。
- 将原始图像文件输入Huffman编码器,得到输出码流,给出概率分布图并计算压缩比。
- 最后比较两种系统(1.DPCM+熵编码和2.仅进行熵编码)之间的编码效率(压缩比和图像质量)。压缩质量以PSNR进行计算。
二、算法实现
1.素材准备
通过之前的BMP2YUV程序将给到的BMP文件转换为YUV文件(帧数设置为1)
2.主程序
int main(int argc, char* argv[])
{
char* ori_yuvfilename = NULL;
char* pre_yuvfilename = NULL;
char* rec_yuvfilename = NULL;
FILE* ori_y_file = NULL;
FILE* pre_y_file = NULL;
FILE* rec_y_file = NULL;
int width, height, bitdepth;
ori_yuvfilename = argv[1];
pre_yuvfilename = argv[2];
rec_yuvfilename = argv[3];
width = atoi(argv[4]);
height = atoi(argv[5]);
bitdepth = atoi(argv[6]);
unsigned char* u_buffer = NULL;
unsigned char* v_buffer = NULL;
unsigned char* y_buffer = NULL; //原始图像
unsigned char* rec_y_buffer = NULL; //重建图像
unsigned char* pre_y_buffer = NULL; //预测误差
errno_t err;
if ((err = fopen_s(&ori_y_file, ori_yuvfilename, "rb")) != 0) {
cout << "FAIL TO OPEN YUV FILE!";
exit(1);
}
if ((err = fopen_s(&pre_y_file, pre_yuvfilename, "wb")) != 0) {
cout << "FAIL TO OPEN PRE_YUV FILE!";
exit(1);
}
if ((err = fopen_s(&rec_y_file, rec_yuvfilename, "wb")) != 0) {
cout << "FAIL TO OPEN REC_YUV FILE!";
exit(1);
}
//开辟空间
y_buffer = (unsigned char*)malloc(width * height);
u_buffer = (unsigned char*)malloc(width * height / 4);
v_buffer = (unsigned char*)malloc(width * height / 4);
pre_y_buffer = (unsigned char*)malloc(width * height);
rec_y_buffer = (unsigned char*)malloc(width * height);
if (y_buffer == NULL || u_buffer == NULL || v_buffer == NULL || pre_y_buffer == NULL || rec_y_buffer == NULL) {
cout << "no enought memory\n";
exit(1);
}
//读取原始图像数据
fread(y_buffer, 1, width * height, ori_y_file);
fread(u_buffer, 1, width * height / 4, ori_y_file);
fread(v_buffer, 1, width * height / 4, ori_y_file);
//得到原始图像的灰度值分布
FILE* orig;
if ((err = fopen_s(&orig, "ori.txt", "wb")) != 0) {
cout << "FAIL TO OPEN TXT FILE!";
exit(1);
}
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(y_buffer, pre_y_buffer, rec_y_buffer, width, height, bitdepth);
PSNR(y_buffer, rec_y_buffer, width, height);
FILE* pre;
if ((err = fopen_s(&pre, "pre.txt", "wb")) != 0) {
cout << "FAIL TO OPEN TXT FILE!";
exit(1);
}
double frequency_pre[256] = { 0 };
GetFrequency(pre_y_buffer, frequency_pre, height, width);
for (int i = 0; i < 256; i++)
{
fprintf(pre, "%d\t%f\n", i, frequency_pre[i]);
}
//写入重建图像
fwrite(rec_y_buffer, width * height, 1, rec_y_file);
fwrite(u_buffer, width * height / 4, 1, rec_y_file);
fwrite(v_buffer, width * height / 4, 1, rec_y_file);
//写入预测图像
fwrite(pre_y_buffer, width * height, 1, pre_y_file);
fwrite(u_buffer, width * height / 4, 1, pre_y_file);
fwrite(v_buffer, width * height / 4, 1, pre_y_file);
free(y_buffer);
free(u_buffer);
free(v_buffer);
free(pre_y_buffer);
free(rec_y_buffer);
fclose(ori_y_file);
fclose(pre_y_file);
fclose(rec_y_file);
return 0;
}
3.DPCM
void DPCM(unsigned char* ori_yBuf, unsigned char* pre_yBuf, unsigned char* rec_yBuf, int width, int height, int bitdepth)
{
double num = pow(2, (int)(9 - bitdepth));
for (int i = 0; i < height; i++) //行
{
for (int j = 0; j < width; j++) //列
{
if (j == 0) //左侧预测
{
//第一行参考值为128
pre_yBuf[i * width] = (unsigned char)(((ori_yBuf[i * width] - 128) + 255) / num);
//重建值=反量化后的误差+预测值
rec_yBuf[i * width] = (unsigned char)((pre_yBuf[i * width] - 255 / num) * num + 128);
}
else
{
//计算预测误差并进行量化
pre_yBuf[i * width + j] = (unsigned char)(((ori_yBuf[i * width + j] - rec_yBuf[i * width + j - 1]) + 255) / num);
//重建值=反量化后的误差+预测值
rec_yBuf[i * width + j] = (unsigned char)((pre_yBuf[i * width + j] - 255 / num) * num + rec_yBuf[i * width + j - 1]);
}
//防止溢出
pre_yBuf[i * width + j] = (unsigned char)(pre_yBuf[i * width + j] * num / 2);
if (rec_yBuf[i * width + j] > 255)
rec_yBuf[i * width + j] = 255;
if (rec_yBuf[i * width + j] < 0)
rec_yBuf[i * width + j] = 0;
}
}
}
4.PSNR
void PSNR(unsigned char* ori_yBuf, unsigned char* rec_yBuf, int width, int height) {
double psnr = 0, MSE = 0;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++) {
MSE += (ori_yBuf[i * width + j] - rec_yBuf[i * width + j]) * (ori_yBuf[i * width + j] - rec_yBuf[i * width + j]);
}
}
MSE = MSE / (height * width);
double MAX = 255;
psnr = 10 * log10((MAX * MAX) / MSE);
cout << "psnr=" << psnr << endl;
}
三、实验结果
- 以下从左到右依次为8bit原图、预测误差图像、重建图像
可见8bit量化可以较完整地还原出图像。
- 以下为8bit量化和4bit量化的效果对比与PSNR值
8bit | PSNR | 4bit | PSNR |
---|---|---|---|
27.0756 | 9.9155 | ||
51.1614 | 19.4272 | ||
51.1283 | 14.7268 | ||
14.8862 | 10.8727 | ||
18.2415 | 15.1050 |
可见4bit量化效果明显差于8bit,相应地,PSNR值更小,量化区间数目越小,图像失真越明显。
- Huffman熵编码
将原文件与预测误差文件输入Huffman编码器
得到编码后的文件大小,计算压缩比
图像 | 原大小(KB) | DPCM+熵编码(KB) | 压缩比 | 仅熵编码(KB) | 压缩比 |
---|---|---|---|---|---|
Camman | 96 | 36 | 2.67 | 73 | 1.32 |
Clown | 96 | 45 | 2.13 | 78 | 1.23 |
Lena | 96 | 43 | 2.23 | 77 | 1.25 |
Noise | 96 | 74 | 1.30 | 74 | 1.30 |
Odie | 96 | 13 | 7.39 | 22 | 4.36 |
由表格可知DPCM+熵编码的压缩比≤仅进行熵编码的压缩比,效率更高。
将概率分布txt文件呈现为图表形式
原图概率分布 | 预测误差概率分布 |
Odie图像大部分颜色分部较为简单,背景为黑色,主体为白色,其概率密度集中度较高,方差较大 ,压缩比显著提高。
由概率分布图可知,Camman、Clown像素间相关性较强,概率大的符号相对集中,经过DPCM后的概率分布图符号集中在127附近,符号种类减少,出现概率增加,huffman编码系统对分布不均匀的信源编码效率较高。而Noise符号概率分布比较分散且平缓,符号间相关性不大,经DPCM之后,信源符号个数并没有显著减少,压缩效果较差,但符号也集中向127靠近。
由DPCM算法实现的结果可知,无失真编码之前进行预测编码能够提高系统性能。