1

I have a homework in which i have to convert some images to grayscale and compress them using huffman encoding. I converted them to grayscale and then i tried to compress them but i get an error. I used the code i found here.

Here is the code i'm using:

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

This is the error i get. It occurs at the second line.

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

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

What am i doing wrong?

Thanks.

P.S. how can i find the compression ratio for each image?

Community
  • 1
  • 1
Christos Baziotis
  • 5,845
  • 16
  • 59
  • 80

2 Answers2

2

The problem is that when you specify the bin locations (the second input argument of 'hist'), they need to be single or double. The vector A itself does not, though. That's nice because sometimes you don't want to convert your whole dataset from an integer type to floating precision. This will fix your code:

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

Click here to see this issue is discussed more in detail.

Nathan
  • 1,135
  • 2
  • 12
  • 27
0

first, try :

whos A

Seems like its type must be single or double. If not, just do A = double(A) after the imread line. Should work that way, however I'm surprised hist is not doing the conversion... [EDIT] I have just tested it, and I am right, hist won't work in uint8, but it's okay as soon as I convert my image to double.

slayton
  • 20,123
  • 10
  • 60
  • 89
CTZStef
  • 1,675
  • 2
  • 18
  • 47
  • i did that. Here is the code `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)` and now i get the error, `Error using huffmandict (line 71) The symbol input must be a vector` – Christos Baziotis Jun 25 '12 at 20:11
  • symbols is not a vector. Actually, hist gives you n the frequency counts in each row, and xout the bin location (i.e. the number of gray levels). They are bidimensional. So as I understand it, just sum up the values in symbols respectively to its columns, that will give you the vector you want. GUESS : symbols=symbols/sum(symbols) ;) – CTZStef Jun 25 '12 at 20:23
  • i did it this way and it works but it is really slow `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);` – Christos Baziotis Jun 25 '12 at 20:35
  • did you try what I wrote ? Honestly I am wondering if this is not just a confusion between p and symbols. Will try it on my own. – CTZStef Jun 25 '12 at 20:38
  • i just tried it. i get an error. It says that Source symbols repeat in huffmandict. :( – Christos Baziotis Jun 25 '12 at 20:39
  • Yes I just got that too... sorry sijoune I can't help you any further, I don't really have time right now and can't focus on your problem as much as I would like so I rather give in. Good luck! (I was right about double at least remember that ;) ) – CTZStef Jun 25 '12 at 20:43