WAVELET-BASED IMAGE COMPRESSION
Shetul Saksena B. E., Gujarat University, India, 2007
PROJECT
Submitted in partial satisfaction of the requirements for the degree of
MASTER OF SCIENCE
in
ELECTRICAL & ELECTRONIC ENGINEERING
at
CALIFORNIA STATE UNIVERSITY, SACRAMENTO
SPRING 2011 WAVELET-BASED IMAGE COMPRESSION
A Project
by
Shetul Saksena
Approved by:
______, Committee Chair Jing Pang, Ph. D.
______, Second Reader Fethi Belkhouche, Ph.D.
______Date
ii
Student: Shetul Saksena
I certify that this student has met the requirements for format contained in the University format manual, and that this project is suitable for shelving in the Library and credit is to be awarded for the Project.
______, Graduate Coordinator ______B. Preetham Kumar, Ph.D. Date
Department of Electrical & Electronic Engineering
iii
Abstract
of
WAVELET-BASED IMAGE COMPRESSION
by
Shetul Saksena
Recently, compression techniques using Wavelet Transformation have received great attention for their promising compression ratio, analysis of the temporal and spectral properties of image signals, and flexible representation of non-stationary signals by taking into account the human perception system. With lossless compression, the original image is recovered immediately after decompression. Unfortunately, with images of natural scenes it is rarely possible to obtain error-free compression at a rate beyond
2:1. Much higher compression ratios can be obtained when some errors, which are usually difficult to perceive, are allowed between the decompressed image and the original image. This project investigates wavelet-based lossy compression of 2D still images. This was achieved by performing Discrete Haar Wavelet Transformation (DWT) on an image followed by Entropy Encoding techniques including Embedded Zerotree
Wavelet (EZW) and Huffman encoding.
______, Committee Chair ______Jing Pang, Ph. D. Date iv
ACKNOWLEDGEMENTS
The process of completing this work has left me gratefully and deeply indebted to
Dr. Jing Pang whose help, stimulating suggestions and encouragement have contributed
endlessly to my success. Dr. Pang’s involvement kept me motivated through the
formative stages of my work and her support and enthusiasm helped me establish the
project in its earliest and most vulnerable stages. Our bi-weekly meetings were essential and enjoyable stepping stone to meet my goals and her informed advice was welcomed on every aspect of this project.
I would like to thank Dr. Fethi Belkhouche for his insightful suggestions following a careful review of my report. The advice I have received over the course of this project has been graciously contributed by a number of my colleagues, I am grateful
to all of them.
I would like to thank all the faculty members of the Department of Electrical &
Electronic Engineering for helping me finish my Master of Science degree at California
State University, Sacramento. To all who helped me in carrying out this project
successfully, I acknowledge my indebtedness and express my great appreciation.
v
TABLE OF CONTENTS
Page
Acknowledgements ...... v
List of Tables ...... viii
List of Figures ...... ix
Chapter
1. INTRODUCTION ...... 1 1.1 What is Compression? ...... 1 1.2 Evolution of Data Compression ...... 2 1.3 My Project ...... 4 1.3.1 Significance of the Project ...... 4 1.3.2 Limitations of the Project ...... 4 2. IMAGE COMPRESSION ...... 5 2.1 Digital Images ...... 5 2.1.1 Color Depth, Bits Per Pixel ...... 6 2.1.2 Compression Ratio ...... 7 2.2 Lossy and Lossless Compression ...... 8 2.3 Basic Image Compression Block Diagram ...... 9 2.4 Progressive Image Compression Algorithm ...... 10 2.5 Wavelet-based Image Compression Steps (Coding Approach) ...... 12 3. WAVELET-BASED IMAGE COMPRESSION ...... 15 3.1 Discrete Haar Wavelet Transform ...... 15 3.2 Embedded Zerotrees of Wavelet Transforms Encoding ...... 23 3.2.1 EZW Encoding Algorithm ...... 25 3.2.2 Morton Scan ...... 29 3.3 Huffman Encoding ...... 30 3.3.1 Huffman Tree ...... 31 4. MATLAB IMPLEMENTATION ...... 33 vi
4.1 Steps of Compression ...... 34 4.1.1 Reading the Image to be Compressed ...... 35 4.1.2 Discrete Haar Wavelet Transformation ...... 35 4.1.3 EZW Encoding of Wavelet Coefficient Matrix ...... 35 4.1.4 Huffman Encoding ...... 36 4.1.5 Writing Compressed Image ...... 36 4.2 Function Flow Diagram ...... 36 4.3 Simulation Results ...... 37 4.4 Observations ...... 39 4.5 Future Improvements ...... 39 5. APPLICATIONS OF WAVELET-BASED IMAGE COMPRESSION ...... 41 6. CONCLUSION ...... 43 Appendix MATLAB Code ...... 44 Bibliography ...... 66
vii
LIST OF TABLES
1. Table 4.1: Level of Haar transformation vs. compression ratio ...... 39
viii
LIST OF FIGURES
1. Figure 2.1: Y, Cb and Cr components of an image...... 6
2. Figure 2.2: Basic image compression block diagram ...... 9
3. Figure 2.3: Progressive image compression algorithm ...... 11
4. Figure 2.4: Region of Interest (ROI) selection ...... 12
5. Figure 3.1: Pyramidal decomposition of an image ...... 20
6. Figure 3.2: Single level DWT ...... 21
7. Figure 3.3: Level 2 DWT ...... 22
8. Figure 3.4: Level 3 DWT ...... 22
9. Figure 3.5: Quad trees ...... 24
10. Figure 3.6: Wavelet coefficients represented in different subbands ...... 25
11. Figure 3.7: Wavelet coefficients coded as P, N, T, and Z ...... 27
12. Figure 3.8: Raster (row-wise) scan order ...... 29
13. Figure 3.9: Morton (Z) scan order ...... 30
14. Figure 3.10: Huffman tree...... 32
15. Figure 4.1: Original test image lena.jpg ...... 33
16. Figure 4.2: Steps of compression ...... 34
ix
17. Figure 4.3: Function flow diagram ...... 37
18. Figure 4.4: Original image ...... 37
19. Figure 4.5: Single level discrete Haar wavelet transform ...... 38
20. Figure 4.4: Reconstructed image ...... 38
x
1
Chapter 1
INTRODUCTION
1.1 What is Compression?
Computing needs have increased exponentially in sync with technological
advances over the last five decades. These increases have lead to a staggering explosion
in the sheer amount of data that must be transmitted and stored via the internet. Out of
necessity for data transfer and storage, “data compression” was born. A solution to the problem, data compression offers the reduction of storage or internet bandwidth required
to handle data. Certain functionalities of the internet such as browsing, chatting and
streaming music or video etc. are possible at a faster speed day after day due to the
important and initial process of data compression and it continues to play a noteworthy
part in maintaining a realistic digital world.
Data compression is considered such a significant piece of the digital experience
because of the numerous needs that it satisfies. Most importantly, data compression
improves data throughput by compressing data whenever possible. As the internet’s
ability to transfer data is stagnant, its maximum capacity can be increased with the assistance of data compression. In some cases, the size of a file may be reduced by 60-
70%, an extremely efficient rate compared to the transmission of the same file in its
entirety. The usefulness and efficiency of data compression have made its technological advancement a necessity for today’s growing computing requirements. Understanding the
2
entire process and the evolution of data compression allows for the additional research
that improves its functionality over time.
1.2 Evolution of Data Compression
The simplest and the earliest form of data compression dates back to the year
1936 when the electric telegraph, a device that transmits pulses of electrical current that
controls an electromagnet for the receiving end of a telegraph wire, was invented
by Samuel F. B. Morse and Alfred Vail. Two years later, a new version of this invention was adopted internationally and became widely used throughout the globe. Soon after,
the development of information theory in the 1940s provided further perspective of the
potential uses of data compression and nine years later, a systematic process for assigning
codewords based on the probabilities of blocks was engineered by Claude
Shannon and Robert Fano. Many engineers contributed to the development of the data
compression process and assisted with its evolution. David Huffman improved Shannon
and Fano’s systematic process in 1951 and the list of contributors continues on into the
1970s where early implementations were typically done in hardware, with specific
choices of codewords being made as compromises between compression and error
correction. Based on the actual data encountered, dynamically updating codewords for
Huffman encoding emerged in mid 1970s. And in the late 1970s, with online text file
storage becoming more and more common, programmers began to develop various
softwares for compression, and almost all the newly developing softwares were based on
adaptive Huffman coding. In 1977, Abraham Lempel and Jacob Ziv came up the basic
3
idea of pointer-based encoding. In the mid-1980s, following work by Terry Welch, the so-called LZW algorithm rapidly became the method of choice for most general-purpose compression systems. It was used in archiving tools such as PKZIP, written by Phil Katz in 1989, as well as in hardware devices such as modems. In the late 1980s, digital images became more common, and standards for compressing them emerged. In the early 1990s, lossy compression methods also began to be widely used, one of which was JPEG-1992.
The history of this technology is expansive and it plays a large role in the directions researchers are taking with image compression, specifically with reduction in the size of images being transmitted. Current research dictates that the Discrete Haar
Wavelet Transform is a less noisy and more efficient process than its counterpart,
Discrete Cosine Transform. The Joint Photographic Experts Group (JPEG) committee created a new image compression standard and coding system in 2000 which not only gives improved compression ratio over JPEG-1992, but adds features such as scalability and editability.
It would be foolish to say that there is no future growth potential for data compression. With the advancement of technology, decreasing costs, and increasing speeds and capacities, people will continue to find ingenious new compression methods to deliver by computer. In the future; I predict that there will be new forms of data that needs new kinds of compression. One potential form of data, holographic image and video, is an exciting prospect for future research.
4
1.3 My Project
This project report has been organized to facilitate a deeper understanding of the
evolution of image compression research allowing readers a better opportunity to
appreciate the potential applications of the project.
This section briefly explains the details of the project, including how it has been
written and organized, its practical limitations, implications, significance and
contributions to the field of image compression.
1.3.1 Significance of the Project
In this project, I have implemented the use of Discrete Haar Wavelet
Transformation (DWT) along with Embedded Zerotree Wavelet (EZW) encoding which form the basis of JPEG-2000 compression technique. The resulting functions embedded in the project are widely applicable and may be utilized with many future compression techniques in order to achieve a higher compression ratio and even better image data preservation. This project will serve as a springboard for future research venturing to expand and or improve the field of image compression.
1.3.2 Limitations of the Project
While the coding is versatile in its potential for application, this project was implemented in MATLAB and has been written in a way that it may only function for image matrices with same width and height.
5
Chapter 2
IMAGE COMPRESSION
Image compression is one of many applications of data compression. This method of compression is useful when attempting to save or transmit image data as it reduces the redundancy of the image making the process more efficient. In addition, this method preserves the integrity of the image and prevents quality degradation to an unacceptable level. There are three forms of redundancy found in images including spatial redundancy caused by overlapping pixels, spectral redundancy caused by overlapping colors, and temporal redundancy caused by the correlation of adjacent frames. The process of image compression is able to reduce data size by decreasing the amount of redundancy with both spatial and spectral redundancies [4].
2.1 Digital Images
Prior to discussing the process of image compression, it is important to understand how an image is stored in raw form in a computer. In most computer displays,
RGB is used as a standard color space, an additive color model where each pixel is represented numerically as fractions of Red, Green and Blue, with values ranging from 0
(low intensity) to 255 (high intensity). In this project NTSC images are coded as YCbCr
(sub-sampled as 4:2:2). These acts as a grouping of color spaces that together form a piece of the color image pipeline for both digital and video photography systems [2].
6
YCbCr color space has a luma component, Y and two chroma components: blue- difference (Cb) and red-difference (Cr) as shown below:
Original Image Y Component
Cb Component Cr Component
Figure 2.1: Y, Cb and Cr components of an image [2]
2.1.1 Color Depth, Bits Per Pixel
Color Depth is defined by the number of bits used to represent the color of a
single pixel in any digital image and is also known as bits per pixel (BPP). To obtain a
7 more diverse range of color depth, with distinct colors, a higher color depth is necessary
[12].
Consider a 128×128 resolution image and that each pixel needs 8 bits for storage in the memory, hence the resulting size of the image would be 128×128×8 = 131072 bits.
The aspiration of image compression is to utilize the available hidden structure within an image in order to minimize such storage specifications [11].
2.1.2 Compression Ratio
Compression Ratio (CR) measures reduced data-representation size resulting from an algorithm of data compression [13]. Mathematically, Compression Ratio is considered a representation of the size of a compressed image compared to the size of the same image in its raw form.
In percentage form,
Compressed Image Size Compression Ratio % = × 100 Uncompressed Image Size
Some examples of compression include video, which can approach a CR of 100:1 with nearly undetectable loss of quality; audio can approach a CR of 10:1 with imperceptible quality loss and still images approach a CR of 10:1 and tend to demonstrate a more marked loss of quality, most notably upon detailed analysis of the image.
8
2.2 Lossy and Lossless Compression
Image compression can be classified into two different practices: lossy and
lossless. The choice to use either one is dependent upon the type of image one wishes to
compress. Lossless compression is based on the idea of breaking the image data into a
smaller form for transmission and/or storage and then joining it back together on the
other end so it can be used again in its original form. Lossy compression on the other
hand is very different; it incorporates the idea of eliminating unnecessary bits of
information resulting in reduction of the image size. When dealing with technical images such as medical documents or technical drawings it is best to utilize lossless compression because the use of lossy could produce compression artifacts (visible distortion of the media) that would be noticeable in the document(s). However, when dealing with more natural images, where the distortions are less salient, lossy compression is suitable and efficient. The current methods for lossless compression include run-length encoding,
DPCM and predictive coding, entropy coding, adaptive dictionary algorithms, deflation and chain codes. Current methods for lossy include reduction of color space, chroma subsampling, transform coding and fractal compression. My approach in this project has been a lossy compression technique where data loss occurs when the raw image data is converted into wavelet coefficients during Discrete Haar Wavelet Transformation in the process of rounding off the decimals; during Quantization which is achieved by compressing a range of values to a single quantum value; during Embedded Zerotree
Wavelet encoding in a process called thresholding.
9
2.3 Basic Image Compression Block Diagram
Block diagram approach makes it easy to understand any complicated process.
The diagram below shows the main functional blocks in any image coding system:
Raw Image x
Forward Transform y
Quantizer q
Encoder
Compressed Image
Figure 2.2: Basic image compression block diagram
1. Forward Transform – This is a transformation or filtering process in which the image x is samples into smaller coefficients (samples) and also preserving the total energy into the resulting y image matrix. This transformation process reduces non-zero samples of y matrix that need to be transmitted. Today, there are many varieties of linear transforms out there such as Discrete Fourier Transform (DFT), Discrete Cosine
Transform (DCT), Discrete Wavelet Transform (DWT) and many more, each with its own advantages and disadvantages [4].
10
2. Quantizer – the amount of storage space (in the memory) required by an image
is reduced by dropping the number of bits of the transformed coefficients. Quantization is
a lossy technique and is a very important step in determining the compression ratio.
Quantization done on each individual transformed coefficient is called Scalar
Quantization (SQ) and the process of quantization done on a group of transformed
coefficient is called Vector Quantization (VQ) [4]. The outcome after a successful
quantization process is the integer matrix q.
3. Encoder – Once the image is quantized it needs to be encoded into a bit stream.
The encoder processes the quantized image matrix losslessly. Some of many examples of
efficient encoders include Huffman encoder and the arithmetic encoder, Run Length
Encoding (RLE). After the encoding process, the quantized matrix q is encoded into a
serial bit stream d.
2.4 Progressive Image Compression Algorithm
In order to process the image for compression, a small region called the Region of
Interest (ROI) is defined and the desired operation is performed on that region and then is
iterated over the entire image. Typically, a square matrix such as 4x4 or 8x8 matrix is
selected as a ROI. ROI coding is one of the most important features incorporated in
JPEG-2000.
11
Raw Image
ROI Selection
Lossy Wavelet Lossy Wavelet
EZW Encoding
Arithmetic Encoder
Compressed Image
Figure 2.3: Progressive image compression algorithm
Figure 2.4: Region of Interest (ROI) selection
Because of the use of ROI selection approach, heterogeneous fidelity constraints can be imposed to different regions of the image rather than encoding the entire image as
12
a single entity. One of major applications lies in medical field where the images consist of regions that can be encoded at different bit rates [8].
After the selection of the region of interest efficiently, various lossless
transformation and filtration processes are performed followed by EZW encoding.
Fig. 2.4 illustrates the application of ROI on a large image.
2.5 Wavelet-based Image Compression Steps (Coding Approach)
1. Digitization: The first step in the wavelet-based compression process is to
digitize the image into a signal s, which is a string of numbers usually represented as a
2D matrix. A digitized image is characterized by its intensity levels, or scales of gray which range from 0 (black) to 255 (white), and its resolution, or the number of pixels per square inch [5].
2. Decomposition (Discrete Haar Wavelet Transform): The signal is then
decomposed into a sequence of wavelet transformed coefficients w using Discrete Haar
Wavelet Transform.
3. Thresholding: Wavelet coefficients are modified into another sequence w’
using the process called thresholding. When an image is decomposed into wavelet
coefficients, the values are close or equal to zero. These coefficients can be modified
through thresholding so that the sequence of wavelet coefficients contains long strings of
zeros which will be easy to quantize and entropy encode. There are three basic types of
13 thresholding procedure incorporated in many applications. First being hard thresholding, where a tolerance h is selected and any wavelet coefficient value less than the tolerance value is assigned a zero value. The goal of using this method is to introduce many zeros without the loss of detail. It should be noted that the larger the threshold value is the more errors will be generated in the reconstruction of the image. Second type of thresholding method is called soft thresholding, where a tolerance h is selected and any wavelet coefficients whose value is less than the tolerance value, is assigned a zero value. The rest of the wavelet coefficients larger than h are replaced by the transformation sign(w)×(|w| - h). The third type of thresholding is called quantile thresholding, where a set percentage of coefficients in w matrix are selected to eliminate and without any comparison, the smallest absolute values in this subset are set to zero [5].
4. Quantization: Higher compression ratios can be obtained by quantizing the non-zero wavelet coefficients. During the process of quantization, a sequence of floating numbers in w' are converted to a sequence of integers q. This is simply achieved by rounding the coefficient to the nearest integer. Another way of doing this is by rounding the wavelet coefficient to the closest integer after multiplying each element of w’ by a scalar constant k [5].
5. Entropy Encoding (EZW Encoding & Huffman Encoding): The most essential part of the compression process is entropy encoding where the quantized matrix q is converted into a bit stream e. Wavelet transformation thresholding and quantization do
14 not play any role in compressing the image, the compression begins with entropy encoding. Entropy encoding formulates logic to reduce the reoccurring numbers in q and saving them into bit stream minimizing the storage space [5].
15
Chapter 3
WAVELET-BASED IMAGE COMPRESSION
3.1 Discrete Haar Wavelet Transform
The Haar transform provides a model for wavelet transform of any kind. For an optimal understanding of more complex wavelet transforms, it is important to complete a thorough review of Haar transform. The first Discrete Wavelet Transform (DWT) was invented by the Hungarian mathematician named Alfréd Haar in 1909. Any transform where the wavelets (coefficients) are discretely sampled is called DWT [7].
Haar wavelet transform is performed on an image matrix in order to obtain zero elements by the utilizing a process of averaging and differencing as explained below.
Considerably less memory is required for storing the resulting sparse matrices, meaning matrices with large proportions of zero elements.
Consider the following row vector Y which comes from one row of an 8x8 block of an image:
Y = [ 448 768 704 640 1280 1408 1600 1600 ]
By the definition, if the data string has length equal to 2k, then the transformation process will consist of k steps.
In our case, there will be 3 steps since 8=23.
The elements are paired in two,
(448, 768) (704, 640) (1280 1408) (1600, 1600)
16
The averages of each pairs are formed and are called the approximation coefficients.
(448+768)/2 (704+640)/2 (1280+1408)/2 (1600+1600)/2
(608) (672) (1344) (1600)
Each average is subtracted from the first entry of the pair to form the detail coefficients.
(448-608) (704-672) (1280-1344) (1600-1600)
(-160) (32) (-64) (0)
After being averaged and differenced,
Y1 = [ 608 672 1344 1600 −160 32 −64 0 ]
The first four coefficients of Y1 are called the approximation coefficients and the last four are called the detail coefficients.
Again, averaging and differencing Y1 we get,
Y2 = [ 640 1472 −32 −128 −160 32 −64 0 ]
This gives first two entries 640, 1472 of the new vector Y2. These are the new approximation coefficients. The third and the fourth entries are obtained by subtracting these averages from the first element of each pair which results in the new detail coefficients. The last four entries of Y2 are the same as the detail coefficients of Y1.
For the last step, the first two entries of Y2 are averaged and subtracted from the first entry which results in,
Y3 = [ 1056 −416 −32 −128 −160 32 −64 0 ]
The rest of the elements remain the same as in Y2.
17
In matrix form, Y1 can be obtained from Y by the following equation,
Y1 = Y×W1 where,
1/20001/2000 1/20001/2000− 01/20001/200 01/20001/200− W1 = 001/20001/20 001/20001/20− 0001/20001/2 0001/20001/2−
Similarly, Y2 can be obtained from Y1 by,
Y2 = Y1×W2
where,
1/201/200000 1/201/200000− 01/201/20000 01/201/20000− W2 = 00001000 00000100 00000010 00000001
Again Y3 can be obtained from Y2 by,
Y3 = Y2×W3
18
where,
1/21/2000000 1/21/2000000− 00100000 00010000 W3 = 00001000 00000100 00000010 00000001
We can sum up the entire process as,
Y3 = Y×W1×W2×W3
and,
Y3 = Y×W
where,
1/8 1/8 1/4 0 1/2 0 0 0 1/8 1/8 1/4 0− 1/2 0 0 0 1/8 1/8− 1/4 0 0 1/2 0 0 1/8 1/8−− 1/4 0 0 1/2 0 0 W = W1×W2×W3 = 1/8− 1/8 0 1/4 0 0 1/2 0 1/8−− 1/8 0 1/4 0 0 1/2 0 1/8−− 1/8 0 1/4 0 0 0 1/2 1/8−− 1/8 0 1/4 0 0 0− 1/2
Also note that W is invertible,
-1 -1 -1 -1 W = W1 ×W2 ×W3
which implies that the original Y can be obtained from Y3 by,
Y = W-1×Y3
19
Y3 is the row-transformed matrix when each row is averaged and differenced by
the process explained above. In order to get the final transformation, the matrix needs to
be averaged and differenced column-wise too. Hence the same process is done on each
column.
Mathematically,
T = WT×Y×W
Using the properties of inverse matrix, original image matrix can be retrieved by,
Y = (W-1) T×T×W-1
This process if applied to a 2D image matrix would result into a Discrete Haar
Wavelet Transformation. If the image matrix is averaged and differenced once row-wise
and column-wise, it is called a single level decomposition, if it is averaged and
differenced twice then it is called a two level decomposition and so on.
In fig. 3.1, the image in the upper left-hand corner is comprised of the approximation coefficient matrix. The images in the other three corners come from the detail coefficients. The white outlines in the other three images correspond to large numbers in the detail coefficients. These occur at areas of the image where there is high contrast, and therefore a large difference between neighboring elements in the image matrix [6].
20
Original Image Single Level Decomposition
Two Level Decomposition Three Level Decomposition
Figure 3.1: Pyramidal decomposition of an image
An image can be decomposed into a sequence of different spatial resolution images using the above explained process of averaging and differencing. An N level decomposition when performed results in 3N+1 different frequency bands. It is observed that the increase in the level of decomposition accompanies higher compression ratios
[9].
In the first stage of a discrete wavelet transform, the image is divided into four subbands using separable filters. Each coefficient represents a spatial area corresponding
21 to approximately a 2 × 2 area of the original picture. The low frequencies represent a bandwidth approximately corresponding to 0 < |ω| < π/2, whereas the high frequencies represent the band from π/2 < |ω| < π. The subbands labeled LH1, HL1, and HH1 represent the finest scale wavelet coefficients.
LL1 HL1
LH1 HH1
Figure 3.2: Single level DWT
To obtain the next coarser scale of wavelet coefficients, the subband LL1 is further decomposed and sampled as shown below, a two-scale wavelet decomposition in which the image is divided into four subbands using separable filters. Each coefficient in the subbands LL2, LH2, HL2 and HH2 represents a spatial area corresponding to approximately a 4 × 4 area of the original picture. The low frequencies at this scale represent a bandwidth approximately corresponding to 0 < |ω| < π/4, whereas the high frequencies represent the band from π/4 < |ω| < π/2.
22
LL2` HL2
HL1
LH2 HH2
LH1 HH1
Figure 3.3: Level 2 DWT
LL3 HL3 HL2
LH3 HH3 HL1
LH2 HH2
LH1 HH1
Figure 3.4: Level 3 DWT
23
The process continues until some final scale is reached. At each scale, there are
three subbands; the remaining lowest frequency subband is a representation of the
information at all coarser scales.
3.2 Embedded Zerotrees of Wavelet Transforms Encoding
Embedded Zerotrees of Wavelet (EZW) is one of many lossy compression
algorithms out there. It was developed by Jerome Shapiro in 1993 and it was published
through IEEE Journal. It is a special kind of encoder designed originally to operate on 2D
images and specifically for the use with Wavelet transforms. It utilizes progressive
approach of encoding to compress an image into bit streams. As with an increase in the
number of bits in the stream, the image obtained after the decoding process will be more
detail oriented, this is a property similar to JPEG encoding technique [17].
As quoted by Shapiro [18] in his paper, “The embedded zerotree wavelet
algorithm (EZW) is a simple, yet remarkably effective, image compression algorithm,
having the property that the bits in the bit stream are generated in order of importance,
yielding a fully embedded code.”
By following Shapiro’s Zerotree encoding technique, lower bit rates of an image
are embedded prior to higher bit rates in a bit stream,. This is done in order to obtain the
best image detail quality [22]. One of many advantageous properties of EZW encoding is
observed when an encoder has the capability of terminating the process of encoding at
any point thereby allowing for a target rate set by user or distortion metric to be met
exactly [18].
24
To further improve the compression quality, a new data structure called zerotree
is introduced. First, a threshold T is set using the below mentioned formula, every
wavelet coefficient x is compared with T and if |x| < T it is considered insignificant.
In order to explain the process of zerotree encoding there are two terms that are
essential to understand: parent and children wavelet coefficients. As defined by Shapiro
[18], “The coefficient at the coarse scale is called the parent, and all coefficients
corresponding to the same spatial location at the next finer scale of similar orientation are
called children.” After an establishment of that particular wavelet coefficient to be
insignificant, it is proposed that all the remaining wavelet coefficients having the same
orientation are likely to be insignificant with respect to T. descendants and ancestors are
terms associated with matrices’ parent element and child element respectively. The set of
wavelet coefficients at finer scales (in the same location) are called descendants of that
particular parent. Similarly, the set of wavelet coefficients at coarser scales (in the same
location) are called ancestors of that particular child.
Level 3
Level 2
Level 1
Figure 3.5: Quad trees
25
It is observed that all parents have four children, only exception being the lowest frequency subband, where the parent node has only three children as shown in fig. 3.6.
Figure 3.6: Wavelet coefficients represented in different subbands
3.2.1 EZW Encoding Algorithm
The first step in the EZW encoding algorithm is to determine the initial threshold.
It can be derived mathematically,
[log (MAX |γ(x, y)|)] Initial Threshold, t0 = 2 2
where,
26
MAX(.) = maximum coefficient value in the image matrix
γ(x, y) = wavelet coefficient
After the value of initial threshold is determined dominant pass and subordinate
pass are applied on the image and the threshold is halved after every pass until it goes
below the minimum threshold set. This can be illustrated using simple coding steps as
follows:
threshold = t0;
do
{
dominantpass(image matrix);
subordinatepass(image matrix);
threshold = threshold/2;
}
while (threshold > minthreshold);
In the dominant pass, the image matrix is scanned in Morton scan order and every
coefficient is assigned a symbol based on a criteria. Every coefficient is compared with
the threshold and if found to be greater, it is assigned the symbol P (positive), if not, it is
assigned the symbol N (negative). The symbol T (zerotree root) is assigned when found
that the coefficient is a root of a zerotree and the symbol Z (isolated zero) is assigned if found that the coefficient is smaller than T but is not the root of a zerotree.
27
Wavelet Coefficient
YES NO Is Coefficient Significant?
YES Positive (+) NO NO Does Coefficient YES Descend from a Sign? Zerotree Root?
Code as Code as Predictably Insignificant, Positive ‘P’ Negative ‘N’ Not Coded.
YES Does Coefficient NO Have Significant Descendants?
Code as Code as Isolated Zero ‘Z’ Zerotree Root ‘T’
Figure 3.7: Wavelet coefficients coded as P, N, T, and Z
The dominant pass can be illustrated using simple coding steps as follows: initialize_fifo(); // initializes a FIFO stack to read the matrix elements while (fifo_not_empty) // scans until the FIFO is not empty
{ get_coded_coefficient_from_fifo(); if coefficient was coded as P, N or Z then
28
{
code_next_scan_coefficient();
put_coded_coefficient_in_fifo();
if coefficient was coded as P or N then
{
add abs(coefficient) to subordinate list;
set coefficient position to zero;
}
}
}
The dominant pass follows the subordinate pass which is a basically a refinement process applied on the wavelet coefficients coded as P, N, T and Z in the dominant pass.
First, a subordinate threshold is set which usually is half the current threshold. The
wavelet coefficient is compared with the subordinate threshold, if found greater, its value
is changed by subtracting the subordinate threshold from it and a 1 is outputted, if not, its
value remains the same and a 0 is outputted. This process is carried out on the entire
subordinate list. A subordinate list is obtained by placing all the wavelet coefficients that
were coded as P or N. This is done automatically in the dominant pass. The subordinate
pass can be illustrated using simple coding steps as follows:
subordinate_threshold = current_threshold/2;
// subordinate threshold is initialized
for all elements on subordinate list do
29
{
if (coefficient > subordinate_threshold)
{
output a one;
coefficient = coefficient-subordinate_threshold;
}
else output a zero;
}
It is recommended to use thresholds that are a power of two such as 8, 16, 32 etc., this reduces the subordinate pass to very few logical iterations and the entire process can be executed comparatively faster.
3.2.2 Morton Scan
In majority of encoders out there today raster (row-wise) scan is widely used for image compression algorithm which does not require spatial processing.
Figure 3.8: Raster (row-wise) scan order
30
Morton (Z) scan order on the other hand enables faster and efficient mean
(average) computation of square image blocks. It is shown in fig. 3.8:
Figure 3.9: Morton (Z) scan order
In addition, the pixel index in the Morton sequence helps determine the actual coordinates of a pixel. For example, the pixel with index 21 (image size 8×8) has a binary representation (010101). By partitioning this binary number into two parts (odd and even digits) the row and column coordinates of the pixel can be extracted, respectively (000 =
0, 111 = 7).
3.3 Huffman Encoding
The Huffman coding is a type of entropy encoding algorithm. The Huffman Code has become a valuable asset and was created by Dr. David Huffman during his time spent at Massachusetts Institute of Technology as a graduate student. He would later go on to earn his Ph.D., but he would be well remembered for the term paper "A Method for the
31
Construction of Minimum-Redundancy Codes" with which he invented Huffman coding in 1952 [14].
The Huffman Code is extremely helpful with decreasing the number of required bits for representing a string of symbols, because of this it is one of the most adopted compression techniques. To decrease the number of required bits for a string of symbols, the Huffman Code allows for symbols to be of varying lengths and assigns shorter codes to commonly used symbols and assigns the longer codes to symbols which occur much less frequently within the string. Statistics are used to determine the frequency with which symbols appear [15].
3.3.A Huffman Tree
Huffman tree is formulated by assigning the least number of bits to the most frequently occurring pixel values in the image matrix. The pixel values are stored in binary form, denoting ‘0’ and ‘1’ to represent 8 bit pixel value in case of 8bpp image. As for example, a pixel value of 124 is represented and stored in binary form as 01111100.
The construction of Huffman tree involves the following steps. Two lowest probability parentless nodes are selected and a new node is created which acts as a parent of the two lowest probability nodes. The new node is assigned a probability equal to the sum of its children’s probabilities. This process is carried on until there is only one parentless node remaining. Each symbol (coded in EZW encoding process) is assigned a code by tracing a path from the root of the Huffman tree. There can be only two directions that a branch can turn into, of which one direction path is assigned a ‘1’ and the other direction path is
32
assigned a ‘0’. For example, a symbol which is reached by branching right thrice, then
left and then right is represented as ‘11101’ [15]. The fig. 3.9 (below) depicts codes for nodes of a sample tree.
* 0 1
10 11
110 111
Figure 3.10: Huffman tree [15]
33
Chapter 4
MATLAB IMPLEMENTATION
This chapter discusses the MATLAB implementation of the image compression algorithm discussed earlier. All the steps of compression using various MATLAB functions that I created are listed with detailed explanation. The process of compression is followed by the process of decompression in order to retrieve the original image back and to find the compression ratio.
The image shown below lena.jpg is a 128×128 resolution standard JPEG image which is widely used for testing and development purpose of image processing.
Figure 4.1: Original test image lena.jpg
I have considered a grayscale image for the purpose of compression. For a YCbCr color image having three matrices: Luminance Y component, Chroma Blue difference
34 and Chroma Red difference, the processing needs to be done only on the Y component and the blue and red difference can stay the same.
4.1 Steps of Compression
This section explains the process of image compression that was carried out in
MATLAB. The steps involved in the compression and decompression process are shown below in the form of a flowchart:
Original X Image imread() haardec()
de_x ezw_encode()
DD huffman() Compression Steps encode_x change() encode_bin_x Decompression Steps ihuffman()
decode_x
ezw_decode()
ezw_decode_x ihaar() imshow() re_x Reconstructed
Image
Figure 4.2: Steps of compression
35
4.1.1 Reading the image to be compressed
The original JPEG image lena.jpg is read using MATLAB’s function imread.
When an image is read, it gets converted into a defined matrix of resolution 128×128 in this case. The image matrix is stored into the variable x having width 128 and height 128.
After the image is successfully read, it is then displayed using the function imshow which is used to display any image matrix.
4.1.2 Discrete Haar wavelet transformation
The matrix x is then decomposed into wavelet coefficient matrix de_x using the function haardec. Row-wise and column-wise Discrete Haar Wavelet Transformation is performed on the matrix. The value of level is defined in the function haardec which determines the levels of Haar transformation. The value of level controls the number of times the image will be decomposed. The function haar is called in order to perform arithmetic operation on the matrix. The output matrix is called a coefficient matrix denoted by de_x, which contains wavelet coefficients obtained after Discrete Haar
Wavelet Transformation is applied to the input image matrix. After the image is decomposed into wavelet coefficients, it is then displayed using the function imshow. The resulting images are shown in section 3.3.
4.1.3 EZW encoding of wavelet coefficient matrix
The wavelet coefficient matrix de_x is EZW encoded using the function ezw_encode. In this process, a threshold value is selected and a morton scan order is
36 coded so that the 2D matrix is scanned into a 1D array in a certain way as explained earlier in EZW Encoding method. The function mapping is used to create the morton scan order matrix. Dominant pass and Subordinate pass is done unless the threshold becomes 0.5. The functions named dominantpass and subordinatepass are used for the same purpose.
4.1.4 Huffman encoding
The function huffman creates the compressed image vector encode_x after performing the steps of Huffman encoding.
4.1.5 Writing compressed image
In this step, the image data is written into a text file in order to verify the encoded data.
4.2 Function Flow Diagram
To summarize the process involved in the MATLAB implementation of image compression, I created the function flow diagram as shown below. It contains all the functions used in the MATLAB code and how every function is linked to its parent function. The function main is the parent program code which calls all the listed functions through the nest as shown. The function haardec calls haar function to perform the arithmetic matrix operation. The function ezw_encode calls the functions mapping, dominantpass and subordinatepass. The function dominantpass calls for the functions
37 checkdescendants and checkanscestors. The function ezw_decode calls for the function checkchildren. The function ihaar calls for the function haar.
main.m haardec() haar()
ezw_encode() mapping()
huffman() dominantpass() checkdescendants()
change() subordinatepass() checkanscestors()
ihuffman()
ezw_decode() checkchildren()
ihaar() haar()
Figure 4.3: Function flow diagram
4.3 Simulation Results
Figure 4.4: Original image
38
Figure 4.5: Single level discrete Haar wavelet transform
Figure 4.4: Reconstructed image
39
4.4 Observations
As observed in fig. 4.6 the reconstructed image has visible some data loss which is acceptable because the image size was small and the encoding process require wide data to be able to convert the matrix elements into bit stream.
After performing the compression process on the image and changing the level of
Haar wavelet transformation, I came up with the following results:
Level of Haar Transformation Compression Ratio
1 63.1332
2 30.5389
3 14.5721
Table 4.1: Level of Haar transformation vs. compression ratio
Hence, it is observed that the compression ratio decreases as the level of Haar transformation increases.
4.5 Future Improvements
Inclusion of PSNR calculation for the compressed image and the reconstructed image would have been a great addition to my MATLAB code. It is defined as the measurement of the quality of the reconstructed image. Usually, PSNR values for a lossy
40
image compression range from 30 to 50dB. Higher the PSNR value better details are
observed in the reconstructed image.
In theory, the following MATLAB function would have been able to calculate the
PSNR value of the reconstructed image.
function PSNR_dB = psnr(input_image, output_image)
p = 255; input_image = double(input_image); % promote to double output_image = double(output_image); % promote to double difference = input_image – output_image; diff_square = difference ^ 2;
RMS = sqrt(mean(diff_square(:))); % calculating the RMS value
PSNR_dB = 10*log10(p/RMS);
41
Chapter 5
APPLICATIONS OF WAVELET-BASED IMAGE COMPRESSION
The use of image compression has evolved to acquire a myriad of applications across a varying population of fields. While originally utilized by the field of telecommunications for simple reductions in storage space and increased efficiency, image compression now plays a major role in fields such as medicine, law enforcement, personal consumer products, museums and art preservation, as well as even large retail companies. Image compression today has a whole host of benefits to attract varying users and we are discovering new applications for this technology quite often.
One of data compression's newest and largest consumers is the field of medicine.
With the recent demand to create electronic databases for patient data, medicine faced the daunting issue of storing millions of patient files, x-rays and various other images with limited storage space. Image compression allows medical images to be accessed quickly and frequently and by many different people. In addition, the integrity of the image is preserved ensuring the most accurate data is provided to health care professionals.
In addition to the health care industry, the law enforcement and security industries have looked to image compression to solve their data conundrums. Whether it is private security, government and military branches, or law enforcement, image compression allows for quick access to much more standardized images, all of which is necessary for effective security. Imagine the amount of data that must be stored to simply contain fingerprint scans for the entire population of federal or state prisoners. Image
42
compression ensures that members of law enforcement outfits have access to large
amounts of data without losing the image quality necessary to effectively complete their
duties. While law enforcement has to worry about the sheer volume of data and making this data available to many security officials, another field has adopted the use of image compression for very different reasons.
Art restoration professionals and historical museums are very concerned for the quality of the images they are storing. Image compression provides the means to store many different images and preserve the natural beauty of the art by preserving image quality. More frequently today we see many art galleries displaying images online. The ability to store a large number of these beautiful pieces of art on the internet and allow others to view them without loss of image quality is only possible thanks to the use of image compression.
While there are currently many uses for image compression, it is very possible
that in the near future we will find additional applications we have yet to discover. As our
world continues to revolve more around electronic devices and the desire to access
information faster and more efficiently, more fields will find a need to utilize image
compression to achieve their goals.
43
Chapter 6
CONCLUSION
The initial goal of this project was to create an efficient method of wavelet-based image compression. This was proposed to be achieved by performing Discrete Haar
Wavelet Transformation (DWT) on an image followed by Entropy Encoding techniques including Embedded Zerotree Wavelet (EZW) and Huffman encoding. Not only was this project a success, but I succeeded in furthering my understandings of the application of
Haar wavelet transformation in image compression because the project itself offered many unique learning opportunities and experiences. I came across many tradeoffs between the level of decomposition, the compression ratio and image reconstruction quality.
There were some limitations I encountered during this project that are of importance. In future projects of a similar nature, I may wish to determine a better quantization scheme, compare soft thresholding to the thresholding techniques I used, as well as attempt to calculate the PSNR value of the reconstructed image. While addressing these limitations would have improved the overall quality of the project, the culmination of my efforts has resulted in a superior product by any standard.
44
APPENDIX
MATLAB Code
% file: main.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% author: Shetul Saksena
% term: Spring 2011
% title: Wavelet-based Image Compression
% faculty: Dr Jing Pang
%
% step 1: reading image to be compressed 128×128
% step 2: discrete haar transform
% step 3: ezw encoding of wavelet coefficient matrix
% step 4: huffman encoding
% step 5: writing compressed image
% step 6: huffman decoding
% step 7: ezw decoding
% step 8: inverse haar transform
% step 9: writing reconstructed image to file
% clear all; % clear the workspace, remove all existing variables close all; % close all figures clc; % clear command space
45
% original input image read and displayed
X=imread('lena.jpg');
figure, imshow(X); title('Input Image'); % The original image is displayed
% discrete haar decomposition of the image de_x=haardec(X); figure, imshow(de_x); title('Discrete Haar Wavelet Transform');
% ezw encoding
[DD,L1,SS,L2]=ezw_encode(de_x,6);
% huffman encoding
[encode_x,h,sortindex]=huffman(DD);
encode_bin_x=change(encode_x); dlmwrite('encode1.txt',encode_bin_x,' '); % writing to text file dlmwrite('encode2.txt',SS,' '); % in order to verify encoded data
s=size(encode_bin_x,2)+size(SS,2); % compressed image size
[m,n]=size(X); t=m*n*8; % original size comp_ratio =(s/t)*100 % calculation of compression ratio
46
% huffman decoding
decode_x=ihuffman(encode_x,h,sortindex);
% ezw decoding ezw_decode_x=ezw_decode(de_x,6,decode_x,L1,SS,L2);
% inverse haar transform and reconstructing image re_x=haarrec(ezw_decode_x); re_x=uint8(re_x); figure, imshow(re_x); title('Reconstructed Image');
% file: haardec.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function de_x=haardec(X) c=double(X);
t=1;
level=1; % number of levels of haar transform
h=size(X,2);
while and(h>1,t<=level)
% row-wise haar wavelet transform
for row=1:h
c(row,:)=haar(c(row,:),h);
47
end
% column-wise haar wavelet transform
for col=1:h
c(:,col)=haar(c(:,col),h);
end
h=h/2;
t=t+1; % level increment
end
de_x=c;
% file: haar.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function x=haar(c,h) x=c; sqrt2=sqrt(2);
h=h/2;
for i=1:h
x(i)=(c(2*i-1)+c(2*i))/sqrt2;
x(h+i)=(c(2*i-1)-c(2*i))/sqrt2;
end
48
% file: ezw_encode.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [DD,L1,SS,L2]=ezw_encode(X,LEVEL)
Y0=max(X);
Y1=max(Y0);
% determination of threshold value ---> for i=0:20;
if 2^i<=Y1 & 2^i>0.5*Y1;
threshold=2^i;
initialthreshold=threshold;
laststeplevel=i+1;
break;
end end
% generation of morton scan order ---> sublist=[];
[xx,yy]=size(X);
A=mapping(xx);
[m,n]=size(A); global N; % declare as global variable
N=zeros(m*n,2);
49 for i=1:m,
for j=1:n,
N(A(i,j),1)=i;
N(A(i,j),2)=j;
end end
order=1;
L1=[0];
L2=[0];
DD=[];
SS=[];
% encoding steps --->
% if threshold~=0.5, do dominantpass and subordinatepass. while threshold ~= 0.5 & order <= LEVEL,
%Dominant Pass
[D,X,sublist] = dominantpass(X,threshold,sublist);
DD=[DD,D];
L1=[L1,size(D,2)];
50
%Subordinate Pass
threshold=threshold/2;
if threshold ==0.5,
break;
end
S = subordinatepass(sublist,threshold);
SS=[SS,S];
L2=[L2,size(S,2)];
order=order+1;
end
% file: mapping.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% function to create a Morton Scan Order matrix function A = mapping(n)
If n == 2
A = [1 2; 3 4]; else
B = mapping(n/2);
A = [B B+(n/2)^2; B+(n/2)^2*2 B+(n/2)^2*3]; end
51
% file: dominantpass.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [D,X,sublist] = dominantpass(X,threshold,sublist)
D=[]; global N;
[m,n]=size(X);
% matrix R is a reference matrix, same size as X;
R=zeros(m);
% '0' means this coefficient is not a descendant from zerotree root;
[a,b]=size(N); if abs(X(1,1))>=threshold; % X(1,1) is DC coefficient
sublist=[sublist, abs(X(1,1))];
if X(1,1)>0;
D=[D,'p'];
else
D=[D,'n'];
end
X(1,1)=0; else
D=[D,'z']; end
52
for k=2:4
if abs(X(N(k,1),N(k,2)))>=threshold,
sublist=[sublist, abs(X(N(k,1),N(k,2)))];
% append this significant coefficient to the subordinate list;
if X(N(k,1),N(k,2))>0, % determine the sign
D=[D,'p']; % >0,assign a "p"
else
D=[D,'n']; % <0,assign a "n"
end
X(N(k,1),N(k,2))=0;
else
% 2,3,4 has no parents, check its descendants.
result = checkdescendants( k,X,threshold,0);
if result==1,
D=[D,'z'];
else
D=[D,'t'];
R(N(k,1),N(k,2))=1;
end
end end
53 for k=5:a,
if abs(X(N(k,1),N(k,2)))>=threshold,
sublist=[sublist, abs(X(N(k,1),N(k,2)))];
if X(N(k,1),N(k,2))>0, % determine the sign
D=[D,'p']; % >0,assign a "p"
else
D=[D,'n']; % <0,assign a "n"
end
X(N(k,1),N(k,2))=0;
else
zerotree = checkancestors( k,R,0);
% check coefficient is descended from zerotree root?
if zerotree==0, % Not descends from zerotree root
result = checkdescendants( k,X,threshold,0);
% Check its has significant descendants?
if result==1,
D=[D,'z']; % isolated zero
else
D=[D,'t']; % zerotree
R(N(k,1),N(k,2))=1;% if zerotree, reference matrix coefficient=1
end
end
54
end end
% file: subordinatepass.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function S = subordinatepass(sublist,threshold)
S=[];
[m,n]=size(sublist); for i=1:n;
if bitand(round(sublist(1,i)),threshold)==threshold
S=[S,1];
else S=[S,0];
end end
% file: checkdescendants.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%% function result = checkdescendants(j,X,threshold,result)
% initially set result=0
% if the result=1, means that a coefficient has at least 1 significant descendant. global N
[m,n]=size(N); for i=(4*j-3):4*j
if result==1 | i>m
55
break;
end
if abs(X(N(i,1),N(i,2)))>=threshold
result=1;
break;
else
result=checkdescendants(i,X,threshold,result);
end end
% file: checkancestors.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function zerotree = checkancestors(j,R,zerotree)
% zerotree=1 means coefficient is descended from zerotree root
% initially set zerotree=0 global N; i = floor((j+3)/4 ); while i>1
if R(N(i,1),N(i,2))==1;
zerotree=1;
break;
end i = floor((i+3)/4 );
56
end
% file: huffman.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [encode_x,h,sortindex]=huffman(DD)
a=size(DD,2);
for i=1:a,
if DD(i)=='p';
D(i)=1;
end
if DD(i)=='n';
D(i)=2;
end
if DD(i)=='z';
D(i)=3;
end
if DD(i)=='t';
D(i)=4;
end end
len=length(D); f=[];
57
for i=1:4,
f=[f,size(find(D==i),2)];
end
f=f./len;
% huffman encoding begins -->
sim=find(f~=0);
f=f(sim);
[f,sortindex]=sort(f);
sim=sim(sortindex);
n=length(f); m=zeros(n-1,n); for i=1:n-1
[f,l]=sort(f);
m(i,:)=[l(1:n-i+1),zeros(1,i-1)];
f=[f(1)+f(2),f(3:n),1];
end
for i=1:n-1
c(i,:)=blanks(n*n);
end
c(n-1,n)='0';
c(n-1,2*n)='1';
58 for i=2:n-1
c(n-i,1:n-1)=c(n-i+1,n*(find(m(n-i+1,:)==1))-(n-2):n*(find(m(n-i+1,:)==1)));
c(n-i,n)='0';
c(n-i,n+1:2*n-1)=c(n-i,1:n-1);
c(n-i,2*n)='1';
for j=1:i-1
c(n-i,(j+1)*n+1:(j+2)*n)=c(n-i+1,n*(find(m(n-i+1,:)==1)) - (n-
2):n*(find(m(n-i+1,:)==1)));
n*(find(m(n-i+1,:)==j+1)-1)+1:n*find(m(n-i+1,:)==j+1));
end end for i=1:n
h(i,1:n)=c(1,n*(find(m(1,:)==i)-1)+1:find(m(1,:)==i)*n); end
encode_x=[]; for i=1:a,
if D(i)==1;
encode_x=[encode_x,h(find(sortindex==1),1:4)];
end
if D(i)==2;
encode_x=[encode_x,h(find(sortindex==2),1:4)];
59
end
if D(i)==3;
encode_x=[encode_x,h(find(sortindex==3),1:4)];
end
if D(i)==4;
encode_x=[encode_x,h(find(sortindex==4),1:4)];
end
end
% file: change.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function encode_bin_2=change(encode_x) uint=uint8(encode_x); a=find(uint>=48); y1=uint(a(1:end)); y1=uint(a(1:end)); b=find(y1==49); y2=zeros(1,size(y1,2)); y2(b(1:end))=1; encode_bin_2=y2;
% file: ihuffman.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function decode_x=ihuffman(encode_x,h,sortindex)
60
len=length(encode_x);
decode_x=[]; for i=1:4:len,
if encode_x(1,i:i+3)==h(find(sortindex==1),1:4);
decode_x=[decode_x,'p'];
end
if encode_x(1,i:i+3)==h(find(sortindex==2),1:4);
decode_x=[decode_x,'n'];
end
if encode_x(1,i:i+3)==h(find(sortindex==3),1:4);
decode_x=[decode_x,'z'];
end
if encode_x(1,i:i+3)==h(find(sortindex==4),1:4);
decode_x=[decode_x,'t'];
end
end
% file: ezw_decode.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function ezw_decode_x=ezw_decode(X,LEVEL,DD,L1,SS,L2)
Y0=max(X);
Y1=max(Y0);
61
% determination of threshold --->
for i=0:20;
if 2^i<=Y1 & 2^i>0.5*Y1;
threshold=2^i;
initialthreshold=threshold;
laststeplevel=i+1;
break;
end; end;
global N;
[m,n]=size(N); % the size of initial image, m is the pixels of initial image
XX=zeros(sqrt(m)); % initialize the reconstructured image to zero
threshold=initialthreshold; % initial theshold
sublist=[];
% sublist is the new position matrix for all significant coefficients 'p' and 'n'
f=min(laststeplevel,LEVEL);
for level=1:f
RR=zeros(size(XX));
b=L1(level+1);
end
62
% dominant pass --->
i=1; j=1;
while i<=m
if j>b; % b is the length of dominant pass vector
break;
end
if RR(N(i,1),N(i,2))==1;
i=i+1;
else
if DD(j+sum(L1(1:level)))=='p';
XX(N(i,1),N(i,2))=threshold;
RR(N(i,1),N(i,2))=1;
sublist=[sublist;N(i,1),N(i,2)];
end
if DD(j+sum(L1(1:level)))=='n';
XX(N(i,1),N(i,2))=-threshold;
RR(N(i,1),N(i,2))=1;
sublist=[sublist;N(i,1),N(i,2)];
end
if DD(j+sum(L1(1:level)))=='z';
RR(N(i,1),N(i,2))=1;
end
63
if DD(j+sum(L1(1:level)))=='t';
RR(N(i,1),N(i,2))=1;
RR=checkchildren(i,RR);% all zerotree's descendants are set to 1.
end
i=i+1;
j=j+1;
end end
% subordinate pass --->
[xx,yy]=size(sublist); threshold=threshold/2; for i=1:xx
if level==laststeplevel |threshold==0.5;
break;
end
if SS(i+sum(L2(1:level)))==1;
if XX(sublist(i,1),sublist(i,2))>0;
XX(sublist(i,1),sublist(i,2))= XX(sublist(i,1),sublist(i,2))+ threshold;
else
XX(sublist(i,1),sublist(i,2))= XX(sublist(i,1),sublist(i,2))-threshold;
end
64
end
end
ezw_decode_x=XX;
% file: checkchildren.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [RR]=checkchildren(j,RR)
set(0,'RecursionLimit',10000);
% if a symbol 't' is encountered, then make all its descendants in reference
% matrix RR's components equal 1---ZEROTREES
global N
[m,n]=size(N); for i=(4*j-3):4*j;
if i<=m,
RR(N(i,1),N(i,2))=1;
[RR]=checkchildren(i,RR);
end
end;
% file: ihaar.m %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function x=ihaar(c,h) h1=h/2;
65 for i=1:h1
x(2*i-1)=(c(i)+c(h1+i))/sqrt(2);
x(2*i)=(c(i)-c(h1+i))/sqrt(2); end
66
BIBLIOGRAPHY
[1] Stephen Wolfram, "A New Kind of Science", Wolfram Media, 2002.
[2] Charles Poynton, "Digital Video and HDTV", Morgan Kaufmann, 2003.
[3] Paul S Addison, "The Illustrated Wavelet Transform Handbook: Introductory
Theory and Applications in Science, Engineering, Medicine and Finance", Taylor
& Francis, July 2002.
[4] Kamrul Hasan Talukder and Koichi Harada, "Haar Wavelet Based Approach for
Image Compression and Quality Assessment of Compressed Image", IAENG,
International Journal of Applied Mathematics, Online Publication, February 2007.
[5] Sarah Betz, Nirav Bhagat, Paul Murphy and Maureen Stengler, "Wavelet-based
Image Compression", Elec. 301 Final Project, Rice University.
[6] Greg Ames, "Image Compression Using the Haar Wavelet Transform".
http://online.redwoods.cc.ca.us/instruct/darnold/laproj/Fall2002/ames/thecompsho
w.pdf
[7] Stéphane G. Mallat, "A Wavelet Tour of Signal Processing", Academic Press,
1999.
[8] D. Vijendra Babu and Dr. N. R. Alamelu, "Wavelet based Medical Image
Compression Using ROI EZW", International Journal of Recent Trends in
Engineering, Vol. 1, No. 3, May 2009.
[9] K Kannan and S Arumuga Perumal, "Optimal Decomposition Level of Discrete
Wavelet Transform for Pixel Based Fusion of Multi-focused Images",
67
International Conference on Computational Intelligence and Multimedia
Applications, 2007.
[10] Walker, James S, "A Primer on Wavelets and Their Scientific Applications",
Chapman & Hall/CRC, 1999.
[11] Walnut, David F, "An Introduction to Wavelet Analysis", Birkhauser, 2001.
[12] Edward M. Schwalb, "iTV handbook: technologies and standards", Prentice Hall,
2003.
[13] David Salomon, G. Motta and D. Bryant, "Data Compression: The Complete
Reference", Springer, December 2006.
[14] D. A. Huffman, "A Method for the Construction of Minimum-Redundancy
Codes", Proceedings of the I.R.E., September 1952.
[15] Dipperstein, Michael, "Huffman Code Discussion and Implementation".
http://michael.dipperstein.com/huffman/index.html
[16] Van Fleet, Patrick J, "Discrete Wavelet Transformations", John Wiley & Sons,
Inc., 2008.
[17] Valens, C, "EZW Encoding".
http://polyvalens.pagesperso-orange.fr/clemens/ezw/ezw.html
[18] Shapiro, Jerome M, "Embedded Image Coding Using Zerotrees of Wavelet
Coefficients", IEEE Transactions on Signal Processing, Vol. 41, No. 12,
December 1993.
[19] Tinku Acharya, "JPEG2000 Standard for Image Compression: Concepts,
Algorithms and VLSI Architectures", Wiley-Interscience, October 2004.
68
[20] P. Schelkens, A. Skodras & T. Ebrahimi, "The JPEG 2000 Suite", Wiley, 2009.
[21] David Taubman, "JPEG2000: Image Compression Fundamentals, Standards and
Practice", Springer, November 2001.
[22] Adrian Munteanu, Jan Cornelis, Geert Van der Auwera, and Paul Cristea,
"Wavelet-Based Lossless Compression Scheme with Progressive Transmission
Capability", John Wiley & Sons, Inc., 1999.