我不知道这是要我在matlab做什么?编码是什么意思?答案应该是什么形式?有人能帮我解决这个问题吗?对8x8图像补丁进行编码并打印结果
我有一张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)发布于 2011-12-26 08:54:57
你了解Huffman coding的原理吗?
简单地说,它是一种用于压缩数据的算法(就像您的例子中的图像)。这意味着算法的输入是图像,输出是比输入小的数字代码:因此是压缩。
Huffman编码的原则是(粗略地)将原始数据中的符号(在您的例子中是图像的每个像素的值)替换为一个数字代码,该数字代码根据符号的概率来赋值。为了实现对数据的压缩,最可能的(即最常见的)符号将被较短的代码所取代。
为了解决您的问题,Matlab在Communications中有两个函数:huffmandict和huffmanenco。
huffmandict**:**此函数构建一个字典,用于将符号从原始数据转换为它们的数值Huffman代码词。要构建此词典,huffmandict需要数据中使用的符号列表及其出现的概率,即它们使用的时间除以数据中的符号总数。
huffmanenco**:** --这个函数通过使用huffmandict构建的字典来转换原始数据。原始数据中的每个符号都被转换成一个数字Huffman代码。要测量这种压缩方法的增益大小,可以计算压缩比,即用于描述原始数据的比特数与对应代码的哈夫曼比特数之间的比率。在您的例子中,根据压缩比的计算,您有一个8乘8的图像,使用8位整数来描述每个像素,而Huffman对应的代码使用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)https://stackoverflow.com/questions/8631044
复制相似问题