【问题标题】:MATLAB - image huffman encodingMATLAB - 图像哈夫曼编码
【发布时间】:2012-06-25 20:01:13
【问题描述】:

我有一个作业,我必须将一些图像转换为灰度并使用霍夫曼编码对其进行压缩。我将它们转换为灰度,然后尝试压缩它们,但出现错误。我使用了我找到的代码here

这是我正在使用的代码:

A=imread('Gray\36.png');
[symbols,p]=hist(A,unique(A))
p=p/sum(p)
[dict,avglen]=huffmandict(symbols,p)
comp=huffmanenco(A,dict)

这是我得到的错误。它出现在第二行。

Error using eps
Class must be 'single' or 'double'.

Error in hist (line 90)
    bins = xx + eps(xx);

我做错了什么?

谢谢。

附:如何找到每张图片的压缩比?

【问题讨论】:

    标签: image matlab huffman-code


    【解决方案1】:

    问题在于,当您指定 bin 位置('hist' 的第二个输入参数)时,它们需要是单数或双数。但是,向量 A 本身没有。这很好,因为有时您不想将整个数据集从整数类型转换为浮点精度。这将修复您的代码:

    [symbols,p]=hist(A,double(unique(A)))
    

    Click here看这个问题有更详细的讨论。

    【讨论】:

      【解决方案2】:

      首先,尝试:

      whos A
      

      似乎它的类型必须是单或双。如果没有,只需在 imread 行之后执行 A = double(A) 即可。应该这样工作,但我很惊讶 hist 没有进行转换...... [编辑] 我刚刚测试过,我是对的,hist 在 uint8 中不起作用,但是只要我将图像转换为 double 就可以了。

      【讨论】:

      • 我做到了。这是代码A=imread('Gray\36.png'); A = double(A); [symbols,p]=hist(A,unique(A)) p=p/sum(p) [dict,avglen]=huffmandict(symbols,p) comp=huffmanenco(A,dict),现在我得到了错误,Error using huffmandict (line 71) The symbol input must be a vector
      • 符号不是向量。实际上, hist 为您提供 n 每行中的频率计数,并 xout bin 位置(即灰度级数)。它们是二维的。因此,据我了解,只需将符号中的值分别汇总到其列中,即可为您提供所需的向量。猜测:symbols=symbols/sum(symbols) ;)
      • 我是这样做的,它确实有效,但它真的很慢I=imread('Gray\36.png'); [m,n]=size(I); Totalcount=m*n; cnt=1; sigma=0; %computing the cumulative probability. for i=0:255 k=I==i; count(cnt)=sum(k(:)); %pro array is having the probabilities pro(cnt)=count(cnt)/Totalcount; sigma=sigma+pro(cnt); cumpro(cnt)=sigma; cnt=cnt+1; end; symbols = [0:255]; %Huffman code Dictionary dict = huffmandict(symbols,pro); %function which converts array to vector vec_size = 1; for p = 1:m for q = 1:n newvec(vec_size) = I(p,q); vec_size = vec_size+1; end end %Huffman Encodig hcode = huffmanenco(newvec,dict);
      • 你试过我写的吗?老实说,我想知道这是否不仅仅是 p 和符号之间的混淆。我会自己尝试的。
      • 我刚试过。我得到一个错误。它说 Source 符号在 huffmandict 中重复。 :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 1970-01-01
      相关资源
      最近更新 更多