一、DCPM编解码器原理图
DPCM是差分预测编码调制的缩写,是比较典型的预测编码系统。在DPCM系统中,需要注意的是预测器的输入是已经解码以后的样本。之所以不用原始样本来做预测,是因为在解码端无法得到原始样本,只能得到存在误差的样本。因此,在DPCM编码器中实际内嵌了一个解码器,如编码器中虚线框中所示。
二、实验内容
DPCM编码系统的设计
在本次实验中,我们采用固定预测器和均匀量化器。预测器采用左侧、上方预测均可。量化器采用8比特均匀量化。本实验的目标是验证DPCM编码的编码效率。首先读取一个256级的灰度图像,采用自己设定的预测方法计算预测误差,并对预测误差进行8比特均匀量化(基本要求)。还可对预测误差进行1比特、2比特和4比特的量化设计。
在DPCM编码器实现的过程中可同时输出预测误差图像和重建图像。将预测误差图像写入文件并将该文件输入Huffman编码器,得到输出码流、给出概率分布图并计算压缩比。将原始图像文件输入Huffman编码器,得到输出码流、给出概率分布图并计算压缩比。最后比较两种系统(1.DPCM+熵编码和2.仅进行熵编码)之间的编码效率(压缩比和图像质量)。压缩质量以PSNR进行计算。
头文件
#pragma once
void DPCM(unsigned char*Y_buffer, unsigned char*Y_d, unsigned char*Y_restr, int pre, int number, int bits);
void RDPCM(unsigned char* Y_d, unsigned char* Y_restr, int pre, int number, int bits);
double MSE(unsigned char* Y_buffer, unsigned char* Y_restr, int height, int width);
double PSNR(unsigned char* Y_buffer, unsigned char* Y_restr, int height, int width);
int ErrorQuantity(int X, int bits);
int OverflowX(int x, int High, int Low);
int invErrorQuantity(int X, int bits);
主程序
#include<iostream>
#include<fstream>
#include<math.h>
#include<string.h>
#include"dcpm_head.h"
using namespace std;
int main(void)
{
FILE* file_in;
FILE* file_out;
FILE* file_out2;
if (fopen_s(&file_in, "E:\\数据压缩\\20200503\\seed.yuv", "rb") != 0)
{
cout << "Failed to open the original file!" << endl;
}
else
{
cout << "Successfully opened original file!" << endl;
}
if ((fopen_s(&file_out, "E:\\数据压缩\\20200503\\seed_d.yuv", "wb")) != 0)
{
cout << "Failed to write in the seed_d file!" << endl;
}
else
{
cout << "Successfully written seed_d file!" << endl;
}
if ((fopen_s(&file_out2, "E:\\数据压缩\\20200503\\seed_re.yuv", "wb")) != 0)
{
cout << "Failed to write in the seed_re file!" << endl;
}
else
{
cout << "Successfully written seed_re file!" << endl;
}
int height = 500; int width = 500;
int size = height * width;
int bits = 8;
//cout<<"输入量化比特数"<<endl;
//cin >> bits;
unsigned char* y_b = (unsigned char*)malloc(sizeof(char) * size);
unsigned char* yd_b = (unsigned char*)malloc(sizeof(char) * size);
unsigned char* restr_b = (unsigned char*)malloc(sizeof(char) * size);
unsigned char* else_b = (unsigned char*)malloc(sizeof(char) * size * 2);
fread(y_b, sizeof(unsigned char), size, file_in);
for (int i = 0; i < size * 2; i++) else_b[i] = 128;
for(int i = 0; i< height; i++)
for (int j = 0; j < width; j++)
{
int pre = i * width;
DPCM(y_b, yd_b, restr_b, pre, j, bits);
}
for (int i = 0; i < size; i++) restr_b[i] = 0;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
int pre = i * width;
RDPCM(yd_b, restr_b, pre, j, bits);
}
double peak_SNR = PSNR(y_b, restr_b, height, width);
cout << peak_SNR << endl;
fwrite(yd_b, sizeof(char), size, file_out);
fwrite(else_b, sizeof(char), size * 2, file_out);
fwrite(restr_b, sizeof(char), size, file_out2);
fwrite(else_b, sizeof(char), size * 2, file_out2);
fclose(file_in);
fclose(file_out);
fclose(file_out2);
free(y_b);
free(yd_b);
free(else_b);
free(restr_b);
return 0;
}
子程序
#include <iostream>
#include <cstdio>
#include <fstream>
#include <cmath>
#include "dcpm_head.h"
using namespace std;
void DPCM(unsigned char* Y_buffer, unsigned char* Y_d, unsigned char* Y_restr, int pre, int number, int bits)
{
/*for the first Pixel,suppose YError[0]=128-YOrigi[0],
then quantify it and store*/
int d_temp, r_temp;
switch (number)
{
case 0:
d_temp = 128 - Y_buffer[pre + number];
//quantity
d_temp = ErrorQuantity(d_temp, bits);
Y_d[pre + number] = d_temp;
//inverse quantization
d_temp = invErrorQuantity(d_temp, bits);
r_temp = 128 - d_temp;
//prevent overflow
r_temp = OverflowX(r_temp, 255, 0);
Y_restr[pre + number] = (unsigned char)r_temp;
break;
default:
d_temp = Y_buffer[pre + number] - Y_restr[pre + number - 1];
//quantity
d_temp = ErrorQuantity(d_temp, bits);
Y_d[pre + number] = d_temp;
//inverse quantization
d_temp = invErrorQuantity(d_temp, bits);
r_temp = d_temp + Y_restr[pre + number - 1];
//prevent overflow
r_temp = OverflowX(r_temp, 255, 0);
Y_restr[pre + number] = (unsigned char)r_temp;
break;
}
}
void RDPCM(unsigned char* Y_d, unsigned char* Y_restr, int pre, int number, int bits)
{
int d_temp, r_temp;
//int number = pre_number % width;
switch (number)
{
case 0:
//inverse quantization
d_temp = invErrorQuantity(Y_d[pre + number], bits);
r_temp = 128 - d_temp;
//prevent overflow
r_temp = OverflowX(r_temp, 255, 0);
Y_restr[pre + number] = (unsigned char)r_temp;
break;
default:
//inverse quantization
d_temp = invErrorQuantity(Y_d[pre + number], bits);
r_temp = d_temp + Y_restr[pre + number - 1];
//prevent overflow
r_temp = OverflowX(r_temp, 255, 0);
Y_restr[pre + number] = (unsigned char)r_temp;
break;
}
}
int ErrorQuantity(int X, int bits)
{
X = X + 255;//化为无符号数
X = X / 2;//将误差范围归一到[0,255]
X = floor(X / pow(2, 8 - bits));
X = X * pow(2, 8 - bits);
X = OverflowX(X, 255, 0);
return X;
}
int OverflowX(int x, int High, int Low)
{
if (x > High)
return High;
else if (x < Low)
return Low;
else
return x;
}
int invErrorQuantity(int X, int bits)
{
X = X * 2;//将误差放回原来的范围
X = X - 255;//将无符号数转为有符号数
return X;
}
double MSE(unsigned char* Y_buffer, unsigned char* Y_restr, int height, int width)
{
int size = height * width;
long long int sum = 0;
double mean;
for (int i = 0; i < size; i++)
{
long long int temp = (long long int)(Y_buffer[i] - Y_restr[i]) * (long long int)(Y_buffer[i] - Y_restr[i]);
sum += temp;
}
mean = (double)sum / (double)size;
return mean;
}
double PSNR(unsigned char* Y_buffer, unsigned char* Y_restr, int height, int width)
{
int fmax = pow(2, 8) - 1;
int a = fmax * fmax;
double mean_se = MSE(Y_buffer, Y_restr, height, width);
double peak_SNR = 10 * log10((double)a / mean_se);
return peak_SNR;
}
8bit示例图