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

Huffman coding and decoding for image(JPEG, BMP)

Asked by Manisha Mehra on 28 Feb 2011
Latest activity Commented on by fateh on 9 Nov 2014

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.


Manisha Mehra


No products are associated with this question.

6 Answers

Answer by Paulo Silva on 28 Feb 2011
Accepted answer


Manisha Mehra on 1 Mar 2011

It worked awesome.

fateh on 9 Nov 2014

how could i run this example

Paulo Silva
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));


sailaja on 31 Jan 2014

how to use this code for an image

Walter Roberson on 31 Jan 2014

Change the input() call to,

filename = input('enter the image file name', 's');
string = imread(filename);
string = string(:);
projecte on 26 Apr 2014

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

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

Walter Roberson on 19 May 2012

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

slama najla
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


slama najla
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

Walter Roberson on 20 May 2012

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.

slama najla
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


slama najla

Contact us