DIGITAL VIDEO CODING
HUFFMAN CODING
Use the Huffman encoding and decoding algorithms to do the following:
a) Code the Sena, Sinan, and Omaha images. List the compression ratios for each of the image
1. Connected to Omega Server using SSH Secure Shell and Basic Unix commands are used
to execute the huff_enc.c file.
2. The output compressed images are observed in the SSH Secure File Transfer.
3. Results
=
Image Original size (bytes) Compressed size (bytes) Compression Ratio
sena.img 65,536 57,504 1.1397
Sinan.img 65,536 61,650 1.0630
Omaha.img 65,536 58,374 1.1227
b) Write a program to take the difference between adjoining pixels, and then use huff_enc to
code the difference images. Compare the compression ratios with part (a)
1. MATLAB code to take the difference between adjoining pixels
clc;
close all; % closes open windows
clear all; % clears previous inputs and values from memory
FileName=input('Enter the image name ','s'); % Initialising the input
% image to the FileName
FileID=fopen(FileName); % the image is opened using fopen command and
identified
Image=fread(FileID); % the image is read using the command fread
[a,b]=size(Image); % Size of the input image is assigned as 1x2 matrix
row=sqrt(a); % rows are initialised with the square root of 'a'
column=row; % coloumns are initialised to rows
ReshapeImg=reshape(Image,row,column); % the image is reshaped using reshape
%command as its size is too high to display
figure; % displays original image
imshow(uint8(ReshapeImg')); % displays the reshaped image which is
% an 8 bit unsigned integer
Title=['Original Image of ',num2str(FileName)];
title(Title); % title of the image is taken from FileName
for i=1:row % initialising for loop of rows
for j=1:column-1 % initialising for loop of coloumns
DifferImg(i,j+1)=ReshapeImg(i,j+1)-ReshapeImg(i,j);
% difference of adjoining pixels is carried out
end
end
for i=1:256
DifferImg(i,1)=ReshapeImg(i,1)-128;
end
figure; % displays difference image
imshow(uint8(DifferImg')); % displays the difference image, an 8 bit unsigned
integer
Title=['Difference Image of ',num2str(FileName)];
title(Title); % title of the image is taken from FileName
FileID=fopen(['Resultant_',num2str(FileName)],'w+'); % creates new difference
%image file and discards existing contents.
fwrite(FileID,int8(DifferImg)); % the 8 bit signed integer is created for the
% difference image file
2. Resultant Difference Images
3. Commands to compress the pixel difference images using huff_enc.c file
Difference image Compression Ratio
Original size
Image compressed size Pixel difference Compressed
(bytes)
(bytes) image image
sena.img 65,536 22,654 2.8929 1.1397
Sinan.img 65,536 22,311 2.9374 1.0630
Omaha.img 65,536 27,846 2.3535 1.1227
4. Conclusion
From the above compression ratio results, we can observe the compression ratio
corresponding to pixel difference image is greater than that of Huffman encoded compressed
image.
c) Using the program huff_enc, code the Bookshelf1 and Sena images using the code-book
generated by the Sinan image. Compare the results with the case where the code-book was
generated by the image being compressed
1. Commands in SSH Secure Shell to execute the huff_enc.c program
2. Results
Number Object Size (bytes)
1 Codebook sinan 1,280
2 sena code 67,304
3 Output sinan 60,370
4 Original image without coding 65,536
65536
= = = 0.9737
67304
3. Conclusion
The results show that the size of the new image has been increased. In any type of
compression, the compressed image should be of lesser size than that of its original. The main
reason for increment of image size is that, the codebook of sinan image is used to code sena
and bookshelf1 images. As a result, segmentation fault for the images of sena and bookshelf1
has occurred, which is an error in image compression. This error has occurred due to Dynamic
mismatch of the range. Thus the codebook generated for a particular image, should be used for
the operations on that image only. In other words, code book is unique to each image. If used
for other images, results in larger image sizes or undesired outputs.