2011-12-25 310 views
3

我不確定這是要求我在matlab中做什麼?這意味着什麼編碼?答案是什麼格式?任何人都可以幫我解決它嗎? 編碼8x8的圖像塊,並打印出結果Matlab,圖像壓縮

我有一個8x8的圖像

symbols=[0 20 50 99]; 
p=[32 8 16 8]; 
p = p/sum(p); 
[dict, avglen] = huffmandict(symbols, p); 
A = ... 
[99 99 99 99 99 99 99 99 ... 
20 20 20 20 20 20 20 20 ... 
0 0 0 0 0 0 0 0 ... 
0 0 50 50 50 50 0 0 ... 
0 0 50 50 50 50 0 0 ... 
0 0 50 50 50 50 0 0 ... 
0 0 50 50 50 50 0 0 ... 
0 0 0 0 0 0 0 0]; 
comp=huffmanenco(A,dict); 
ratio=(8*8*8)/length(comp) 
+0

親愛@meena,有什麼問題嗎? –

回答

8

你明白的Huffman coding原則?

簡而言之,它是一種用於壓縮數據的算法(如您的案例中的圖像)。這意味着算法的輸入是圖像,輸出是比輸入小的數字代碼:因此是壓縮。

霍夫曼編碼原理(大致)是用數字代碼替換原始數據中的符號(在你的情況下是圖像中每個像素的值),數字代碼是根據符號的概率歸因的。爲了實現數據的壓縮,最可能的(即最常見的)符號將被較短的代碼所取代。

爲了解決您的問題,Matlab在Communications Toolbox中有兩個功能:huffmandicthuffmanenco

huffmandict該函數構建一個字典,用於將符號從原始數據轉換爲其數字霍夫曼代碼字。要建立這本詞典,huffmandict需要數據中使用的符號列表及其出現概率,即它們的使用次數除以數據中符號的總數。

huffmanenco該功能用於翻譯您的原始數據,使用由huffmandict構建的字典。原始數據中的每個符號都被轉換爲數字霍夫曼編碼。要測量此壓縮方法的增益大小,可以計算壓縮比率,即用於描述原始數據的位數與霍夫曼相應代碼的位數之間的比率。在你的情況下,根據你對壓縮比的計算來推斷,你有一個8乘8的圖像,用8位整數來描述每個像素,霍夫曼相應的代碼使用length(comp)位。

考慮到這一點的所有,你可以用這種方式閱讀你的代碼:

% Original image 
A = ... 
[99 99 99 99 99 99 99 99 ... 
20 20 20 20 20 20 20 20 ... 
0 0 0 0 0 0 0 0 ... 
0 0 50 50 50 50 0 0 ... 
0 0 50 50 50 50 0 0 ... 
0 0 50 50 50 50 0 0 ... 
0 0 50 50 50 50 0 0 ... 
0 0 0 0 0 0 0 0]; 

% First step: extract the symbols used in the original image 
% and their probability (number of occurences/number of total symbols) 
symbols=[0 20 50 99]; 
p=[32 8 16 8]; 
p=p/sum(p); 
% To do this you could also use the following which automatically extracts 
% the symbols and their probability 
[symbols,p]=hist(A,unique(A)); 
p=p/sum(p); 

% Second step: build the Huffman dictionary 
[dict,avglen]=huffmandict(symbols,p); 

% Third step: encode your original image with the dictionary you just built 
comp=huffmanenco(A,dict); 

% Finally you can compute the compression ratio 
ratio=(8*8*8)/length(comp)