霍夫曼图像压缩编码源程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

clearX=imread('lena512.bmp');data=uint8(X);[zipped,info]=huffencode(data);%调用Huffman编码程序进行压缩unzipped=huffdecode(zipped,info,data);%调用Huffman编码程序进行解码%显示原始图像和经编码后的图像,显示压缩比,并计算均方根误差得erms=0,表示是Huffman是无失真编码subplot(121);imshow(data);subplot(122);imshow(unzipped);%erms=compare(data(:),unzipped(:))cr=info.ratiowhosdataunzippedzippedfunction[zipped,info]=huffencode(vector)%输入和输出都是uint8格式%info返回解码需要的结构信息%info.pad是添加的比特数%info.huffcodes是Huffman码字%info.rows是原始图像行数%info.cols是原始图像列数%info.length是原始图像数据长度%info.maxcodelen是最大码长if~isa(vector,'uint8')error('inputargumentmustbeauint8vector');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);%生成Huffman树,得到码字编码表whilelength(f)1index1=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);endcodeword=cell(256,1);codeword(symbols)=codeword_tmp;len=0;forindex=1:length(vector)%得到整个图像所有比特数len=len+length(codeword{double(vector(index))+1});endstring=repmat(uint8(0),1,len);pointer=1;forindex=1:length(vector)%对输入图像进行编码code=codeword{double(vector(index))+1};len=length(code);string(pointer+(0:len-1))=code;pointer=pointer+len;endlen=length(string);pad=8-mod(len,8);ifpad0string=[stringuint8(zeros(1,pad))];endcodeword=codeword(symbols);codelen=zeros(size(codeword));weights=2.^(0:23);maxcodelen=0;forindex=1:length(codeword)len=length(codeword{index});iflenmaxcodelen;maxcodelen=len;endiflen0code=sum(weights(codeword{index}==1));code=bitset(code,len+1);codeword{index}=code;codelen(index)=len;endendcodeword=[codeword{:}];%计算压缩的向量cols=length(string)/8;string=reshape(string,8,cols);weights=2.^(0:7);zipped=uint8(weights*double(string));%码表存储到一个希疏矩阵huffcodes=sparse(1,1);forindex=1:nnz(codeword)%length(codeword)%numel(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;%函数addnode添加节点functioncodeword_new=addnode(codeword_old,item)codeword_new=cell(size(codeword_old));forindex=1:length(codeword_old)codeword_new{index}=[itemcodeword_old{index}];end%函数frequency计算各符号出现的概率functionf=frequency(vector)if~isa(vector,'uint8')error('inputargumentmustbeauint8vector');endf=repmat(0,1,256);len=length(vector);forindex=0:255f(index+1)=sum(vector==uint8(index));endf=f./len;%归一化functionvector=huffdecode(zipped,info,image)%函数对输入矩阵vector进行Huffman解码,返回解压后的图像数据if~isa(zipped,'uint8')error('inputargumentmustbebeauint8vector');end%产生0,1序列,每位占一个字节len=length(zipped);string=repmat(uint8(0),1,len.*8);bitindex=1:8;forindex=1:lenstring(bitindex+8.*(index-1))=uint8(bitget(zipped(index),bitindex));endstring=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;forindex=1:lencode=bitset(code,codeindex,string(index));codeindex=codeindex+1;byte=decode(bitset(code,codeindex),info);ifbyte0vector(vectorindex)=byte-1;codeindex=1;code=0;vectorindex=vectorindex+1;endendvector=reshape(vector,info.rows,info.cols);%函数decode返回码字对应的符号functionbyte=decode(code,info)byte=info.huffcodes(code);

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功