# Introductory Lecture

Input and output image, computing frequencies, probabilities, entropy. Simple encode/decode by zip, gzip

## Read one tiff image (it exists in matlab default directories)

InputImage = imread('peppers.png'); % A color image

InputImage = imread( 'cameraman.tif' );
% transform variables from uint8 to double (matlab works mainly with double)
InputImage = double(InputImage);
[nr,nc] = size(InputImage);
figure(1),clf,imagesc(InputImage),colormap(gray)


## Dynamic range, occurence number, empirical probability

Dynamic range

maxII = max( InputImage(:) ) % largest value
minII = min( InputImage(:) ) % smallest value

% Number of occurences of a gray level in the image
hh = hist( InputImage(:),minII:maxII);
figure(2), plot( minII:maxII, hh )
title('Number of occurrences versus graylevel')

maxII =

253

minII =

7



Empirical probability of each graylevel in the image

pp = hh/sum(hh);
figure(3), plot( minII:maxII, pp )
title('Empirical probability versus graylevel')
xlabel('x (gray level)')
ylabel('p(x)')


## Basic I/O for reading and writing in raw format

write the image in raw format (8 bits/pixel) in the current folder

filename = 'cameraman.bin';
fid = fopen(filename,'wb');
fwrite(fid,InputImage,'uchar');
fclose(fid);
% check the size of the file on the disk ( should be 512*512 = 262144)
S = dir(filename)
% read back from the disk

fid = fopen(filename,'rb');
fclose(fid);
% check the correctness
[sum( II(:) ~= InputImage(:) ) sum( II(:) == InputImage(:) )]

S =

name: 'cameraman.bin'
date: '08-maalis-2016 01:06:29'
bytes: 65536
isdir: 0
datenum: 7.3640e+05

ans =

0       65536



% use the compressor gzip
GZFileName = gzip(filename)
S = dir(GZFileName{1})
CompressionRatio = S.bytes/(nr*nc)
% uncompresses 'lena512.bin.gz' and look for the size of the result
II2 = gunzip(GZFileName{1});
S = dir(II2{1} )

% what is the size of the compressed file in jpg format?
% compress the file in jpg format, to 'cameraman.jpg' output file
imwrite( InputImage/max(InputImage(:)),'cameraman.jpg' );
S = dir('cameraman.jpg' )
CompressionRatio = S.bytes/(nr*nc)
% read, i.e., decompress the image
number_of_errors = sum( InputImage(:) ~= InputImage2(:))
[max( InputImage(:)) max( InputImage2(:)) ]
figure(1),imagesc( abs( double(InputImage)-double(InputImage2 ))), colormap(gray)
title('Error between initial and decompressed images')
figure(2),imagesc(  InputImage2 ), colormap(gray)
title('Decompressed image in JPG format')
figure(3),imagesc(  InputImage ), colormap(gray)
title('Initial image')

GZFileName =

'cameraman.bin.gz'

S =

name: 'cameraman.bin.gz'
date: '08-maalis-2016 01:06:29'
bytes: 49159
isdir: 0
datenum: 7.3640e+05

CompressionRatio =

0.7501

S =

name: 'cameraman.bin'
date: '08-maalis-2016 01:06:29'
bytes: 65536
isdir: 0
datenum: 7.3640e+05

S =

name: 'cameraman.jpg'
date: '08-maalis-2016 01:06:29'
bytes: 10741
isdir: 0
datenum: 7.3640e+05

CompressionRatio =

0.1639

number_of_errors =

55549

ans =

253  255



## Entropy corresponding to the empirical probability of cameraman

Entropy is

H = - sum(pp.*log2(pp)) % obviously here we have an error (log(0) = NaN)
% take out the values 0 from the vector pp
pp = pp(pp>0);
H = - sum(pp.*log2(pp))

% one needs H = 7.0097 bits per pixel to encode the image using the probability pp
% (the compressor able to compress close to this bitrate is the Huffman coder to be
% studied in Lecture 2)
TotalCodelength = ceil(nr*nc*H/8) % result in bytes

H =

7.0097

H =

7.0097

TotalCodelength =

57424



## Summary of the important code to be used later !

maxII = max( InputImage(:) )
minII = min( InputImage(:) )
% Number of occurences of a gray level in the image
hh = hist( InputImage(:),minII:maxII);
% Empirical probability of a gray level in the image
pp = hh/sum(hh);
pp = pp(pp>0);
H = - sum(pp.*log2(pp))

maxII =

253

minII =

7

H =

7.0097