Matlab,图像压缩 [英] Matlab, Image compression

查看:243
本文介绍了Matlab,图像压缩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

解决方案

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

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

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

为解决您的问题,Matlab在通讯工具箱"中具有两个功能:huffmandicthuffmanenco.

huffmandict::此函数可建立字典,该字典用于将符号从原始数据转换为数字霍夫曼代码字.要构建此词典,huffmandict需要数据中使用的符号列表以及它们出现的概率,该概率是使用它们的时间除以数据中符号的总数.

huffmanenco::此功能用于通过使用huffmandict内置的词典来翻译原始数据.原始数据中的每个符号都转换为数字霍夫曼代码.要测量此压缩方法的大小增益,可以计算压缩率,该压缩率是用于描述原始数据的位数与霍夫曼对应代码的位数之间的比率.在您的情况下,从对压缩率的计算中可以得出,您有一个8 x 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)

i am unsure about what this is asking me to do in matlab? what does it mean to encode? what format should the answer be? can anyone help me to work it out please? Encode the 8x8 image patch and print out the results

I have got an 8X8 image

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)

解决方案

Do you understand the principle of Huffman coding?

To put it simply, it is an algorithm used to compress data (like images in your case). This means that the input of the algorithm is an image and the output is a numeric code that is smaller in size than the input: hence the compression.

The principle of Huffman coding is (roughly) to replace symbols in the original data (in your case the value of each pixel of the image) by a numeric code that is attributed according to the probability of the symbol. The most probable (i.e. the most common) symbol will be replaced by shorter codes in order to realize a compression of the data.

To solve your problem, Matlab has two functions in the Communications Toolbox: huffmandict and huffmanenco.

huffmandict: this function build a dictionary that is used to translate symbols from the original data to their numeric Huffman codewords. To build this dictionary, huffmandict needs the list of symbols used in the data and their probability of appearance which is the number of time they are used divided by the total number of symbols in your data.

huffmanenco: this function is used to translate your original data by using the dictionary built by huffmandict. Each symbol in the original data is translated to a numeric Huffman code. To measure the gain in size of this compression method, you can compute the compression ration, which is the ratio between the number of bits used to describe your original data and the number of bits of the Huffman corresponding code. In your case, infering from your computation of the compression ratio, you have an 8 by 8 image using 8 bits integer to describe each pixel, and the Huffman corresponding code uses length(comp) bits.

With all this in mind, you could read your code in this way:

% 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)

这篇关于Matlab,图像压缩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆