Data Compression Experiment Four: Realization and Analysis of DPCM Compression System

1. Experimental principle

1. DPCM codec principle

     DPCM is the abbreviation of Differential Predictive Coding Modulation, which is a typical predictive coding system. Predictive coding is to use the correlation between adjacent symbols of the source to predict the new sample using the previous sample value according to a certain model, and then subtract the actual value of the sample from its predicted value to obtain an error value, and finally its error value. to encode. If the model is good enough and the sample sequence is highly correlated in time, the magnitude of the error signal will be much smaller than the original signal, resulting in greater data compression.

     The block diagram of the DPCM codec system is as follows:

     

    Where Xk^ is the predicted value of Xk, which is the decoded value (reconstructed value) of the previous sample, so a decoder is actually embedded in the encoder, as shown in the red box in the figure.

    The prediction error ek is obtained by subtracting the original value Xk of the sample and the predicted value Xk^, and the quantized prediction error ek^ is obtained by quantizing ek. In addition to being sent to the encoder for encoding and output, ek^ is also used to update the predicted value. That is, add ek^ and the original predicted value Xk^ to form a new input Xk' of the predicted value.

    The quantization error of the DPCM system, that is, the difference between the original value input to the encoder and the reconstructed value Xk' with the quantization error after quantization,     is the quantization error introduced by the quantization of the prediction error ek.

 

2. Design of DPCM system

      Two parts need to be designed in a DPCM system: predictor and quantizer. Ideally, the predictor and quantizer should be jointly optimized. In practice, a suboptimal design method is adopted: the optimal design of the linear predictor and the quantizer are carried out separately.

    In this experiment, a fixed predictor and a uniform quantizer are used. The predictor uses adjacent left samples, and the quantizer uses 8bit uniform quantization.

Second, the experimental process analysis

   1. Based on Experiment 2 BMP2YUV, read a BMP image, convert and extract the Y component, and store it in the yBuf buffer.

   2. Loop through each pixel of the image, take the reconstructed value of the adjacent left sample as the predicted value, and subtract the original value from the predicted value to obtain the prediction error dev.

   3. Perform 8bit quantization on dev, and store the quantized prediction error in the buffer Q_dev.

   4. Inversely quantize the quantized prediction error, and then add it to the current predicted value (ie, the reconstructed value of the previous sample) to obtain the reconstructed value of the current sample and store it in the y_new buffer.

   5. Output the quantized prediction error image and the reconstructed image. Write the prediction error image to file and feed it to the Huffman encoder, and compare it with the original image file input to the Huffman encoder.

3. Key code and analysis

Create three buffers:

unsigned char* yBuf=(unsigned char*)malloc(width*height);///Pixel initial value buffer
unsigned char* y_new=(unsigned char*)malloc(width*height);///Pixel reconstruction value buffer
unsigned char* Q_dev=(unsigned char*)malloc(width*height);///Quantized prediction error buffer


Because after the Y component is extracted from the BMP image, the obtained image is a grayscale image, and the grayscale value range is [0, 255]. The prediction error range [-255, 255] obtained by subtracting the reconstruction value of the current pixel and the previous pixel. To perform 8-bit quantization, you can directly divide the prediction error by 2 to obtain the quantized prediction error range [-127, 127], and then do +128 transform, transform the quantized prediction error range to [0,255], and output the corresponding image file.

Then perform inverse quantization on the quantized prediction error, that is, multiply the quantized prediction error by 2.

for(i=0;i<width * height;i++)///Iterate over each pixel
{
	float dev;///Define the prediction error as a floating point type
	if(i%width==0)///If the pixel is in the first column of the image
	{
		dev=(float)(*(y+i)-128);///The predicted value is selected as 128, and the original value and the predicted value are subtracted to obtain the prediction error
		*(Q_dev+i)=(unsigned char)(dev/2+128);/// Quantize the prediction error and transform the gray value
		*(y_new+i)=(*(Q_dev+i)-128)*2+128;///Inversely quantize the quantized prediction error and add it to the predicted value to get the reconstructed value
	}
	else/// if the pixel is not in the first column of the image
	{
		dev=(float)(*(y+i)-*(y_new+i-1));///Select the reconstructed value of the previous pixel as the predicted value
		*(Q_dev+i)=(unsigned char)(dev/2+128);
		*(y_new+i)=(*(Q_dev+i)-128)*2+*(y_new+i-1);

	}
}


 

Finally write the corresponding buffer to the file:

fwrite(yBuf, 1, width * height, yuvFile);
fwrite(y_new,1,width*height,y_newFile);
fwrite(Q_dev,1,width*height,Q_devFile);

4. Experimental results and analysis

1. Enter a 24-bit true color image:

 

It can be seen that the original image (left image) and the reconstructed image (right image) are almost indistinguishable.

By comparing the pixels of the same macroblock, it can be found that the grayscale values ​​of some pixels are not exactly the same. For example, the pixel value of the original image coordinate (0,7) is 119, the predicted value is the reconstruction value of the left pixel (0,6) point 112, the prediction error is 119-112=7, and the prediction error after quantization is 7/2=3.5 , since the data output to the Q_dev buffer is of unsigned char type, the floating point number 3.5 is converted into 3 after data type conversion, and then inversely quantized 3*2=6, which is added to the predicted value to obtain the reconstructed value of this point 112+6=118 , there is an error with the corresponding pixel value of the original image.

dev=(float)(*(y+i)-*(y_new+i-1));///Select the reconstructed value of the previous pixel as the predicted value
*(Q_dev+i)=(unsigned char)(dev/2+128);
*(y_new+i)=(*(Q_dev+i)-128)*2+*(y_new+i-1);


 

The quantized prediction error image is as follows, and its gray values ​​are all concentrated around 128.

2. Select other groups of BMP images for testing:

 

 

 

 

The difference image and quantized prediction error image are as follows:

 

 

The original image file of the above image and the quantized prediction error image Q_dev file are respectively sent to the Huffman encoder for encoding to obtain the output code stream.

The probability distribution of the original image and the quantized prediction error image is as follows:

 

 

Regardless of the probability distribution of the original image, the probability distribution of the quantized prediction error values ​​becomes concentrated, and is distributed around 0. After the +128 conversion, the prediction error values ​​are concentrated around 128.

 

Original image file size before compression (KB) 1153 66 193 66 66 1407
Original image file size after compression (KB) 1099 59 184 60 13 1283
原图文件压缩比 1.049 1.119 1.049 1.1 5.077 1.097
量化预测误差图像文件压缩前大小(KB) 384 64 64 64 64 469
量化预测误差图像文件压缩后大小(KB) 175 35 42 62 17 138
量化预测误差图像文件压缩比大小 2.194 1.828 1,524 1.032 3.765 3.398

可以看到,对于一般图像,对量化预测图像文件进行编码,比直接对原图进行编码,压缩比都有所提高。而对于第四幅图像(噪声图像),由于原图图像细节多,相邻像素间相关性较小,预测编码后的预测误差值分布也较为分散,压缩比得不到明显改善。

五、实验结论


 如果直接对原图编码,由于原始图像概率分布往往较为均匀分散,不能得到很好的压缩。而采用差分预测编码,即对量化后的预测误差值进行编码,由于预测误差值在整个取值范围[0,255]内分布极不均匀,通过Huffman编码后,压缩效率可以大大提高。这样在实际应用中运用差分预测编码可以节省许多的存储资源,减小传输带宽。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325686084&siteId=291194637