MATLAB Answers


Huffman coding and decoding for image(JPEG, BMP)

Asked by Manisha Mehra on 28 Feb 2011
Latest activity Commented on by Walter Roberson
on 12 Dec 2016

I have to implement huffman encoding and decoding for a '.bmp' image without using the inbuilt matlab function like huffmandict, huffmanenco and huffmandeco.

Can anybody help me by sending me the source code?

Moreover, I have done the encoding part but I am not able to do the decoding. I have no idea as how to reconstruct the image through decoding.


Show 1 older comment

That is possible. You need to store the code dictionary as well as the encoding. With 256 different symbols possible, unless at least half of them are never used, you can be sure that some of the encodings will be at least 8 bits, the original length. When the probabilities are about equally distributed each of the symbols would come out as 8 bits, so saving nothing for the encoded part. But you need to save the dictionary as well and that takes room.

Huffman encoding is a lossless encoding, so you need to have as much "information" stored in the encoded version as in the unencoded version. It doesn't begin to save space on the encoding until some of the symbols are at least twice as probable as some of the others or at least half the potential symbols are never unused, which are situations that would allow it to save 1 bit per occurrence. Those bits saved have to add up to the size of the saved dictionary before you get any net savings on the storage.

Efficient storage of the dictionary takes some thought since each of the entries is a variable number of bits -- the bit pattern and the symbol it decodes to. And remember to account for the end-of-stream marker or some other method of indicating where the end of the stream of bits is, since it will not generally be at a byte boundary so you can't tell by end-of-file.

I have to implement huffman encoding and decoding for a 'jpeg' image using C or C++. Please anyone help me by sending the source code?

  1. this is a resource for MATLAB questions not C or C++
  2. there are already a lot of public sources for that task including libjpeg
  3. if we send you source then you would not have implemented it. Would we get the academic credit or would you?

Log in to comment.


No products are associated with this question.

6 Answers

Answer by Falak
on 4 May 2012

Try this one... just change to your required image

%Author Name:Falak Shah
%Target: To huffman encode and decode user entered string
string=input('enter the string in inverted commas');         %input string
symbol=[];                                %initialise variables
%------------------------------------------loop to separate symbols and how many times they occur
for i=1:length(string)                   
  flag=ismember(symbol,string(i));      %symbols
      if sum(flag)==0
      symbol(j) = string(i);
      c=sum(k);                         %no of times it occurs  
      count(j) = c;
total=sum(count);                         %total no of symbols
%-----------------------------------------for loop to find probability and
for i=1:1:size((count)');                   
%-----------------------------------------function to create dictionary
[dict avglen]=huffmandict(symbol,prob);    
% print the dictionary.
         temp = dict;
         for i = 1:length(temp)
         temp{i,2} = num2str(temp{i,2});
         var=var+(length(dict{i,2})-avglen)^2;  %variance calculation
%-----------------------------------------encoder  and decoder functions           
equal = isequal(string,deco)
%-----------------------------------------decoded string and output
str ='';
for i=1:length(deco)    
str= strcat(str,deco(i));


Show 1 older comment

how to use this code for an image

Change the input() call to,

filename = input('enter the image file name', 's');
string = imread(filename);
string = string(:);

hi how can i decode a image from a secret image in the same format

Log in to comment.

Answer by slama najla on 19 May 2012

Hello please is that you can help me? is that you can send me your registration code files before you get the picture compressed as I encounter the same problem as let go of compressed image is a very superior picture framer and my original remark that the fault is in the recording files. thank you in advance

  1 Comment

The dictionary is dependent on the image being encoded. Having the dictionary for a different image would not help you.

Log in to comment.

Answer by slama najla on 19 May 2012

good evening; is that you can help me? I realize my enthusiasm for my masters research topic is "medical image compression". the outcome of my application is as follows: I break my image into 4 subbands LL, HH, HL and LH. then, I applied on lossless jpeg LL and DPCM of HH, HL and LH. thereafter, I apply reading and zigzag across the huffman bandes.Mais the problem that I can not find how to record all this data in order to have the compressed image size. is that you can help me to find a matlab code to record all this data in order to have the compressed image size. thank you in advance for helping me


Log in to comment.

Answer by slama najla on 20 May 2012

me problem is in this partie. I find the size of compressed image is very superior original image

% Alog Mettre le code de 0101010 ... de Dans un Vecteur de 8 bits par l'élement de% Erreur L2 = longueur (CODEH);

LCDH = round (L2 / 8);

si mod (L2, 8) <4

   si mod (L2, 8) ~ = 0
      LCDH = LCDH +1;

LcdnH = (LCDH) * 8;

DIFLcdH = (LcdnH)-L2;

codnH = zeros (1, LcdnH);

k = 1; pour i = 1: L2; codnH (k) = CODEH (i);

    k = k +1;

Tcod8H = zeros (1, la LCDH);

k = 1;

pour i = 1: la LCDH

   pour j = 00:07
Tcod8H (i) = (codnH (k + j)) * (2 ^ j) + Tcod8H (i);
k = k +8;

LTH = longueur (Tcod8H);

LXF = longueur (XfuH);

A = []; P = []; B = [];

k = 1, ii = 1; jj = 1;

pour i = 1: si LXF XfuH (i)> 255

   temp = abais (XfuH (i));
   P (ii) = i;
     pour j = 1:2
   A (k) = temp (j);
   k = k +1;
   ii = ii 1;
      B (jj) = XfuH (i);
      jj = jj +1;
l = longueur (A);
h01 = redimensionnement (LXF);

% H03 = redimensionnement (LT);

H02 = redimensionnement (l);

H04 =% redimensionnement (T);

% H05 = redimensionnement (LXfu);

Fichier = [ABP h01 H02 Tcod8H DIFLcdH taille taille1 taille2 taille3];

 Taux_de_compression% = MX * NX / longueur (fichier);
 Gain_de_compression% = 100 * (1-Taux_de_compression)
 Gain_de_compression% = 100 * (1-1/Taux_de_compression);
f = fopen ('compressionnnnn irm with prédiction.comp', 'w');

fwrite (f, Fichier, 'ubit8');

fclose (f);

  1 Comment

Please put all this (including the earlier one) code together into a new Question and remove it here, as your question does not have much to do with the Question here.

Or instead of a new Question, you should edit your Question to have all the information you have provided here.

Log in to comment.

Answer by slama najla on 20 May 2012

thank you to your answer. my question is to be applied after the DWT of the image and lossless jpeg on my LL and DPCM code is the 3 sub-bands LH, HL and HH. and thereafter the huffman encoder on four sub bandes.enfin when I save the image with this code I get a compressed image is much higher than original image. pleaze is that it is possible to send me a registration code? thank you to help your


Log in to comment.

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!