实验原理
预测编码的基本原理
信源的相邻符号通常是相关的
预测编码:利用信源相邻符合之间的相关性
根据某一模型利用以往的样本值对新样本进行预测,然后将样本的实际值与其预测值相减得到一个误差值,最后对这一误差值进行编码。
如果模型足够好,且样本序列在时间上相关性较强,则误差信号的幅度将远远小于原始信号,从而得到较大的数据压缩。
DPCM的基本原理
DPCM编码系统的设计
在本次实验中,我们采用固定预测器和均匀量化器。预测器采用左侧、上方预测均可量化器采用8比特均匀量化。本实验的目标是验证DPCM编码的编码效率。首先读取一个256级的灰度图像,采用自己设定的预测方法计算预测误差,并对预测误差进行8比特均匀量化(基本要求)。还可对预测误差进行1比特、2比特和4比特的量化设计(提高要求)。
PSNR
PNSR(Peak Signal to Noise Ratio),即峰值信噪比,是基于对应像素点间的误差,即基于误差敏感的图像质量评价,但是并没有考虑人眼的视觉特性,因而经常出现评价结果与人的主观感受不一致的情况。
·PSNR高于40dB说明图像质量极好,即非常接近原始图像;
·PSNR在30~40dB通常表示图像质量是好的,即失真可以察觉但是可以接受;
·PSNR在20~30dB说明图像质量差;
·PSNR低于20dB的图像通常不可以被接受。
PNSR计算公式:
MSE是指均方误差,计算公式为:
实验分析
代码分析
主要代码
#include<iostream>
#include<math.h>
using namespace std;
//量化
int Quant(int qbit, int error)
{
int k = 0;
k = ((error + 255) / pow(2, (9 - qbit))); //+255将误差值抬为正值再做量化
return k;
}
//反量化
int inverseQuant(int qbit, unsigned char pred) {
int k = 0;
k = (pred * pow(2, (9 - qbit)) - 255);
return k;
}
void DPCM(unsigned char* y_buffer, unsigned char* pred_buffer, unsigned char* re_buffer, int width, int height, int qbit) {
int error;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (j == 0)
{
error = (y_buffer[i * width + j]) - 128; //误差值
pred_buffer[i * width + j] = Quant(qbit, error); //量化误差值
re_buffer[i * width + j] = inverseQuant(qbit, pred_buffer[i * width + j]) + 128; //重建值
}
else
{
error = (y_buffer[i * width + j]) - re_buffer[i * width + j - 1]; //误差值
pred_buffer[i * width + j] = Quant(qbit, error); //量化误差值
re_buffer[i * width + j] = inverseQuant(qbit, pred_buffer[i * width + j]) + re_buffer[i * width + j - 1]; //重建值
}
}
}
int max = pow(2, qbit) - 1; //当前量化的最大值
for (int i = 0; i < width * height; i++)
{
if (pred_buffer[i] < 0) pred_buffer[i] = 0;
if (pred_buffer[i] > max) pred_buffer[i] = max;
if (re_buffer[i] < 0) re_buffer[i] = 0;
if (re_buffer[i] > 255) re_buffer[i] = 255;
}
}
//计算峰值信噪比
double PSNR(unsigned char* y_buffer, unsigned char* re_buffer, int width, int height, int qbit)
{
double mse = 0, psnr = 0;
for (int i = 0; i < width * height; i++)
{
mse += pow((y_buffer[i] - re_buffer[i]), 2);
}
mse = mse / (width * height);
psnr = 10 * log10(pow(255, 2) / mse);
return psnr;
}
//计算概率
void Frequency(unsigned char* buffer, double* frequency, int height, int width)
{
int size = height * width;
for (int i = 0; i < size; i++) frequency[buffer[i]] += 1;
for (int i = 0; i < 256; i++) frequency[i] /= size;
}
main函数
int main(int argc, char** argv)
{
char* o_name = argv[1]; //原始图像
char* p_name = argv[2]; //预测误差图像
char* r_name = argv[3]; //重建图像
int qbit = atoi(argv[4]); //量化bit数
FILE* o_file = NULL; //原始yuv
FILE* p_file = NULL; //预测误差yuv
FILE* r_file = NULL; //重建yuv
int width = 256, height = 256;
unsigned char* y_buffer = new unsigned char[width * height];
unsigned char* u_buffer = new unsigned char[(width * height) / 4];
unsigned char* v_buffer = new unsigned char[(width * height) / 4]; //原始图像的yuv
unsigned char* p_buffer = new unsigned char[width * height]; //预测误差图像buffer
unsigned char* r_buffer = new unsigned char[width * height]; //重建图像buffer
o_file = fopen(o_name, "rb");
if (o_file == NULL) {
cout << "Can't open the origin image!" << endl;
}
else {
cout << "The origin image has been opened!" << endl;
}
p_file = fopen(p_name, "wb");
if (p_file == NULL) {
cout << "Can't open the predict image!" << endl;
}
else {
cout << "The predict image has been opened!" << endl;
}
r_file = fopen(r_name, "wb");
if (r_file == NULL) {
cout << "Can't open the rebuild image!" << endl;
}
else {
cout << "The rebild image has been opened!" << endl;
}
//读原文件
fread(y_buffer, 1, width * height, o_file);
fread(u_buffer, 1, (width * height) / 4, o_file);
fread(v_buffer, 1, (width * height) / 4, o_file);
//计算原文件的概率分布
FILE* orig;
orig = fopen("D:/xlsx/Lena256B_2.txt", "wb");
double frequency[256] = { 0 };
Frequency(y_buffer, frequency, height, width);
fprintf(orig, "%s\t%s\n", "symbol", "freq");
for (int i = 0; i < 256; i++)
{
fprintf(orig, "%d\t%f\n", i, frequency[i]);
}
//DPCM
DPCM(y_buffer, p_buffer, r_buffer, width, height, qbit);
//计算PSNR
double psnr;
psnr = PSNR(y_buffer, r_buffer, width, height, qbit);
cout << "The PSNR of " << o_name << " is " << psnr << endl;
//写预测误差文件
fwrite(p_buffer, 1, width * height, p_file);
fwrite(u_buffer, 1, (width * height) / 4, p_file);
fwrite(v_buffer, 1, (width * height) / 4, p_file);
//计算预测误差文件的概率分布
FILE* pred;
pred = fopen("D:/xlsx/Lena256B_p_2.txt", "wb");
double frequency_[256] = { 0 };
Frequency(p_buffer, frequency_, height, width);
fprintf(pred, "%s\t%s\n", "symbol", "freq");
for (int i = 0; i < 256; i++)
{
fprintf(pred, "%d\t%f\n", i, frequency_[i]);
}
//写重建图像文件
fwrite(r_buffer, 1, width * height, r_file);
fwrite(u_buffer, 1, (width * height) / 4, r_file);
fwrite(v_buffer, 1, (width * height) / 4, r_file);
fclose(o_file);
fclose(p_file);
fclose(r_file);
delete[] y_buffer;
delete[] u_buffer;
delete[] v_buffer;
delete[] p_buffer;
delete[] r_buffer;
return 0;
}
使用matlab将输出的概率分布文件画出
clc;%清理命令行窗口
clear all;%清理工作区
%读取excel数据,同目录下
tx=xlsread('Lena256B.xlsx');
symbol=tx(:,1);%第1列数据
freq=tx(:,2);%第2列数据
subplot(1,1,1)
plot(symbol,freq)
title('原始图像概率分布');
xlabel('symbol');
legend('fred');
实验结果
原始图像
原始图像概率分布
量化比特数 | 预测图 | 恢复图 | 预测图概率分布 | 峰值信噪比 |
8bit | ![]() | ![]() | ![]() | 51.1338 |
4bit | ![]() | ![]() | ![]() | 14.8189 |
2bit | ![]() | ![]() | ![]() | 7.64999 |
量化电平越低,峰值信噪比越低,重建图像的质量越差,在2bit时已经达到了肉眼不能忍受的失真。