Matlab,图像压缩

mee*_*ena 3 matlab huffman-code

我不确定这要求我在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)
Run Code Online (Sandbox Code Playgroud)

Aab*_*baz 8

你了解霍夫曼编码的原理吗?

简而言之,它是一种用于压缩数据的算法(如您的情况下的图像).这意味着算法的输入是一个图像,输出是一个数字代码,其大小小于输入:因此压缩.

霍夫曼编码的原理(大致)用原始数据中的符号(在您的情况下是图像的每个像素的值)替换根据符号概率归因的数字代码.最可能的(即最常见的)符号将被较短的代码替换,以便实现数据的压缩.

为了解决您的问题,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)
Run Code Online (Sandbox Code Playgroud)