实验四 DPCM 压缩系统的实现和分析

一.实验目的

掌握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后的熵编码直接进行熵编码
原图大小96KB96KB
压缩后大小45.768.2KB
压缩比47.6%71%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值