首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Matlab,图像压缩

Matlab,图像压缩
EN

Stack Overflow用户
提问于 2011-12-25 19:05:27
回答 1查看 8.4K关注 0票数 3

我不知道这是要我在matlab做什么?编码是什么意思?答案应该是什么形式?有人能帮我解决这个问题吗?对8x8图像补丁进行编码并打印结果

我有一张8X8的照片

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2011-12-26 08:54:57

你了解Huffman coding的原理吗?

简单地说,它是一种用于压缩数据的算法(就像您的例子中的图像)。这意味着算法的输入是图像,输出是比输入小的数字代码:因此是压缩。

Huffman编码的原则是(粗略地)将原始数据中的符号(在您的例子中是图像的每个像素的值)替换为一个数字代码,该数字代码根据符号的概率来赋值。为了实现对数据的压缩,最可能的(即最常见的)符号将被较短的代码所取代。

为了解决您的问题,Matlab在Communications中有两个函数:huffmandicthuffmanenco

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

huffmanenco**:** --这个函数通过使用huffmandict构建的字典来转换原始数据。原始数据中的每个符号都被转换成一个数字Huffman代码。要测量这种压缩方法的增益大小,可以计算压缩比,即用于描述原始数据的比特数与对应代码的哈夫曼比特数之间的比率。在您的例子中,根据压缩比的计算,您有一个8乘8的图像,使用8位整数来描述每个像素,而Huffman对应的代码使用length(comp)位。

考虑到所有这些,您可以这样阅读代码:

代码语言:javascript
复制
% 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)
票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8631044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档