Huffman coding and decoding for image(JPEG, BMP)

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.

4 件のコメント

Dimple Anandpara
Dimple Anandpara 2015 年 6 月 23 日
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
Walter Roberson 2015 年 6 月 23 日
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.
Jasmina Vanasiwala
Jasmina Vanasiwala 2016 年 12 月 12 日
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?
Walter Roberson
Walter Roberson 2016 年 12 月 12 日
  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?

サインインしてコメントする。

 採用された回答

Paulo Silva
Paulo Silva 2011 年 2 月 28 日

0 投票

2 件のコメント

Manisha Mehra
Manisha Mehra 2011 年 3 月 1 日
Thanks..
It worked awesome.
fateh
fateh 2014 年 11 月 9 日
how could i run this example

サインインしてコメントする。

その他の回答 (2 件)

Falak
Falak 2012 年 5 月 4 日

0 投票

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

8 件のコメント

sailaja
sailaja 2014 年 1 月 31 日
its not working for an image
sailaja
sailaja 2014 年 1 月 31 日
how to use this code for an image
Walter Roberson
Walter Roberson 2014 年 1 月 31 日
Change the input() call to,
filename = input('enter the image file name', 's');
string = imread(filename);
string = string(:);
projecte
projecte 2014 年 4 月 26 日
hi how can i decode a image from a secret image in the same format
juveria fatima
juveria fatima 2018 年 2 月 17 日
filename = input('enter the image file name', 's');
what is 's' here
satyendra kumar
satyendra kumar 2019 年 3 月 23 日
they not work properly.....how can solve it?
%Target: To huffman encode and decode user entered string
%--------------------------------------------------------------------------
filename = input('enter the image file name', 's');
string = imread('cameraman.tif');
string = 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
Walter Roberson
Walter Roberson 2019 年 3 月 23 日
What leads you to say that it does not work?
Harshil Patel
Harshil Patel 2020 年 4 月 28 日
I ran the code in matlab 2020a, and it generated some otput with some values for str, ent, avglen and var for an image input. So, how do I know if it's correct?

サインインしてコメントする。

slama najla
slama najla 2012 年 5 月 19 日

0 投票

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

9 件のコメント

Walter Roberson
Walter Roberson 2012 年 5 月 19 日
The dictionary is dependent on the image being encoded. Having the dictionary for a different image would not help you.
SS
SS 2019 年 10 月 15 日
Can we use huffman coding for both lossy and lossless image compression??
Walter Roberson
Walter Roberson 2019 年 10 月 15 日
Yes. After you derive your symbols and remove the ones with sufficiently low probability or merge them with the nearest higher probability symbol, you can do Huffman encoding on what is left.
SS
SS 2019 年 10 月 15 日
Thank you
SS
SS 2019 年 11 月 23 日
how to create huffman tree diagram for lossless image compression?
Walter Roberson
Walter Roberson 2019 年 11 月 23 日
huffmandict()
SS
SS 2019 年 11 月 26 日
編集済み: SS 2019 年 11 月 26 日
i have compressed an image with lossless compression using huffman coding algorithm....i want to draw huffman tree for this process in matlab....can you please tell me how can i show the tree in matlab???
Walter Roberson
Walter Roberson 2019 年 11 月 26 日
SS
SS 2019 年 11 月 27 日
rng('default') % For reproducibility
X = rand(10,3);
In this code, what will be the value of X for my image..???

サインインしてコメントする。

カテゴリ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by