2011-12-25 308 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)