You are on page 1of 12

Implementing the SPIHT Algorithm in MATLAB

Aldo Morales and Sedig Agili


Department of Electrical Engineering,
Penn State University at Harrisburg
777 West Harrisburg Pike
Middletown, PA 17057

Abstract

New algorithms for image compression based on wavelets have been recently
developed1-4. One of the most efficient algorithms is the Set Partitioning in Hierarchical
Trees (SPIHT) algorithm1. Most of the implementations known to date for the SPIHT
algorithm have been done in C (or C++). This paper describes a MATLAB
implementation of the SPIHT algorithm where wavelet functions are directly called from
the wavelet toolbox, hence no code is needed, whereas in the C (C++) implementation,
one has to write and test wavelet function code. In the SPIHT algorithm zero-trees are
generated by the wavelet transform followed by a tree-search. Typically in C (or C++),
pointers are use to perform such a search. However, in MATLAB, the tree-search can be
performed using a structure array, which can be generated at run-time. In addition, the
SPIHT algorithm orders the wavelet coefficients according to a significance test and
stores this information in three separate lists. These lists are also easily implemented
using MATLAB’s structure arrays. MATLAB also allows working efficiently at bit-level
operations for bit-plane transmission of wavelet coefficients. Because of the wide use of
MATLAB in the academic community and the robustness of its toolboxes, particularly
the wavelets toolbox, students’ can easily modify the SPIHT m-files to accommodate
their research projects in the areas of image and signal processing. MATLAB sample
code and simulation results on image compression examples will be presented.

I. Introduction

New algorithms for image compression based on wavelets have been recently
developed1-4. These methods have resulted in practical advances such as: superior low-
bit rate performance, continuous-tone and bit-level compression, lossless and lossy
compression, progressive transmission by pixel accuracy and resolution, region-of-
interest coding and others. One of the most efficient procedures that fulfill the above
goals is the Set Partitioning in Hierarchical Trees (SPIHT) algorithm1. This algorithm
bases its efficiency in key concepts like: a) partial ordering of wavelet coefficients by
magnitude, with transmission of order by a subset partitioning that is replicated at the
decoder, b) ordered bit-plane transmission of refinement bits and c) exploitation of self-
similarity of the image wavelet coefficients across different scales.

Most of the implementations known to date for the SPIHT algorithm have been done in C
(or C++). This paper describes a MATLAB implementation of the SPIHT algorithm.
MATLAB is widely used in the academic community as one of the teaching platforms for
signal and image processing. It has a robust set of toolboxes, particularly the wavelets

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
toolbox5. The SPIHT algorithm creates a pyramid structure based on a wavelet
decomposition of an image. It has been discussed1-3,6 that the wavelet coefficients at the
top of the pyramid have a strong spatial relationship with their children. The SPIHT
algorithm bases its efficiency by iteratively searching for significant pixels throughout
the pyramid tree. Typically in C (or C++), there will be pointers (or arrays pointers) to
perform a search in the tree. In MATLAB, one of its newest data structures (structure
array) can be used for the same purpose. In addition, the SPIHT coder algorithm orders
the wavelets coefficients according to a significance test and stores this information in
three separate lists: list of insignificant sets (LIS), the list of insignificant pixels (LIP) and
the list of significant pixels (LSP). These lists are also implemented using structure
arrays. MATLAB also allows working efficiently at bit-level operations for bit-plane
transmission of wavelet coefficients. Although the MATLAB implementation of the
SPIHT algorithm is not as fast as its C (or C++) counterpart, the code can be easily
modified by calling different wavelet routines in the wavelet toolbox. In addition, the
SPIHT m-file can be used in the areas of research currently being pursued such as the
region of interest (ROI) coding. This paper is organized as follows. In section II the
SPIHT algorithm is briefly described. Structure arrays and their used in implementing
zero-trees are discussed in section III. Experimental results and discussions are presented
in section IV. Conclusions are shown in section.

II. The SPIHT Algorithm1

One of the most efficient algorithms in the area of image compression is the Set
Partitioning in Hierarchical Trees (SPIHT). In essence it uses a sub-band coder, to
produce a pyramid structure where an image is decomposed sequentially by applying
power complementary low pass and high pass filters and then decimating the resulting
images. These are one-dimensional filters that are applied in cascade (row then column)
to an image whereby creating a four-way decomposition: LL (low-pass then another low
pass), LH (low pass then high pass), HL (high and low pass) and finally HH (high pass
then another high pass). The resulting LL version is again four-way decomposed, as
shown in Figure 1. This process is repeated until the top of the pyramid is reached.

LL HL
HL
LH HH

LH HH

Figure 1: Image decomposition using wavelets

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
There exists a spatial relationship among the1-3,6 coefficients at different levels and
frequency sub-bands in the pyramid structure. A wavelet coefficient at location (i,j) in
the pyramid representation has four direct descendants (off-springs) at locations:

O(i, j ) = {(2i, 2 j ), (2i, 2 j + 1), (2i + 1, 2 j ), (2i + 1, 2 j + 1)} (1)

and each of them recursively maintains a spatial similarity to its corresponding four off-
spring. This pyramid structure is commonly known as spatial orientation tree. For
example, Figure 2 shows the similarity among sub-bands within levels in the wavelet
space6. If a given coefficient at location (i,j) is significant in magnitude then some of its
descendants will also probably be significant in magnitude. The SPIHT algorithm takes
advantage of the spatial similarity present in the wavelet space to optimally find the
location of the wavelet coefficient that are significant by means of a binary search
algorithm.

Figure 2: Off-spring dependencies in the pyramid structure6

The SPIHT algorithm sends the top coefficients in the pyramid structure using a
progressive transmission scheme. This scheme is a method that allows obtaining a high
quality version of the original image from the minimal amount of transmitted data. As
illustrated in Figure 3, the pyramid wavelet coefficients are ordered by magnitude and
then the most significant bits are transmitted first, followed by the next bit plane and so
on until the lowest bit plane is reached. It has been shown that progressive transmission
can significantly reduced the Mean Square Error (MSE) distortion for every bit-plane
sent1.

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
To take advantage of the spatial relationship among the coefficients at different levels
and frequency bands, the SPIHT coder algorithm orders the wavelets coefficient
according to the significance test defined as:

max Ci, j ≥ 2n (2)


(i , j ) ∈ τ m

where Ci, j is the wavelet coefficient at the nth bit plane, at location (i,j)of the τ m subset
of pixels, representing a parent node and its descendants. If the result of the significance
test is yes an S flag is set to 1 indicating that a particular test is significant. If the answer
is no, then the S flag is set to 0, indicating that the particular coefficient is insignificant.
This is represented by equation (3).

Bit sign s s s s s s s s s
Row
MSB 5 1 1 0 0 0 0 0 0 0
4 1 1 0 0 0 0 0
3 1 1 1 0 0
2 0
1
LSB 0

Figure 3: Bit-plane ordering and transmission scheme

1, max Ci, j ≥ 2n


 (i , j ) ∈ τ
Sn (τ ) =  (3)
0, otherwise

Wavelets coefficients which are not significant at the nth bit-plane level may be
significant at (n-1)th bit-plane or lower. This information is arranged, according to its
significance, in three separate lists: list of insignificant sets (LIS), the list of insignificant
pixels (LIP) and the list of significant pixels (LSP). In the decoder, the SPIHT algorithm
replicates the same number of lists. It uses the basic principle that if the execution path of
any algorithm is defined by the results on its branching points, and if the encoder and
decoder have the same sorting algorithm then the decoder can recover the ordering
information easily1.

The SPIHT algorithm1 can be summarized as follows

 { }
1. Initialization: Output n= n = log 2 (max (i, j ) Ci, j  ; set the LSP as empty list

and add the coordinates (i, j ) ∈ H to the LIP and only those with descendents also
to the LIS, as type A entries.
2. Sorting Pass

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
2.1 for each entry (i,j) in the LIP do:
2.1.1 Output Sn (i, j )
2.1.2 If Sn (i, j ) == 1 then move (i,j) to the LSP and output the sign of Ci, j
2.2 for each entry (i,j) in the LIS do:
2.2.1 if the entry is of type A then
• output Sn (D (i, j ));
• if Sn (D (i, j )) = 1 then
* for each (k , l ) ∈ O(i, j ) do:
o Output Sn (k , l )
o If Sn (k , l ) = 1 then add (k , l ) to the LSP and output
the sign of Ck ,l ;
o If Sn (k , l ) = 0 then add (k , l ) to the end of LIP
* If L (i, j ) ≠ φ then move (i, j ) to the end of the LIS as
entry of type B, and go to step 2.2.2; otherwise
remove entry (i, j ) from the LIS:
2.2.2 if the entry is of type B then
• Output Sn (L (i, j ))
• If Sn (L (i, j )) = 1 then
* add each (k , l ) ∈ O(i, j ) to the end of the LIS as
entry of type A
* remove (i, j ) from the LIS

3. Refinement Pass: For each entry (i, j ) in the LSP except those included in the
last sorting pass (i.e. with the same n), output the nth most significant bit of Ci, j
4. Quantization step update: decrement n by 1 and go to step 2

Notations used in the algorithm are defined as follows:


O(i,j): set of coordinates of the off-spring (i,j)
D(i,j): set of coordinates of all descendants (i,j)
H(i,j): set of coordinates of all tree roots in the highest level of the pyramid
L(i,j)=D(i,j)-O(i,j)

III. MATLAB data structures and SPIHT implementation

Most of SPIHT implementation algorithm has been coded in C or C++. However, the
authors feel that MATLAB and its wavelet toolbox offer an interesting option to
implement the SPIHT algorithm5,7. The MATLAB is very stable platform regarding code
and offers a rich variety of wavelets functions, which students can experiment with. In
addition, MATLAB offers two new data structures, cell arrays and structure arrays that
can be used to search for the coefficients in the trees generated by the wavelet
decomposition. These data structures allow grouping dissimilar but related arrays into a

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
single variable. Data management then becomes easier since groups of related data can be
organized and accessed through a cell or array structure. Particularly useful are structure
arrays, which can be created at run-time and can be used to access elements by field.
Since the SPIHT works with both the significance value of the wavelet coefficient and its
location (i,j) , then it seems suitable to work with these two different data types in a
structure array.

Structure arrays can access elements by names called fields7. These arrays can have a
number of dimensions. For example

LIS(1).significance=1;
LIS(1).location=[2 5];
LIS(2).significance=0
LIS(2).location =[8 10]

In the above example the structure array LIS has two fields: significance and location
with different data types, a singleton and 1-D array. In addition the structure array itself
has two entries LIS(1) and LIS(2). The LIS array fields and entries can then be
accessed easily.

Using structure arrays the search through the wavelet tree was implemented with a
standard stack search shown below, where T is the top of the tree:

FindMax(T)
(if T==NULL)
error;
else
SÅCreate New Stack;
Push(S,Root(T));
MaxÅ Root(T);
While (notEmptyStack(S));
TempÅ Pop(S);
If data(Temp) > Data (Max);
Max Å Temp;
Push all of the children of Temp onto S;
Endwhile;
Return Max;
End FindMax

This generic algorithm sequentially traverses the tree until it finds the maximum node, it
then efficiently pushes or pops elements on the top of the stack as it traverses the tree
thus keeping the size of the stack at minimum. The MATLAB version of this algorithm is
presented in appendix A, where a structure array was used to define the stack.

The LIS, the LIP and the LSP lists were also built using structure arrays. These arrays are
especially useful when handling the refinement pass as these lists can grow or shrink at
run-time. The sample code below shows the creation of part of LIS type A at the top of
the pyramid

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
for cellrow=1:2:height
for cellcol=1:2:width
count_LISA=count_LISA+1;
LIS(count_LISA).myIndex(1,1)={[cellrow cellcol]};
LIS(count_LISA).myIndex(1,2)={[cellrow cellcol+1]};
LIS(count_LISA).myIndex(2,1)={[cellrow+1 cellcol]};
LIS(count_LISA).myIndex(2,2)={[cellrow+1 cellcol+1]};
%
% each pixel should be identified properly
LIS(count_LISA).myType(1,1)={'TopALL'};
LIS(count_LISA).myType(1,2)={'TopAHL'};
LIS(count_LISA).myType(2,1)={'TopALH'};
LIS(count_LISA).myType(2,2)={'TopAHH'};

end
end

The LIS, the LIP and the LSP lists can grow dynamically at run-time. For instance in the
above example a new entry can be added to the structure array LIS by incrementing
count_LISA as follows:

count_LISA= count_LISA+1;
LIS(count_LISA).myIndex(1,1)={[cellrow cellcol]};

IV. Experimental results

The SPIHT algorithm was implemented using MATLAB. Our preliminary results are as
follows, Figure 4 shows the original Lena image, Figure 5 illustrates the wavelet pyramid
tree created by using the bi-orthogonal wavelet (bior4.4 in MATLAB [2]), and Figure 5
shows the recovered image using up to 5th bit plane (from the maximum of 13 bit-planes).
The recovered image is visually very close to the original image. If all the bit planes are
used then the original image is recovered completely (up to rounding errors). Although
the MATLAB version of the SPIHT runs slow no attempt was done to optimize the code
as for instance in8,9. The authors’ aim was to have an implementation ready for students
to experiment with. Students’ familiarity with MATLAB and accessible tools in it will
allow them to easily modify the code and reduce development time. For instance one
method is already explored in10 by modifying the SPIHT algorithm using Lossy/Lossless
region of interest (ROI) coding. In a similar fashion our students can modify the
algorithm to include different methods of ROI coding.

V. Conclusions

We have implemented the SPIHT algorithm using MATLAB and its wavelet toolbox.
Students’ familiarity with this platform is what guided us through this development.
Further research includes the optimization of the code as in8, improvements for region of

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
interest coding, and real-time implementation of the algorithm. Although the program
runs slower than its C counterpart, it gives the flexibility of using stable and robust set of
tools such as MATLAB’s.

Figure 4. Original Image

Figure 5: Pyramid tree generated by the four-way decomposition

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
Figure 6: Recovered image at 5th level

Bibliography

1. A. Said and W. Pearlman, “A New, fast and Efficient Image Code Based on Set
Partitioning in Hierarchical Trees,” IEEE Transactions on Circuits and Systems for
Video technology, Vol. 6, No. 3, June 1996.

2. J.M. Shapiro, “Embedded image coding using zero-trees of wavelet coefficients,”


IEEE Transactions Signal Processing, vol. 41, Dec. 1993.

3. D. Taubman, “High Performance scalable image compression with EBCOT,” IEEE


Transactions on Image Processing, vol. 9, July 2000.

4. I. Hontsch and L. Karan “ Locally adaptive perceptual image coding,” IEEE


Transactions on Image Processing, vol. 9, September 2000.

5. Misiti, Y, Misiti, G. Oppenheim, J.M. Poggi, Wavelet Toolboox, For use with
MATLAB. The Mathworks Inc., Natick, MA.

6. V. Ruiz, “Bit-Plane Compression Using SPIHT,”


http://www.ace.ual.es/~vruiz/IASTED-2000/html/node3.html

7. D. Hanselman and B. Littlefield, Mastering MATLAB: A Comprehensive Tutorial


and Reference, Prentice Hall, Upper Saddle River, New Jersey, 2001.

8. Y. Sun, H. Zhang and G. Hu, “ Real-Time Implementation of a New Low-Memory


SPIHT Image Coding Algorithm Using DSP Chip,” IEEE Transactions on Image
Processing, Vol. 11, No. 9, September 2002.

9. VCDemo at http://www-it.et.tudelft.nl/~inald/vcdemo/

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
10. E. Atsumi and N. Farvardin, “Lossy/Lossless region of interest image coding based
on set partitioning in hierarchical trees,” Proc. of IEEE Int. Conf. Image Processing,
Vol. 1, pp. 87-91, Oct. 1998.

ALDO MORALES

Dr. Morales received his electronic engineering degree with distinction from the University of Tarapaca,
Arica, Chile (formerly Northern University) and M.S. and Ph.D. degrees in electrical and computer
engineering from the State University of New York at Buffalo. Since August 2001 he has been working as
an associate professor of electrical engineering at Penn State Harrisburg. From 1990 to August 2001 he
was with Penn State DuBois first as assistant professor and then as an associate professor of engineering.
His research interests are in mathematical morphology, digital image processing, computer vision, and
neural networks. Dr. Morales has been involved in teaching microprocessors applications, computer
languages, software application and local area networks for many years. In 1996 Dr. Morales was honored
by the Institute of Electrical and Electronic Engineers (IEEE) with the Best Paper Award at the
International Asia Pacific Conference on Circuits and Systems for the paper " Basis Matrix Representation
of Morphological Filters with N-Dimensional Structuring Elements."

SEDIG AGILI

Dr. Agili received his BS, MS, and Ph.D. in Electrical and Computer Engineering from Marquette
University in 1986, 1989, and 1996, respectively. As a student, he was awarded fellowships from
Marquette University and the U.S. Department of Education. Upon receiving his Ph.D., he joined the
faculty at Marquette University where he taught several courses in electrical engineering and conducted
research in the area of electro-optic devices, fiber optic communication and fiber optic sensors. In fall of
2001, he joined the electrical engineering and electrical engineering technology programs at Penn State
University, Capital College. Currently has is teaching and conducting research in electronic
communications, fiber optic communications and fiber optic sensors. He has authored several articles
published in journals and conference proceedings, and made presentations at many conferences and
seminars. He also worked for Astronaut Corporation of America in Milwaukee, Wisconsin where he was
involved in designing optical projection and heads-up display systems. He is a member of the Institute of
Electrical and Electronic Engineers, American Society for Engineering Education, and Sigma Xi.

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
Appendix : Implementing a tree search using structure arrays

function [myMax,N] = my_stack(myWavCo,LFLAG,i,j,ii,jj)


% create an stack to find the items of the list
% where every node creates 4 direct descendants
% pointer to stack
% Aldo Morales and Sedig Agili PSU Harrisburg
% Many Thanks to Hans Royer, CS Department PSU Harrisburg
% myWavCo wavelet coefficients
% (i,j) pixel locations where the descendants are calculated from
% n is stack pointer
% LIS.mystack is the data structure containing the stack
% (ii,jj) is the last coordinate calculated from the wavelet
% decomposition
% if LFLAG ==1 then do search for Sn(L(i,j)) otherwise search for
% Sn(D(i,j))
% global variables below
%
global struct LIS;
global count_LISA;
global struct LIP;
global count_LIP;
global struct LSP;
global count_LSP;
global tempCountLISA;
global Sn_Matrix;
global Sn_Sign;
global SnDij_Matrix;
%
% n=1 is the stack's origin
n=1;
% To modify easily this algorithm make the root of the tree very, very
% small
% create a temporary stack
if (LFLAG==1)
% get a very small number
myMax=0;
%
else
% get the direct descendant
%
myMax=abs(myWavCo(i,j));
end
TempLIS(n).mystack=[i j];
n=n+1;
%n=n+1 means push into the stack
%
bigestI=i;
bigestJ=j;
%calculate the total number of iterations based on the level
N=0;
while(n>1)
% here goes the max calculation
% here comes the pop
N=N+1;
n=n-1;
TEMP=TempLIS(n).mystack;
%ready to push
% get the pixel coordinates
% pop and check value
%
myI=TempLIS(n).mystack(1);

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education
myJ=TempLIS(n).mystack(2);
%check for ending
bigestII=2*myI;
bigestJJ=2*myJ;
%internal checking
% Is this the end of the tree?
% if not create four descendants
% and push them into the stack
if ((bigestII<=ii)& (bigestJJ<=jj))
TempLIS(n).mystack=[2*myI 2*myJ];
%push again and test
if(abs(myWavCo(2*myI,2*myJ))>abs(myMax))
myMax=myWavCo(2*myI,2*myJ);
end
n=n+1;
TempLIS(n).mystack=[2*myI 2*myJ-1];
if(abs(myWavCo(2*myI,2*myJ-1))>abs(myMax))
myMax=myWavCo(2*myI,2*myJ-1);
end
%push again
n=n+1;
TempLIS(n).mystack=[2*myI-1 2*myJ];
if(abs(myWavCo(2*myI-1,2*myJ))>abs(myMax))
myMax=myWavCo(2*myI-1,2*myJ);
end
% and push again
n=n+1;
TempLIS(n).mystack=[2*myI-1 2*myJ-1];
if(abs(myWavCo(2*myI-1,2*myJ-1))>abs(myMax))
myMax=myWavCo(2*myI-1,2*myJ-1);
end
n=n+1;
%push four times
%
end
% below error routine if we pass beyond the stack's origins
if (n<1)
error('Below stack exiting ...');
end
%fprintf('n=stacks depth =%d\n',n);
end
clear TempLIS;

Proceedings of the 2003 ASEE/WFEO International Colloquium


Copyright  2003, American Society for Engineering Education

You might also like