【问题标题】:Matlab, Image compressionMatlab,图像压缩
【发布时间】:2011-12-25 19:05:27
【问题描述】:

我不确定这要求我在 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)

【问题讨论】:

  • 亲爱的@meena,请问有什么问题?

标签: matlab huffman-code


【解决方案1】:

你了解Huffman coding的原理吗?

简而言之,它是一种用于压缩数据的算法(例如您的图像)。这意味着算法的输入是图像,输出是比输入小的数字代码:因此进行了压缩。

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

为了解决您的问题,Matlab 在通信工具箱中有两个函数:huffmandicthuffmanenco

huffmandict: 此函数构建一个字典,用于将符号从原始数据转换为其数字霍夫曼码字。要构建此字典,huffmandict 需要数据中使用的符号列表及其出现概率,即使用它们的次数除以数据中的符号总数。

huffmanenco:此功能用于使用huffmandict构建的字典翻译您的原始数据。原始数据中的每个符号都被转换为数字霍夫曼码。要衡量这种压缩方法的大小增益,您可以计算压缩比,即用于描述原始数据的位数与 Huffman 相应代码的位数之间的比率。在您的情况下,根据您对压缩比的计算推断,您有一个 8 x 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)

【讨论】:

    猜你喜欢
    • 2012-11-16
    • 2014-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-29
    • 1970-01-01
    • 2013-08-07
    相关资源
    最近更新 更多