資源簡介
霍夫曼,算術,行程,jpg 圖像壓縮在matlab下的源代碼

代碼片段和文件信息
clear?all
fprintf(‘Reading?data...‘)
data?=?imread(‘cameraman.tif‘);
data?=?uint8(data);?%讀入數(shù)據(jù)并將數(shù)據(jù)限制為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);
%計算各元素出現(xiàn)的概率
simbols?=?find(f~=0);
f?=?f(simbols);?%將元素按照出現(xiàn)的概率排列
[fsortindex]?=?sort(f);
simbols?=?simbols(sortindex);
%產(chǎn)生碼字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)];
%將數(shù)據(jù)重新排列使兩個節(jié)點的頻率盡量與前一個節(jié)點的頻率相當
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
%產(chǎn)生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
%產(chǎn)生信息結構體
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
評論
共有 條評論