November 03, 2008

My first Matlab Program, Encode and Decode image using Huffman Coding

For the academic purpose i had written a small program in Matlab.

I have tried encoding and decoding the image by using Huffman coding.

Huffman code generally used for data encoding but i tried Huffman coding on small image.

Preconditions:
  1. You need to create a small image, in my code i am using PSR.jpg
  2. It will work well only with small images

Matlab Code:

%Huffman Coding on image by Suresh Raju Pilli

%clearing all variableas and screen
clear all;
close all;
clc;

%Reading image
a=imread('psr.JPG');
figure,imshow(a)

%converting an image to grayscale
I=rgb2gray(a);

%size of the image
[m,n]=size(I);
Totalcount=m*n;

%variables using to find the probability
cnt=1;
sigma=0;

%computing the cumulative probability.
for i=0:255
k=I==i;
count(cnt)=sum(k(:))

%pro array is having the probabilities
pro(cnt)=count(cnt)/Totalcount;
sigma=sigma+pro(cnt);
cumpro(cnt)=sigma;
cnt=cnt+1;
end;


%Symbols for an image
symbols = [0:255];

%Huffman code Dictionary
dict = huffmandict(symbols,pro);

%function which converts array to vector
vec_size = 1;
for p = 1:m
for q = 1:n
newvec(vec_size) = I(p,q);
vec_size = vec_size+1;
end
end

%Huffman Encodig
hcode = huffmanenco(newvec,dict);

%Huffman Decoding
dhsig1 = huffmandeco(hcode,dict);

%convertign dhsig1 double to dhsig uint8
dhsig = uint8(dhsig1);

%vector to array conversion
dec_row=sqrt(length(dhsig));
dec_col=dec_row;

%variables using to convert vector 2 array
arr_row = 1;
arr_col = 1;
vec_si = 1;

for x = 1:m
for y = 1:n
back(x,y)=dhsig(vec_si);
arr_col = arr_col+1;
vec_si = vec_si + 1;
end
arr_row = arr_row+1;
end


%converting image from grayscale to rgb
[deco, map] = gray2ind(back,256);
RGB = ind2rgb(deco,map);
imwrite(RGB,'decoded.JPG');

%end of the huffman coding

Postcondition:
  1. Check the original image and decoded image
  2. Analyze the compression ratio.
You can easily understand the code. Its not at all difficult.

Please send your feedback and comments to psrdotcom@gmail.com

21 comments:

Anonymous said...

nice, works perfect

Anonymous said...

the decoded image is still grey..

Suresh Raju Pilli said...

First check your image .. I think it is also grey .. Because the .jpeg file is compressed one.

Unknown said...

what happen if use a big image?
is there limited resolution?

Suresh Raju Pilli said...

I hope, there won't be any limit in the resolution .. You can try it ..

Anonymous said...

works fine, but wondering if you could do arithmetic coding on an image

kazzama said...
This comment has been removed by the author.
kazzama said...

why the results of compression still grayscale??

Suresh Raju Pilli said...

As I had given reply for the same question above, Please verify your image after saving it. Because JPEG image compression causes image color change.

kazzama said...

can without going through the process of grayscale?

anmol said...

Can you explain me the following statements?
for i=0:255
k=I==i;
count(cnt)=sum(k(:))

Nanda said...

how to calculate compression ration

Unknown said...

Sir my input image is 165x124 jpg color image sir.after decoded the image is displayed in grey color sir.please explain.

Suresh Raju Pilli said...

Hi Selva Murugan, Have you checked the saved image color after creating?

Sometimes, the image which you have created might be changed after saving.

Karishma said...

Hi , this code takes long to execute the code can you plz help me.
i have taken rice.png image which is in a matlab.

Anonymous said...

Nice work! Just a quick question, wouldn't the reconstruction process be affected if we embed channel coding after Huffman encoding process?

Siddesh said...

How can we come to know that its a lossless or lossy ? could you explain it briefly ?

Unknown said...

How Huffman code perform on HL2,LH2, HH2 sub bands

I need Arithmetic code for image.

Unknown said...

hi
i use whos(a) for orginal image and whos(decoded) for decoded image in matlab.
The result was the same size.
Usually , After compression coding should be reduced in size.
please guide me.

Unknown said...

sir.i want to implement huffma coding for image compression.this code is not working there some error occur in
for i=0:255
k=I==i;count(cnt)=sum(k(:))
plz tell me hw to calculate the probability of pixels..

Unknown said...

please send i need this type model mat lab program

my QUESTION IS ...?


Implement Huffman Coding for 12 unique symbols of massage only. Implement
encoding decoding for Image. Show your result on Image after converting
image into 12 unique pixel values only.


PLEASE SEND
my eamil: malliec2@gmail.com

MATLAB CODE November 23date before

Featured Post

Java Introdcution

Please send your review and feedback to psrdotcom@gmail.com