0% found this document useful (0 votes)
182 views

Lab Manual 15

This document contains information about image compression techniques including Huffman coding and JPEG compression. It provides code examples and explanations for generating a Huffman code dictionary from symbol probabilities, encoding data using Huffman codes, and decoding Huffman encoded data back to the original values. Functions for Huffman coding and decoding like huffmandict, huffmanenco, and huffmandeco are discussed along with their usage.

Uploaded by

Szn Ess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
182 views

Lab Manual 15

This document contains information about image compression techniques including Huffman coding and JPEG compression. It provides code examples and explanations for generating a Huffman code dictionary from symbol probabilities, encoding data using Huffman codes, and decoding Huffman encoded data back to the original values. Functions for Huffman coding and decoding like huffmandict, huffmanenco, and huffmandeco are discussed along with their usage.

Uploaded by

Szn Ess
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA

FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

COMPUTER GRAPHICS
&
IMAGE PROCESSING

LAB MANUAL 15

PREPARED BY:: ENGR. ALI JAVED

Computer Graphics & Image Processing 7th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.

Example: Creating and Decoding a Huffman Code


The example below performs Huffman encoding and decoding, using a source whose alphabet has
three symbols. Notice that the huffmanenco and huffmandeco functions use the dictionary that
huffmandict created.

sig = repmat([3 3 1 3 3 3 3 3 2 3],1,50); % Data to encode

symbols = [1 2 3]; % Distinct data symbols appearing in sig

p = [0.1 0.1 0.8]; % Probability of each data symbol

dict = huffmandict(symbols,p); % Create the dictionary.

hcode = huffmanenco(sig,dict); % Encode the data.

dhsig = huffmandeco(hcode,dict); % Decode the code.


Computer Graphics & Image Processing 7th Term-SE UET Taxila
UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Creating a Huffman Code Dictionary


Huffman coding requires statistical information about the source of the data being encoded. In
particular, the p input argument in the huffmandict function lists the probability with which the source
produces each symbol in its alphabet.

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.

symbols = [1 2 3]; % Data symbols

p = [0.1 0.1 0.8]; % Probability of each data symbol

dict = huffmandict(symbols,p) % Create the dictionary.

dict{1,:} % Show one row of the dictionary.

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 =

[1] [1x2 double]


[2] [1x2 double]
[3] [ 0]

ans =

ans =

1 1

Computer Graphics & Image Processing 7th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.

symbols = [1:6]; % Distinct symbols that data source can produce

p = [.5 .125 .125 .125 .0625 .0625]; % Probability distribution

[dict,avglen] = huffmandict(symbols,p); % Create dictionary.


actualsig = randsrc(1,100,[symbols; p]); % Create data using p.

comp = huffmanenco(actualsig,dict); % Encode the data.

dsig = huffmandeco(comp,dict); % Decode the Huffman code.

isequal(actualsig,dsig) % Check whether the decoding is correct.

The output below indicates that the decoder successfully recovered the data in actualsig.

ans =

Computer Graphics & Image Processing 7th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.

The outputs of huffmandict are


 dict, a two-column cell array in which the first column lists the distinct signal values from
symbols and the second column lists the corresponding Huffman codewords. In the second
column, each Huffman codeword is represented as a numeric row vector.
 avglen, the average length among all codewords in the dictionary, weighted according to the
probabilities in the vector p.

For Specific Syntaxes

[dict,avglen] = huffmandict(symbols,p) generates a binary Huffman code dictionary using the


maximum variance algorithm.

[dict,avglen] = huffmandict(symbols,p,N) generates an N-ary Huffman code dictionary using the


maximum variance algorithm. N is an integer between 2 and 10 that must not exceed the number of
source symbols whose probabilities appear in the vector p.

Computer Graphics & Image Processing 7th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

[dict,avglen] = huffmandict(symbols,p,N,variance) generates an N-ary Huffman code dictionary with


the minimum variance if variance is 'min' and the maximum variance if variance is 'max'. N is an
integer between 2 and 10 that must not exceed the length of the vector p.

Examples

symbols = [1:5];

p = [.3 .3 .2 .1 .1];

[dict,avglen] = huffmandict(symbols,p)

samplecode = dict{5,2} % Codeword for fifth signal value

The output is below, where the first column of dict lists the values in symbols and the second column
lists the corresponding codewords.

dict =

[1] [1x2 double]


[2] [1x2 double]
[3] [1x2 double]
[4] [1x3 double]
[5] [1x3 double]

avglen =

2.2000

samplecode =

1 1 0

Computer Graphics & Image Processing 7th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

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.

symbols = [1:6]; % Distinct symbols that data source can produce

p = [.5 .125 .125 .125 .0625 .0625]; % Probability distribution

[dict,avglen] = huffmandict(symbols,p); % Create dictionary.

actualsig = randsrc(100,1,[symbols; p]); % Create data using p.

comp = huffmanenco(actualsig,dict); % Encode the data.

Computer Graphics & Image Processing 7th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

JPEG image compression

DCT and Image Compression


In the JPEG image compression algorithm, the input image is divided into 8-by-8 or 16-by-16 blocks,
and the two-dimensional DCT is computed for each block. The DCT coefficients are then quantized,
coded, and transmitted. The JPEG receiver (or JPEG file reader) decodes the quantized DCT
coefficients, computes the inverse two-dimensional DCT of each block, and then puts the blocks back
together into a single image. For typical images, many of the DCT coefficients have values close to
zero; these coefficients can be discarded without seriously affecting the quality of the reconstructed
image.

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);

imshow(I), figure, imshow(I2)

Computer Graphics & Image Processing 7th Term-SE UET Taxila


UNIVERSITY OF ENGINEERING AND TECHNOLOGY, TAXILA
FACULTY OF TELECOMMUNICATION AND INFORMATION ENGINEERING

SOFTWARE ENGINEERING DEPARTMENT

Image courtesy of MIT

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.

Computer Graphics & Image Processing 7th Term-SE UET Taxila

You might also like