



下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、精选优质文档-倾情为你奉上clear X=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(:)
2、,unzipped(:)cr=info.ratiowhos data unzipped zippedfunction zipped, info = huffencode(vector)% 输入和输出都是 uint8 格式% info 返回解码需要的结构信息% info.pad 是添加的比特数% info.huffcodes 是 Huffman 码字% info.rows 是原始图像行数% info.cols 是原始图像列数% info.length 是原始图像数据长度% info.maxcodelen 是最大码长if isa(vector, 'uint8') error(
3、9;input argument must be a uint8 vector');endm, 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 = ce
4、ll(len, 1);% 生成 Huffman 树,得到码字编码表while length(f)>1 index1 = symbols_index1; index2 = symbols_index2; 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(
5、3:end); f, sortindex = sort(f); symbols_index = symbols_index(sortindex);endcodeword = cell(256, 1);codeword(symbols) = codeword_tmp;len = 0;for index = 1:length(vector) %得到整个图像所有比特数 len = len + length(codeworddouble(vector(index)+1);endstring = repmat(uint8(0), 1, len);pointer = 1;for index = 1:len
6、gth(vector) %对输入图像进行编码 code = codeworddouble(vector(index)+1; len = length(code); string(pointer + (0:len-1)=code; pointer = pointer + len;endlen = length(string);pad = 8-mod(len, 8);if pad > 0 string = string uint8(zeros(1, pad);endcodeword = codeword(symbols);codelen = zeros(size(codeword);weig
7、hts = 2.(0:23);maxcodelen = 0;for index = 1:length(codeword) len = length(codewordindex); if len > maxcodelen; maxcodelen = len; end if len > 0 code = sum(weights(codewordindex = 1); code = bitset(code, len + 1); codewordindex = code; codelen(index) = len; endendcodeword = codeword:; %计算压缩的向量c
8、ols = 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) % length(codeword) %numel(codeword) huffcodes(codeword(index), 1) = symbols(index);end %填写解码时所需的结构信息info.pad = pad;inf
9、o.huffcodes = huffcodes;info.ratio = cols./length(vector);info.length = length(vector);info.maxcodelen = maxcodelen;info.rows = m;info.cols = n; %函数addnode添加节点function codeword_new = addnode(codeword_old, item)codeword_new = cell(size(codeword_old);for index = 1:length(codeword_old) codeword_newinde
10、x = item codeword_oldindex;end%函数frequency计算各符号出现的概率function f = frequency(vector)if isa(vector, 'uint8') error('input argument must be a uint8 vector');endf = repmat(0, 1, 256);len = length(vector);for index = 0:255 f(index+1) = sum(vector = uint8(index);endf = f./len; %归一化 function
11、 vector = huffdecode(zipped, info, image)% 函数对输入矩阵vector进行Huffman解码,返回解压后的图像数据if isa(zipped, 'uint8') error('input argument must be 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.*(
12、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;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; c
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 6509-2025聚己内酰胺(PA6)切片和纤维中己内酰胺及低聚物含量的测定
- 2025海南保亭黎族苗族自治县市场监督管理局公益性岗位人员招聘1人模拟试卷及答案详解(夺冠)
- 2025年度黑龙江省气象部门高校毕业生招聘4人(第三批次气象类)考前自测高频考点模拟试题及答案详解(新)
- 2025广东广州市中山大学孙逸仙纪念医院康复医学科医教研岗位和医技岗位招聘2人模拟试卷及答案详解(历年真题)
- 2025福建厦门市集美实验学校非在编教师招聘1人模拟试卷完整参考答案详解
- 2025河南开封国禹运营管理有限公司招聘园区转运中心人员10人考前自测高频考点模拟试题附答案详解(黄金题型)
- 2025合同履行期间的保证期间与证期间的长短及起算点
- 冬季防疫知识培训总结课件
- 2025湖南红花园投资开发有限公司招聘10人模拟试卷及完整答案详解1套
- 2025湖北咸宁市通城县事业单位高层次和急需紧缺人才引进48人考前自测高频考点模拟试题(含答案详解)
- 2025年国家电网《安规》考试判断题库及答案
- 2025上海美术馆招聘6人备考考试题库附答案解析
- 检验科二级生物实验室汇报
- 盾构姿态监测-洞察及研究
- 药店店员技巧培训
- 学生进厂安全培训课件
- 2025年四川省巴中市中考数学试题
- 乡村振兴课件简介模板
- (一检)泉州市2026届高三高中毕业班质量监测(一)数学试卷(含标准答案)
- 管道热处理课件
- 手机媒体概论(自考14237)复习必备题库(含真题、典型题)
评论
0/150
提交评论