多媒体课程设计报告[1]_多媒体课程设计报告

2020-02-27 其他范文 下载本文

多媒体课程设计报告[1]由刀豆文库小编整理,希望给你工作、学习、生活带来方便,猜你可能喜欢“多媒体课程设计报告”。

广州大学机械与电气工程学院

课程设计报告

设计题目: 采用matlab实现霍夫曼编码仿真

专业班级: 电信112

姓 名:

学 号:

指导老师: 高星辉 李丽

完成日期: 2014年7月

一、实现功能

哈夫曼编码是一种无损压缩编码,它不会造成信息损失,解压缩时能够从压缩数据精确地恢复原始图像。jpg彩色图像,有RGB三个分量,所以其输出有三个分量解码输出的图像。

二、各个控件功能及代码

系统设计的完整主程序如下

%%%%%%%%%%%%%%%%%%%%%%%%%主程序%%%%%%%%%%%%%%%%%%%%%%%%%%% clc clear cd;X=imread('2014.jpg');data=uint8(X);[zipped,info]=huffencode(data);unzipped=huffdecode(zipped,info);subplot(121);imshow(data);title('原始图像')subplot(122);imshow(unzipped);title('解码后的图像')whos data unzipped zipped fprintf('pad=%dn',info.pad);%info.pad=为凑整字节数,编码字符串最后添加零的位数

fprintf('ratio=%fn',info.ratio);%info.ratio=压缩率

fprintf('maxcodelen=%dn',info.maxcodelen);%info.maxcodelen=最大码长

%%%%%%%%%%%%%%%%%%%%%%%%%%编码函数%%%%%%%%%%%%%%%%%%%%%%%%%% %huffencode函数对输入矩阵vector进行huffman编码,返回编码后的向量及相关信息

function [zipped,info]= huffencode(vector)if ~isa(vector,'uint8')eror('input argument must be a uint8 vector');end [m,n]=size(vector);vector=vector(:)';

f=frequency(vector);symbols=find(f~=0);f=f(symbols);[f,sortindex]=sort(f);symbols=symbols(sortindex);len=length(symbols);symbols_index=num2cell(1:len);codeword_tmp=cell(len,1);while length(f)>1 index1=symbols_index{1};index2=symbols_index{2};codeword_tmp(index1)=addnode(codeword_tmp(index1),uint8(0));codeword_tmp(index2)=addnode(codeword_tmp(index2),uint8(1));f=[sum(f(1:2))f(3:end)];symbols_index=[{[index1,index2]} symbols_index(3:end)];[f,sortindex]=sort(f);symbols_index=symbols_index(sortindex);end codeword=cell(256,1);codeword(symbols)=codeword_tmp;len=0;for index=1:length(vector)len=len+length(codeword{double(vector(index))+1});end string=repmat(uint8(0),1,len);pointer=1;for index=1:length(vector)code=codeword{double(vector(index))+1};len=length(code);string(pointer+(0:len-1))=code;

pointer=pointer+len;end len=length(string);pad=8-mod(len,8);if pad>0 string=[string uint8(zeros(1,pad))];end codeword=codeword(symbols);codelen=zeros(size(codeword));weights=2.^(0:23);maxcodelen=0;for index=1:length(codeword)len=length(codeword{index});if len>maxcodelen maxcodelen=len;end if len>0 code=sum(weights(codeword{index}==1));code=bitset(code,len+1);codeword{index}=code;codelen(index)=len;end end codeword=[codeword{:}];%计算压缩后的向量

cols=length(string)/8;string=reshape(string,8,cols);weights=2.^(0:7);zipped=uint8(weights*double(string));

%码表存储到一个稀疏矩阵 huffcodes=sparse(1,1);for index=1:nnz(codeword)huffcodes(codeword(index),1)=symbols(index);end

%填写解码时所需的结构信息 info.pad=pad;info.huffcodes=huffcodes;info.ratio=cols./length(vector);info.length=length(vector);info.maxcodelen=maxcodelen;info.rows=m;info.cols=n;%huffdecode函数对输入矩阵vector进行Huffman编码,%%%%%%%%%%%%%%%%%%%%%%%%%%解码函数%%%%%%%%%%%%%%%%%%%%%%%%% %huffdecode函数对输入矩阵vector进行huffman解码,返回解压后的图像数据 function vector=huffdecode(zipped,info)if ~isa(zipped,'uint8')error('input argument must be a uint8 vector');end

%产生0,1序列,每位占一个字节 len=length(zipped);string=repmat(uint8(0),1,len.*8);bitindex=1:8;for index=1:len

string(bitindex+8.*(index-1))=uint8(bitget(zipped(index),bitindex));end

string=logical(string(:)');len=length(string);string((len-info.pad+1):end)=[];len=length(string);

%开始解码

weights=2.^(0:51);vector=repmat(uint8(0),1,info.length);vectorindex=1;codeindex=1;code=0;for index=1:len code=bitset(code,codeindex,string(index));codeindex=codeindex+1;byte=decode(bitset(code,codeindex),info);if byte>0 vector(vectorindex)=byte-1;codeindex=1;code=0;vectorindex=vectorindex+1;end end vector=reshape(vector,info.rows,info.cols);%%%%%%%%%%%%%%%%函数frequency计算各符号出现的概率%%%%%%%%%%%%%%% function f=frequency(vector)if~isa(vector,'uint8')error('input argument must be a uint8 vector');end f=repmat(0,1,256);len=length(vector);

for index=0:255 f(index+1)=sum(vector==uint8(index));end f=f./len;%%%%%%%%%%%%%%%%%%%%%%%%%函数addnode添加节点%%%%%%%%%%%%%%%%%%% function codeword_new=addnode(codeword_old,item)codeword_new=cell(size(codeword_old));for index=1:length(codeword_old)codeword_new{index}=[item codeword_old{index}];end %%%%%%%%%%%%%%%%%%%%函数decode返回码字对应的符号%%%%%%%%%%%%% function byte=decode(code,info)byte=info.huffcodes(code);

三、运行结果

1)图像压缩、解压缩整个过程大约要花20~30 min,一开始不知道,以为死机,后来稍等会就会出结果;

(2)认真观察原始图像和新图像,比较发现:新旧的位图图像视觉效果相差不大,但是其文件大小却变小了。如2014.jpg图像2880000bytes减小到2466205bytes。压缩率为0.856321。由此可说明,哈夫曼编码是一种无损压缩编码,它不会造成信息损失,解压缩时能够从压缩数据精确地恢复原始图像。

(3)比较两幅不同大小的位图的压缩比可知,对不同的信源,哈夫曼编码的压缩比不同。

(4)jpg彩色图像,有RGB三个分量,所以其输出有三个分量解码输出的图像。

四、课程设计心得总结

通过本次设计,我进一步巩固了哈夫曼压缩编码基本原理及方法,学会了使用MATLAB编写哈夫曼编码程序,并仿真实现基于哈夫曼编码的图像编解码系统;也初步了解图像压缩编码技术的应用和开发,进一步提高编程能力;此外,我对于matlab的有关操作也更加熟悉了。

此外,在这次课程设计中开始调试程序时,解码的图像迟迟不能显示,以为MATLAB软件死机,其实是哈夫曼编码有一定的时间(一般为2-3分钟),这是由于自己的不耐心而导致了这个问题。所以我从中习得不管做什么设计、项目,耐心最重要。当未出结果的时候,我们不能一味的焦躁,而是应该冷静的分析,找出问题的所在。

总之这次设计,我受益匪浅。

《多媒体课程设计报告[1].docx》
将本文的Word文档下载,方便收藏和打印
推荐度:
多媒体课程设计报告[1]
点击下载文档
相关专题 多媒体课程设计报告 报告 课程设计 多媒体 多媒体课程设计报告 报告 课程设计 多媒体
[其他范文]相关推荐
    [其他范文]热门文章
      下载全文