Asked by Manisha Mehra
on 28 Feb 2011

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.

*No products are associated with this question.*

Answer by Paulo Silva
on 28 Feb 2011

Accepted answer

Manisha Mehra
on 1 Mar 2011

Thanks..

It worked awesome.

fateh
on 9 Nov 2014

how could i run this example

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 count=[]; j=1; %------------------------------------------loop to separate symbols and how many times they occur for i=1:length(string) flag=0; flag=ismember(symbol,string(i)); %symbols if sum(flag)==0 symbol(j) = string(i); k=ismember(string,string(i)); c=sum(k); %no of times it occurs count(j) = c; j=j+1; end end ent=0; total=sum(count); %total no of symbols prob=[]; %-----------------------------------------for loop to find probability and %entropy for i=1:1:size((count)'); prob(i)=count(i)/total; ent=ent-prob(i)*log2(prob(i)); end var=0; %-----------------------------------------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 end temp %-----------------------------------------encoder and decoder functions sig_encoded=huffmanenco(string,dict) deco=huffmandeco(sig_encoded,dict); equal = isequal(string,deco) %-----------------------------------------decoded string and output %variables str =''; for i=1:length(deco) str= strcat(str,deco(i)); end str ent avglen var

Show 1 older comment

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

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.

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

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; fin fin

LcdnH = (LCDH) * 8;

DIFLcdH = (LcdnH)-L2;

codnH = zeros (1, LcdnH);

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

k = k +1; fin

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); fin; k = k +8; fin;

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; fin; ii = ii 1; d'autre B (jj) = XfuH (i); jj = jj +1; fin; fin; 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);

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 http://www.mathworks.com/matlabcentral/answers/38863-compression-image to have all the information you have provided here.

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

Related Content

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

Learn moreOpportunities for recent engineering grads.

Apply Today
## 2 Comments

## Dimple Anandpara (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/2158#comment_293828

i implement given code of huffman encoding for a .bmp file but the encode string size more then actual size of file can u explain why?

## Walter Roberson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/2158#comment_293835

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.