Lab Manual 15
Lab Manual 15
COMPUTER GRAPHICS
&
IMAGE PROCESSING
LAB MANUAL 15
IMAGE COMPRESSION
LAB OBJECTIVE:
The objective of this lab is to understand & implement
1. Huffman Coding
2. JPEG Compression
HUFFMAN CODING:
Huffman coding offers a way to compress data. The average length of a Huffman code depends on the
statistical frequency with which the source produces each symbol from its alphabet. A Huffman code
dictionary, which associates each data symbol with a codeword, has the property that no codeword in
the dictionary is a prefix of any other codeword in the dictionary.
The huffmandict, huffmanenco, and huffmandeco functions support Huffman coding and decoding.
For example, consider a data source that produces 1s with probability 0.1, 2s with probability 0.1, and
3s with probability 0.8. The main computational step in encoding data from this source using a
Huffman code is to create a dictionary that associates each data symbol with a codeword. The
commands below create such a dictionary and then show the codeword vector associated with a
particular value from the data source.
The output below shows that the most probable data symbol, 3, is associated with a one-digit
codeword, while less probable data symbols are associated with two-digit codewords. The output also
shows, for example, that a Huffman encoder receiving the data symbol 1 should substitute the
sequence 11.
dict =
ans =
ans =
1 1
Huffmandeco
Huffman decoder
Syntax
dsig = huffmandeco(comp,dict)
Description
dsig = huffmandeco(comp,dict) decodes the numeric Huffman code vector comp using the code
dictionary dict. The argument dict is an N-by-2 cell array, where N is the number of distinct possible
symbols in the original signal that was encoded as comp. The first column of dict represents the
distinct symbols and the second column represents the corresponding codewords. Each codeword is
represented as a numeric row vector, and no codeword in dict is allowed to be the prefix of any other
codeword in dict. You can generate dict using the huffmandict function and comp using the
huffmanenco function. If all signal values in dict are numeric, then dsig is a vector; if any signal value
in dict is alphabetical, then dsig is a one-dimensional cell array.
Examples
The example below encodes and then decodes a vector of random data that has a prescribed
probability distribution.
The output below indicates that the decoder successfully recovered the data in actualsig.
ans =
Huffmandict
Generate Huffman code dictionary for a source with known probability model
Syntax
[dict,avglen] = huffmandict(symbols,p)
[dict,avglen] = huffmandict(symbols,p,N)
[dict,avglen] = huffmandict(symbols,p,N,variance)
Description
For All Syntaxes
The huffmandict function generates a Huffman code dictionary corresponding to a source with a known
probability model. The required inputs are
symbols, which lists the distinct signal values that the source produces. It can have the form of
a numeric vector, numeric cell array, or alphanumeric cell array. If it is a cell array, then it
must be either a row or a column.
p, a probability vector whose kth element is the probability with which the source produces the
kth element of symbols. The length of p must equal the length of symbols.
Examples
symbols = [1:5];
p = [.3 .3 .2 .1 .1];
[dict,avglen] = huffmandict(symbols,p)
The output is below, where the first column of dict lists the values in symbols and the second column
lists the corresponding codewords.
dict =
avglen =
2.2000
samplecode =
1 1 0
Huffmanenco
Huffman encoder
Syntax
comp = huffmanenco(sig,dict)
Description
comp = huffmanenco(sig,dict) encodes the signal sig using the Huffman codes described by the code
dictionary dict. The argument sig can have the form of a numeric vector, numeric cell array, or
alphanumeric cell array. If sig is a cell array, then it must be either a row or a column. dict is an N-by-
2 cell array, where N is the number of distinct possible symbols to be encoded. The first column of dict
represents the distinct symbols and the second column represents the corresponding codewords. Each
codeword is represented as a numeric row vector, and no codeword in dict may be the prefix of any
other codeword in dict. You can generate dict using the huffmandict function.
Examples
The example below encodes a vector of random data that has a prescribed probability distribution.
The example code below computes the two-dimensional DCT of 8-by-8 blocks in the input image,
discards (sets to zero) all but 10 of the 64 DCT coefficients in each block, and then reconstructs the
image using the two-dimensional inverse DCT of each block. The transform matrix computation
method is used.
I = imread('cameraman.tif');
I = im2double(I);
T = dctmtx(8);
B = blkproc(I,[8 8],'P1*x*P2',T,T');
mask = [1 1 1 1 0 0 0 0
1 1 1 0 0 0 0 0
1 1 0 0 0 0 0 0
1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0];
B2 = blkproc(B,[8 8],'P1.*x',mask);
I2 = blkproc(B2,[8 8],'P1*x*P2',T',T);
Although there is some loss of quality in the reconstructed image, it is clearly recognizable, even
though almost 85% of the DCT coefficients were discarded. To experiment with discarding more or
fewer coefficients, and to apply this technique to other images, try running the demo function
dctdemo.