圖像程序-霍夫曼編碼_第1頁
圖像程序-霍夫曼編碼_第2頁
圖像程序-霍夫曼編碼_第3頁
圖像程序-霍夫曼編碼_第4頁
圖像程序-霍夫曼編碼_第5頁
已閱讀5頁,還剩2頁未讀 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領

文檔簡介

1、赫夫曼編碼設計原理赫夫曼(Huffman)編碼是1952年提出的,是一種比較經(jīng)典的信息無損熵編碼,該編碼依據(jù)變長最佳編碼定理,應用Huffman算法而產(chǎn)生。Huffman編碼是一種基于統(tǒng)計的無損編碼。設信源X的信源空間為:其中,現(xiàn)用二進制對信源X中的每一個符號(i=1,2,N)進行編碼。根據(jù)變長最佳編碼定理,Huffman編碼步驟如下:(1)將信源符號xi按其出現(xiàn)的概率,由大到小順序排列。(2)將兩個最小的概率的信源符號進行組合相加,并重復這一步驟,始終將較大的概率分支放在上部,直到只剩下一個信源符號且概率達到1.0為止;(3)對每對組合的上邊一個指定為1,下邊一個指定為0(或相反:對上邊一個

2、指定為0,下邊一個指定為1);(4)畫出由每個信源符號到概率1.0處的路徑,記下沿路徑的1和0;(5)對于每個信源符號都寫出1、0序列,則從右到左就得到非等長的Huffman碼。Huffman編碼的特點是:(1)Huffman編碼構造程序是明確的,但編出的碼不是唯一的,其原因之一是兩個概率分配碼字“0”和“1”是任意選擇的(大概率為“0”,小概率為“1”,或者反之)。第二原因是在排序過程中兩個概率相等,誰前誰后也是隨機的。這樣編出的碼字就不是唯一的。(2)Huffman編碼結果,碼字不等長,平均碼字最短,效率最高,但碼字長短不一,實時硬件實現(xiàn)很復雜(特別是譯碼),而且在抗誤碼能力方面也比較差。

3、(3)Huffman編碼的信源概率是2的負冪時,效率達100%,但是對等概率分布的信源,產(chǎn)生定長碼,效率最低,因此編碼效率與信源符號概率分布相關,故Huffman編碼依賴于信源統(tǒng)計特性,編碼前必須有信源這方面的先驗知識,這往往限制了哈夫曼編碼的應用。(4)Huffman編碼只能用近似的整數(shù)位來表示單個符號,而不是理想的小數(shù),這也是Huffman編碼無法達到最理想的壓縮效果的原因。設計程序clearload woman; %讀入圖像數(shù)據(jù)%X=imread('girl.bmp','bmp');data=uint8(X);zipped,info=huffencode(

4、data); %調(diào)用Huffman編碼程序進行壓縮unzipped=huffdecode(zipped,info,data);%調(diào)用Huffman編碼程序進行解碼%顯示原始圖像和經(jīng)編碼后的圖像,顯示壓縮比,并計算均方根誤差得erms=0,表示是Huffman是無失真編碼subplot(121);imshow(data);subplot(122);imshow(unzipped);%erms=compare(data(:),unzipped(:)cr=info.ratiowhos data unzipped zipped%huffencode函數(shù)對輸入矩陣vector進行Huffman編碼,返回

5、%編碼后的向量(壓縮后數(shù)據(jù))及相關信息function zipped,info=huffencode(vector)%輸入和輸出都是unit8格式%info返回解碼需要的機構信息%info.pad是添加的比特數(shù)%info.huffcodes是Huffman碼字%info.rows是原始圖像行數(shù)%info.cols是原始圖像行數(shù)%info.length是原始圖像數(shù)據(jù)長度%info.maxcodelen是最長碼長if isa(vector,'uint8') error('input argument must be a uint8 vector');endm,n=s

6、ize(vector);vector=vector(:)'f=frequency(vector); %計算各符號出現(xiàn)的概率(調(diào)用frequency)symbols=find(f=0);f=f(symbols);f,sortindex=sort(f); %將符號按照出現(xiàn)的概率大小排序symbols=symbols(sortindex);len=length(symbols);symbols_index=num2cell(1:len);codeword_tmp=cell(len,1);while length(f)>1 %生產(chǎn)Huffman樹,得到碼字編碼表 index1=symbo

7、ls_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(3:end); f,sortindex=sort(f); symbols_index=symbols_index(sortindex);endcodeword=c

8、ell(256,1);codeword(symbols)=codeword_tmp;len=0;for index=1:length(vector) %得到整個圖像所有比特數(shù) len=len+length(codeworddouble(vector(index)+1);endstring=repmat(uint8(0),1,len);pointer=1;for index=1:length(vector) %對輸入圖像進行編碼 code=codeworddouble(vector(index)+1; len=length(code); string(pointer+(0:len-1)=code

9、; pointer=pointer+len;endlen=length(string);pad=8-mod(len,8); %非8整數(shù)倍時,最后補pad個0if pad>0 string=string uint8(zeros(1,pad);endcodeword=codeword(symbols);codelen=zeros(size(codeword);weights=2.(0:23);maxcodelen=0;for index=1:length(codeword) len=length(codewordindex); if len>maxcodelen maxcodelen=

10、len; end if len>0 code=sum(weights(codewordindex=1); code=bitset(code,len+1); codewordindex=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);for ind

11、ex=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函數(shù)對輸入矩陣vector進行Huffman編碼,%返回解壓后的圖像數(shù)據(jù)function vector=huffdecode

12、(zipped,info,image)ifisa(zipped,'uint8') error('input argument must be a uint8 vector');end%產(chǎn)生0,1序列,每位占一個字節(jié)len=length(zipped);string=repmat(uint8(0),1,len.*8);bitindex=1:8;for index=1:lenstring(bitindex+8.*(index-1)=uint8(bitget(zipped(index),bitindex);endstring=logical(string(:)'

13、;);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;

14、vectorindex=vectorindex+1; endend%vector=reshape(vector,info.rows,info.cols);%函數(shù)addnode添加節(jié)點function codeword_new=addnode(codeword_old,item)codeword_new=cell(size(codeword_old);for index=1:length(codeword_old) codeword_newindex=item codeword_oldindex;end%函數(shù)frequency計算各符號出現(xiàn)的概率function f=frequency(vect

15、or)ifisa(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;%函數(shù)decode返回碼字對應的符號function byte=decode(code,info)byte=info.huffcodes(code);(1)對圖像 woman 進行編碼cr = 0.6291 Name

16、Size Bytes Class Attributes data 256x256 65536 uint8 unzipped 1x65537 65537 uint8 zipped 1x41226 41226 uint8 (2) 對圖像 cameraman.tif 進行編碼cr = 0.8806 Name Size Bytes Class Attributes data 256x256 65536 uint8 unzipped 1x65537 65537 uint8 zipped 1x57712 57712 uint8 cr =0.8708 Name Size Bytes Class Attributes data 409x541x3 66380

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論