资源简介
霍夫曼,算术,行程,jpg 图像压缩在matlab下的源代码
代码片段和文件信息
clear all
fprintf(‘Reading data...‘)
data = imread(‘cameraman.tif‘);
data = uint8(data); %读入数据并将数据限制为uint8
fprintf(‘Done!\n‘)
%编码压缩
fprintf(‘compressing data ...‘);
[zippedinfo] = norm2huff(data);
fprintf(‘Done!\n‘)
%解压缩
fprintf(‘compresing data ...‘);
unzipped = huff2norm(zippedinfo);
fprintf(‘Done!\n‘)
%测试是否无失真
isOK = isequal(data(:)unzipped(:))
%显示压缩效果
whos data zipped unzipped
%%%%%%%%%% norm2huff %%%%%%%%%%
function [zippedinfo] = norm2huff(vector)
if ~isa(vector‘uint8‘)
error(‘input argument must be a uint8 vector‘)
end
vector = vector(:)‘;
%将输入向量转换为行向量
f = frequency(vector);
%计算各元素出现的概率
simbols = find(f~=0);
f = f(simbols); %将元素按照出现的概率排列
[fsortindex] = sort(f);
simbols = simbols(sortindex);
%产生码字generate the codeword as the 52 bits of a double
len = length(simbols);
simbols_index = num2cell(1:len);
codeword_tmp = cell(len1);
while length(f)>1
index1 = simbols_index{1};
index2 = simbols_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)];
simbols_index = [{[index1 index2]} simbols_index(3:end)];
%将数据重新排列使两个节点的频率尽量与前一个节点的频率相当
resort data in order to have the two nodes with lower frequency as
first to
[fsortindex] = sort(f);
simbols_index = simbols_index(sortindex);
end
%对应相应的元素与码字
codeword = cell(256:1);
codeword(simbols) = codeword_tmp;
%计算总的字符串长度
len = 0;
for index=1:length(vector)
len = len+length(codeword{double(vector(index))+1});
end
%产生01序列
string = repmat(uint8(0)1len);
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(len8);
if pad>0
string =[string unit8(zeros(1pad))];
end
%保存实际有用的码字
codeword =codeword(simbols);
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(codelen+1);
codeword{index} = code;
codelen(index) = len;
end
end
codeword = [codeword{:}]
%计算压缩后的向量
cols = length(string)/8;
string = reshape(string8cols);
weights = 2.^(0:7);
zipped = unit8(weights*double(string));
%存储一个稀疏矩阵
huffcodes = sparse(11); % init sparse matrix
for index = 1:numel(codeword)
huffcodes(codeword(index)1) = simbols(index);
end
%产生信息结构体
info.pad = pad;
info.ratio = cols./length(vector);
info.length = length(vector);
info.maxcodelen = maxcodelen;
%%%%%%%%%% addnode %%%%%%%%%%
function codeword_new = addnode(codeword_olditem)
code
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4775 2008-12-16 18:41 huffman.m
文件 2042 2008-12-16 18:43 JPEG.m
文件 363 2008-12-16 18:43 Rum Length Encoding.m
文件 1216 2008-12-16 18:42 suanshubianma.m
----------- --------- ---------- ----- ----
8396 4
相关资源
- matlab小波变换合集
- 最大后验概率 MAP 准则matlab源码
- 非线性系统辨识
- 异步电机matlab仿真
- matlab 数字升余弦仿真
- 多重网格法求解微分方程-matlab
- 批量读取温度数据
- MATLAB实现DPCM编码
- lz复杂度计算的matlab程序
- 最小二乘法Matlab代码
- matlab 程序\\\\多项式拟合插值.rar
- matlab水准网平差
- 关于汽车的制动性的MAtlab仿真
- 使用Matlab和CCS设计FIR滤波器
- 用matlab语言实现编解码
- 模拟退火算法进行函数优化 matlab
- 通信原理仿真实验 matlab
- 数学物理方程大作业 matlab
- VIBE-MEX--
- 复化Simpson公式
- 基于MATLAB的模糊控制表计算程序.m
- BCH matlab 编码
- matlab guide 选择文件夹
- Matlab_2013_64bit支持VS2013 的配置文件
- 基于视频的matlab光流法
- matlab串口实时波形显示
- matlab神经网络、模糊和LQR控制一级、
- 一级倒立摆神经网络控制
- LK光流法MATLAB代码
- matlab串口实时显示波形。
评论
共有 条评论