MATLAB Answers


how can i do huffman encoding in image compression

Asked by omar
on 8 Dec 2012
Latest activity Commented on by shabu sathyadhas on 22 Mar 2016

hi,i am doing lossy image compression using discrete cosine transform i had done all the steps of the compression(dct then quantization then zigzag scan) now i have a vector and i want to do huffman encoding i know that the code as follows

 [dict,avglen] = huffmandict(symbols,p) 
 comp = huffmanenco(sig,dict)

i am asking now how to get the symbol and p(probability) from the large vector that i have to do the encoding

  1 Comment

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

%Huffman Decoding dhsig1 = huffmandeco(hcode,dict);

%convertign dhsig1 double to dhsig uint8 dhsig = uint8(dhsig1);

%vector to array conversion dec_row=sqrt(length(dhsig)); dec_col=dec_row;

%variables using to convert vector 2 array arr_row = 1; arr_col = 1; vec_si = 1;

for x = 1:m for y = 1:n back(x,y)=dhsig(vec_si); arr_col = arr_col+1; vec_si = vec_si + 1; end arr_row = arr_row+1; end



1 Answer

Answer by Walter Roberson
on 8 Dec 2012
 Accepted answer

If your vector is uint8() then one way of doing it is

symbols = unique(YourVector(:));
counts = hist(YourVector(:), symbols);
p = double(counts) ./ sum(counts);

This is not the only way.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!