astralreb
astralreb

Reputation: 37

Create image from Huffman code - Matlab

I have a project about image compression in Matlab. So far i have successfully implemented Huffman encoding to the image which gives me a vector of binary codes. After that i run Huffman decoding and i get a vector which contains the elements of the image compressed. My problem is that i can find how is possible from this vector to reconstruct the image and create the image file.

Any help would be grateful

Update

Based on Ben A. help i have made progress but i still have some issues. To be more exact. I have an image matrix. After finding unique symbols(elements) on this image matrix, i calculate the probabilities and then with this function:

function [h,L,H]=Huffman_code(p,opt)
% Huffman code generator gives a Huffman code matrix h, 
%  average codeword length L & entropy H
% for a source with probability vector p given as argin(1) 
zero_one=['0'; '1']; 
if nargin>1&&opt>0, zero_one=['1'; '0']; end
if abs(sum(p)-1)>1e-6
  fprintf('\n The probabilities in p does not add up to 1!');
end  
M=length(p);  N=M-1; p=p(:); % Make p a column vector
h={zero_one(1),zero_one(2)};
if M>2
  pp(:,1)=p;
  for n=1:N
     % To sort in descending order
     [pp(1:M-n+1,n),o(1:M-n+1,n)]=sort(pp(1:M-n+1,n),1,'descend'); 
     if n==1, ord0=o; end  % Original descending order
     if M-n>1, pp(1:M-n,n+1)=[pp(1:M-1-n,n); sum(pp(M-n:M-n+1,n))]; end
  end
  for n=N:-1:2
     tmp=N-n+2; oi=o(1:tmp,n);
     for i=1:tmp, h1{oi(i)}=h{i}; end
     h=h1;   h{tmp+1}=h{tmp};
     h{tmp}=[h{tmp} zero_one(1)]; 
     h{tmp+1}=[h{tmp+1} zero_one(2)];
  end
  for i=1:length(ord0), h1{ord0(i)}=h{i}; end
  h=h1;
end
L=0; 
for n=1:M, L=L+p(n)*length(h{n}); end  % Average codeword length
H=-sum(p.*log2(p)); % Entropy by Eq.(9.1.4)

i calculate the huffman codes for the image.

Now i use this function:

function coded_seq=source_coding(src,symbols,codewords)
% Encode a data sequence src based on the given (symbols,codewords).
no_of_symbols=length(symbols);  coded_seq=[];
if length(codewords)<no_of_symbols
  error('The number of codewords must equal that of symbols');
end
for n=1:length(src)
   found=0;
   for i=1:no_of_symbols
      if src(n)==symbols(i), tmp=codewords{i}; found=1; break; end
   end
   if found==0, tmp='?'; end
   coded_seq=[coded_seq tmp];
end

where in src i put my image (matrix) and i get a coded sequence for my image.

Last is this function:

function decoded_seq=source_decoding(coded_seq,h,symbols)
% Decode a coded_seq  based on the given (codewords,symbols).
M=length(h);  decoded_seq=[];
while ~isempty(coded_seq)
  lcs= length(coded_seq); found=0;
  for m=1:M
    codeword= h{m};
    lc= length(codeword);
    if lcs>=lc&codeword==coded_seq(1:lc)
      symbol=symbols(m); found=1;  break;
    end
    if found==0, symbol='?'; end
  end 
  decoded_seq=[decoded_seq symbol]; 
  coded_seq=coded_seq(lc+1:end); 
end

Which is used to decode the coded sequence. The problem is that finally as coded sequence i get a 1x400 matrix where i should get a 225x400 which is my image dimensions. Am i missing something? Maybe should i replace something because i have a matrix and not a number sequence (for which the code is written)?

Upvotes: 0

Views: 11424

Answers (1)

Related Questions