失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 【信源编码技术】实验2-Huffman编码

【信源编码技术】实验2-Huffman编码

时间:2023-09-27 02:16:39

相关推荐

【信源编码技术】实验2-Huffman编码

实验来源

《数据压缩导论(第4版)》(Khalid Sayood著)第三章 霍夫曼编码 项目与习题

实验内容

一、利用程序huff_enc和huff_dec进行以下操作(在每种情况下,利用由被压缩图像生成的码本)。

(a)对sena、sensin和omaha图像进行编码。

(b)编写一段程序,得到相邻像素之差,然后利用huffman对差值图像进行编码。

给出以上每一试验得出的文件大小,并解释其差别。

二、利用程序huff_enc和huff_dec,并使用由sensin图像生成的码本,对bookshelf和sena图像进行编码。将这一结果与使用被压缩图像生成码本的情况进行对比。

实验准备

被压缩图像(.img)

按光栅扫描顺序存储的256*256像素8位灰度图sena、sensin、omaha、bookshelf1、bookshelf2

sena

sensin

omaha

bookshelf1

bookshelf2

程序

huff_enc

使用方法:huff_enc [ -i infile ][ -o outfile][-ccodefile][-s storecode ]

-i infile:The name of the file containing the input. Theprogram will checkthe size of the file before it encodes it.If this option is not giventhe program assumes the input is being piped and will read from stdin.

-o outfile:The name of the file the encoded file should be written to.If this option is not given the program will write to stdout.

-c codefile:The name of the file if you want to use a storedcode insteadof calculating one. The defalt is to calculate a code.

-s storecode:The name of the file you want the code written to. If nofilenameisgiventhecode will be written as a header on the encoded file.

DPCM

计算相邻像素之差,将原始图像转化为差值图像。

实验过程与结果

利用由被压缩图像生成的码本对sena、sensin和omaha图像进行编码

利用由sena图像自身生成的码本对其进行编码

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\sena.img -o ..\images\out\sena.huff

得到原始文件sena.img利用自身码本编码后文件sena.huff

利用由sensin图像自身生成的码本对其进行编码

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\sensin.img -o ..\images\out\sensin.huff

得到原始文件sensin.img利用自身码本编码后文件sensin.huff

利用由omaha图像自身生成的码本对其进行编码

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\omaha.img -o ..\images\out\omaha.huff

得到原始文件omaha.img利用自身码本编码后文件omaha.huff

对比与分析

文件大小对照表

注:压缩率是文件压缩后的大小与压缩前的大小之比。

分析

仅仅单独采用Huffman编码对文件大小的压缩程度并不是很大。

利用由被压缩图像生成的码本对sena、sensin和omaha的差值图像进行编码

利用sena的差值图像自身生成的码本对其进行编码

对sena图像的第一列像素保留原值,其余像素使用后向差分计算得到sena的差值图像

sena_diff

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\out\sena_diff.img -o ..\images\out\sena_diff.huff

得到sena差值图像sena_diff.img利用自身码本编码后文件sena_diff.huff

利用sensin的差值图像自身生成的码本对其进行编码

对sensin图像的第一列像素保留原值,其余像素使用后向差分计算得到sensin的差值图像

sensin_diff

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\out\sensin_diff.img -o ..\images\out\sensin_diff.huff

得到sensin差值图像sensin_diff.img利用自身码本编码后文件sensin_diff.huff

利用omaha的差值图像自身生成的码本对其进行编码

对omaha图像的第一列像素保留原值,其余像素使用后向差分计算得到omaha的差值图像

omaha_diff

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\out\omaha_diff.img -o ..\images\out\omaha_diff.huff

得到omaha差值图像omaha_diff.img利用自身码本编码后文件omaha_diff.huff

对比与分析

文件大小对照表

分析

利用了预测模型,对预测残差(像素差分)进行编码,相较于上节仅单独使用Huffman编码的方式能够显著提升文件大小的压缩程度。

使用由sensin图像生成的码本对bookshelf1、2和sena图像进行编码

获取由sensin图像生成的码本

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\sensin.img -o ..\images\out\sensin.huff -s ..\images\out\sensin.code

得到sensin图像sensin.img的码本sensin.code

使用由sensin图像生成的码本对bookshelf1图像进行编码

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\bookshelf1.img -o ..\images\out\bs1_sensincode.huff -c ..\images\out\sensin.code

得到bookshelf1图像bookshelf1.img利用sensin.code码本的编码后文件bs1_sensincode.huff

使用由sensin图像生成的码本对bookshelf2图像进行编码

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\bookshelf2.img -o ..\images\out\bs2_sensincode.huff -c ..\images\out\sensin.code

得到bookshelf2图像bookshelf2.img利用sensin.code码本的编码后文件bs2_sensincode.huff

使用由sensin图像生成的码本对sena图像进行编码

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\sena.img -o ..\images\out\sena_sensincode.huff -c ..\images\out\sensin.code

得到sena图像sena.img利用sensin.code码本的编码后文件sena_sensincode.huff

(为了对比)利用由bookshelf1图像自身生成的码本对其进行编码

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\bookshelf1.img -o ..\images\out\bookshelf1.huff

得到原始文件bookshelf1.img利用自身码本编码后文件bookshelf1.huff

(为了对比)利用由bookshelf2图像自身生成的码本对其进行编码

HuffmanEnc项目属性→调试→命令参数:

-i ..\images\bookshelf2.img -o ..\images\out\bookshelf2.huff

得到原始文件bookshelf2.img利用自身码本编码后文件bookshelf2.huff

对比与分析

文件大小对照表

分析

使用由sensin图像生成的码本对bookshelf1、bookshelf2和sena图像进行编码均不如使用由bookshelf1、bookshelf2和sena图像自身生成码本进行编码的压缩程度大,尤其是使用sensin图像生成的码本对bookshelf1、bookshelf2进行编码后的文件大小竟超过了其原始大小。这应当是因为bookshelf1、bookshelf2是书架图像,和sensin的人物图像的差异较大,而利用sensin图像生成的码本对同为人物的sena图像编码还能够起到一定的压缩作用,但也仍不及使用被压缩图像自身生成码本的压缩程度。

如果觉得《【信源编码技术】实验2-Huffman编码》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。