Lossless Image Compression Using Matlab: Bachelor Thesis Electrical Engineering June 2020
Lossless Image Compression Using Matlab: Bachelor Thesis Electrical Engineering June 2020
Lossless Image Compression Using Matlab: Bachelor Thesis Electrical Engineering June 2020
Electrical Engineering
June 2020
Bachelor Thesis
Electrical Engineering
June 2020
Contact Information:
Author(s):
Surya Teja Kodukulla
E-mail: suko19@student.bth.se
Supervisor:
Benny Lövström
University Examiner:
Irina Gertsovich
I would like to express my sincere gratitude towards Mr. Benny Lövström for
guiding me through the course and providing all the help I needed and asked
for to complete the project in an efficient manner. I would also like to thank
Irina Gertsovich for guiding me before the start of the thesis work. Her valuable
guidance was essential for the start of my thesis.
I would also like to thank my parents, family, friends and colleagues for sup-
porting and helping us to reach our preset goals and deadlines for this project.
ii
Contents
Abstract i
Acknowledgments ii
1 Introduction 1
1.1 Lossy Compression . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Lossless Compression . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Aims and Objectives . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Research Questions . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 MATLAB as a tool . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Related Work 4
2.1 Photographic data . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.2 Modal Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 Work on Image Compression . . . . . . . . . . . . . . . . . . . . . 4
3 Method 7
3.1 Huffman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
3.2 LZW (Lempel-Ziv-Weich) . . . . . . . . . . . . . . . . . . . . . . 8
3.3 RLE (Run Length Encoding) . . . . . . . . . . . . . . . . . . . . 8
3.4 DCT (Discrete Cosine Transform) . . . . . . . . . . . . . . . . . . 9
3.5 DWT (Discrete Wavelet Transformation) . . . . . . . . . . . . . . 9
4 Implementation 11
4.1 LZW (Lempel-Ziv-Weich) . . . . . . . . . . . . . . . . . . . . . . 11
4.2 RLE (Run Length Encoding) . . . . . . . . . . . . . . . . . . . . 11
4.3 Huffman . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4.4 DCT (Discrete Cosine Transform) . . . . . . . . . . . . . . . . . . 14
4.5 DWT (Discrete Wavelet Transform) . . . . . . . . . . . . . . . . . 17
4.6 Implementation in MATLAB . . . . . . . . . . . . . . . . . . . . 19
5 Results 20
5.1 Results for LZW (Lempel-Ziv-Weich) . . . . . . . . . . . . . . . . 20
5.2 Results for RLE (Run Length Encoding) . . . . . . . . . . . . . . 21
iii
5.3 Results for Huffman . . . . . . . . . . . . . . . . . . . . . . . . . 24
5.4 Results for DCT (Discrete Cosine Transform) . . . . . . . . . . . 27
5.5 Results for DWT (Discrete Wavelet Transform) . . . . . . . . . . 30
5.6 Overview of Results . . . . . . . . . . . . . . . . . . . . . . . . . . 33
6 Discussion 34
References 37
iv
List of Figures
v
5.28 Result of DCT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.29 Flower Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.30 Result of DCT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
5.31 Lamp Post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.32 Result of DCT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
5.33 Berry Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.34 Result of DWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.35 Bird Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.36 Result of DWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
5.37 Dog Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.38 Result of DWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.39 Flower Image . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.40 Result of DWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
5.41 Lamp Post . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
5.42 Result of DWT . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
vi
List of Tables
vii
Chapter 1
Introduction
Image compression is a technique used widely to reduce the image size during
storing and processing of the image. With increasing quality and size of the im-
ages, compression has become essential in day to day life. With the increasing
usage of cloud storage, the compression plays a significant role in storing a large
number of images online [1].
The image compression is efficient when the amount of data which is necessary
to represent an image is reduced, and the space to store them is decreased, and
image transfer efficiency increases.
Different colour channels define the intensity for a pixel location in the image.
The pixel location contains a value which will be converted to an image using
a colour map, by assigning the value to a level in the colour map. grey-scale is
used as the most common colour map in which has all shades from black to white
assigned. Apart from grey-scale images, true-colour images consist of a vector
with Red, Green, Blue components for each pixel position. This type of image
can be considered as an image with three two dimensional planes [2].
The compression techniques are of two types, lossy and lossless compression.
1
Chapter 1. Introduction 2
given transmission and storage. It also minimizes the number of bits required for
an image with loss of information.
Though lossy compression returns a very high reduction in bit rate, as low-quality
images are not preferred in application point of view, the lossless compression is
suitable for broad areas of applications such as Medical, Depth Sensing, Defence
and research purposes [5]. Lossless compression is applied to a wide variety of
files such images, pdf, text documents etc. All lossless compression algorithms
are not applied to images [6]. Hence, this study focuses on the application of
different lossless compression algorithms on images and compare the results for
best quality and compression ratio obtained.
1. To check whether the improvement in the compressed file size for lossless
image compression can be used for better functionality and applications.
2. Can the high-resolution image file size be reduced by using the best com-
pression algorithm after comparing the algorithms?
The lossless compression has also been implemented using the Artificial Neu-
ral Network as a predictor. The prediction process is made lossless by predicting
the integers at compression and decompression stages. However, this method
uses artificial neural networks which may result in good compression ratio but
not available widely [1].
Another method is encryption in JPEG and then compressing the image using
4
Chapter 2. Related Work 5
The hardware implementation for the lossless image compression has also been
tested using Spartan 3 EDK kit. This method improves the compression and
decompression of images, but hardware implementation for everyday image com-
pression is not an optimal method [10].
Encoding of the images has been done using the RLE algorithm by using bit
planes. Higher-level bit planes were encoded using the RLE algorithm, whereas
other bit planes are encoded using arithmetic coding. By combing the arithmetic
encoding and RLE, a high compression efficiency has been achieved. This method
of compression is suited for grey-scale images [11].
LZW algorithm has been implemented for lossy image compression for grey scale
images, with compression efficiency around 40%. The GIF encoder with LZW
algorithm has been implemented to achieve this lossless compression [12].
Huffman encoding has also been used in order to compress the grey-scale im-
ages. This is a lossless compression which reduces the source symbols and then
Huffman coding is used for compression. The method yields 10% more compres-
sion ratio than the usual compression using Huffman encoding [13].
RLE algorithm has been used for semi-lossless compression of images. This
method maps the colours of image to a vector. The RLE is applied on the vector
to obtain a new vector with values of colour and frequency. As the frequencies
are stored in vectors, size of the image is reduced [14].
The implementation of RLE algorithm has been made for Computer Generated
Hologram (CGH). The recurrence of the patterns in the CGH algorithm to gener-
ate images. The calculation time is improved by 88% by using RLE algorithm [15].
Bit Back Coding combined with HiLLoC was tested for lossless compression. This
method was targeted for codecs available on ImageNet images. This method can
also be combined with machine learning for better compression ratios [6].
The lossy image compression using Huffman encoding has been used in medical
Chapter 2. Related Work 6
image compression. The Huffman encoding is used for compressing the grey-scale
scanned images for storing and research purposes [16]. Wavelet compression is
also used for image denoising and compression using MATLAB for biomedical
research applications [17].
In the previous implementations, the lossless image compression has been tested
using a particular algorithm or method. Most of the implementations were made
for grey-scale images. As colour images are widely used for most of the applica-
tions, the lossless compression for colour images is considered in this study. This
study compares the lossless compression algorithms Huffman, LZW, RLE, DCT
and DWT to determine the optimum algorithm to retain the quality of the image
as efficiently as possible. The subjective quality is taken into consideration for
comparing the results obtained for each algorithm.
Chapter 3
Method
The algorithms are executed using MATLAB coding. The Linnaeus 5 data-set
[18] has been used to test the algorithms of lossless compression which are Huff-
man, LZW (Lempel-Ziv-Weich), RLE (Run Length Encoding), DCT (Discrete
Cosine Transform) and DWT (Discrete Wavelet Transform). Each algorithm will
be used to compress the images provided in the Linnaeus 5 data-set, which con-
sists of the following category of images
1. Berries
2. Birds
3. Dogs
4. Flowers
5. Portraits
6. Other images such as signboards, lamp posts, water bodies etc.
These images are of 256 x 256 size in terms of resolution. MATLAB is chosen
for implementation as the image processing toolbox will compile the algorithms
faster, and the functions in MATLAB will provide a superior method for process-
ing the images. The algorithms are implemented as follows:
3.1 Huffman
Huffman encoding is based on the probability of occurrence of values in a given
data. It assigns a variable-length code for various characters, and the frequency
of occurrence of a character will decide the code length. The characters which
occur most frequently have the codes shorter lengths, and the characters which
occur less frequently have codes with longer lengths [19]. In order to compress
the data, two trees are created.
1. Huffman Tree
2. Transverse Tree
Based on the probability of occurrence of the characters, the characters are re-
ordered in descending order. As per these probabilities, a new set of codes are
generated and again rearranged. The process continues until the grouping comes
7
Chapter 3. Method 8
The algorithm works by reducing the physical size of the string of repeating
data which is called a run. The run is encoded into two bytes
The first byte stores the run count, which is the number of values in the run. The
run count contains n-1 characters if the encoded run contains n characters.
The second byte represents the run value, which is the value of the character
present in the run. The value will be in the range of 0 to 255.
The two bytes combined is called an RLE Packet. The RLE Packets are gener-
ated for every new value or if the number of characters in a run exceeds the limit.
RLE has three methods of compressing the data.
1. Encoding along X-axis
2. Encoding along Y-axis
3. Zig-zag Encoding
Chapter 3. Method 9
Zig-zag Encoding
This type of encoding is used for encoding a bitmap into two dimensional instead
of scanning only one dimension. This encoding method scans the map in a diag-
onal fashion. The scanning begins from the upper left corner and continues in a
diagonally Zig-zag manner till the end of bottom right corner [23].
Sequential encoding is best suited for compressing text data containing char-
acter strings or sequence. In order to compress image files, the encoding must be
done in two dimensions, and Zig-zag encoding is best suited for it [2].
the shape of the function. Wavelet transformation gives the same information as
short-time Fourier transformation along with wavelet properties [27]. The DWT
compression can be implemented at different levels of decomposition of wavelets
of the image. Haar wavelet decomposition and compression are chosen for this
study as the computation time is short for Haar wavelet and does not require
multiplications. It consists of many zero elements, so adding elements is easier to
Haar wavelet [28].
Chapter 4
Implementation
The algorithms have been implemented in the MATLAB. To execute the algo-
rithms, the same set of images have been used for compression in order to compare
the results.
In the decoding process, the code generated for the elements is used to generate a
dictionary with an image vector based on the concatenation. The redundancy is
reduced by using the code generated, and the similar elements are not repeated
by using new values. This reduces the size of the image [31]. Flowgraph in figure
4.1 describes the working of the algorithm.
11
Chapter 4. Implementation 12
decomposed into wavelets, and the data from the wavelets is quantised. The RLE
algorithm is then applied to these images using Zig-Zag encoding, as mentioned
in section 3.3 [23].
The image is reconstructed by using inverse Zig-Zag decoding, which has the
encoded image and the dimensions of the image as inputs. The Zig-Zig decoding is
possible due to the quantisation of the wavelet data as it has discrete values. The
Zig-Zag decoded data will still be in wavelet format, which will be reconstructed
to get the compressed image using RLE. Flowgraph in figure 4.2 describes the
working of the algorithm.
Chapter 4. Implementation 13
4.3 Huffman
The implementation is done, as mentioned in section 3.1. The image is encoded
based on the colours present in the image, and the codes are created for each
colour. Huffman codes are created based on the frequency of occurrence of each
colour. This reduces the size of the image without any loss in the subjective
quality of the image [16].
To compress the images using DCT algorithm, 8 x 8 pixel block case is cre-
ated to find the DCT coefficients. The entries of the pixel block case are either
0 or 1. This pixel block case is used for masking the image matrix. The image
matrix is multiplied with the DCT matrix and its transpose. The DCT matrix is
provided in the image processing toolbox in MATLAB, which is an n x n discrete
matrix. The result of this matrix multiplication is multiplied with a pixel block
case, i.e., the masking matrix and the resultant matrix is again multiplied with
the DCT matrix and its transpose [33] [34]. This results in the compressed image
matrix with higher energy coefficients in the upper left corner of the DCT matrix.
The masking matrix is shown in figure 4.5.
Chapter 4. Implementation 15
As the elements in the masking matrix other than the upper left corner are
zero, only the higher energy coefficients are compressed with produces a lossless
compressed image. When combined with coding also of the error residual se-
quence, this method of implementation in DCT produces a lossless image [35]
[36]. Flowgraph in figure 4.4 describes the working of the algorithm.
To perform the wavelet transformation, four coefficient matrices cA, cH, cV, cD
which correspond to level 1 approximation, horizontal details, vertical details,
diagonal details respectively. Similarly, level 2 decomposition can also be made
with the approximations from level 1. The data from level 2 decomposition along
with level 1 decomposition is stored in a vector. After multilevel decomposi-
tion, the original image is synthesized or reconstructed based on the coefficients
from the vector, which consists of the decomposed data [39]. Based on this data
decomposed data the image can be compressed by using wavelet toolbox in MAT-
LAB with commands ’ddencmp’ and ’wpdencmp’. After compression of the
Chapter 4. Implementation 18
decomposed data stored as vectors, the images are reconstructed based on the
sion using decomposed, the data obtained after first level decomposition is also
compressed along with the second level decomposed data in the vector in order to
prevent any subjective deterioration of the image [39] [40]. Flowgraph in figure
4.6 describes the working of the algorithm.
20
Chapter 5. Results 21
The subjective image quality is similar to that of the original image used for
compression. The time taken for compiling the code increases with increasing
size and resolution of the image.
The Fourier coefficients with higher energy are compressed in order to retain
the image quality. The low energy coefficients are either retained or modified to
return zero to make the compression lossless and prevent data loss.
In order to compare the lossless algorithms that can be used for image compres-
sion, different implementations of image compression have been studied, and the
lossless algorithms that can be modified for colour image compression were im-
plemented.
The LZW algorithm was unable to return the lossless compression of images,
combinations with other methods like splitting into wavelets, combining with
RLE were experimented, but the implementation was not successful. RLE, Huff-
man algorithms were originally returning grey-scale images. The RLE algorithm
was modified by quantizing the data and then encoded using the algorithm. The
algorithm encoded only the rows of the image matrix, and implementation did not
convert it into a two-dimensional image with a colour map. The reconstruction
of the image could not be done in the beginning as there were no specifications of
how the image should be formed. The algorithm was then extended for columns,
and the Zig-Zag encoding was used to encode and decode the two-dimensional
image. The algorithm was then able to return a colour image.
The Huffman algorithm at first could not return the colour images similar to
RLE. As the algorithm is based on probabilities, the final codes obtained were
needed to be decoded and then converted to coordinates of the image. This con-
version was not made efficiently, and different functions were implemented to get
the reconstructed lossless image. The decoded codes were then converted to cell
array, which then was used to reconstruct the image.
The DCT was modified to be made into lossless compression by modifying the
masks used for encoding the image for compression. The DWT was used by using
only one level of wavelet transformation. It was then modified to transform the
image again without losing quality.
34
Chapter 7
Conclusions and Future Work
7.1 Conclusion
The DWT algorithm produces lossless images with decent compression ratios for
all kinds of images that were used in this study. Images with higher resolu-
tion were also compressed without any interruption in compiling the algorithm.
Though the compression ratios were a bit low compared to Huffman encoding,
the DWT algorithm is more reliable for high resolution image compression. The
execution of the DWT algorithm is also simple without major complications and
complexity compared to other algorithms used in this study. The DCT algorithm
also produces lossless compressed images with descent compression ratios but
the matrix conversion and compression of coefficients may become complex while
performing compression for lager number of high resolution images. Modifying
the DCT algorithm is also complex as the changing the masking matrix may not
return lossless compression.
For Huffman and RLE algorithms, though the compression ratios are bit higher
than DWT and DCT algorithms, the compiling is not stable for increasing reso-
lution of the images. The execution of Huffman encoding may encounter sudden
pause or termination in compilation without any output. This makes the Huffman
algorithm unreliable for lossless compression of high resolution of images. In order
to produce lossless colour images using RLE algorithm, the execution of the al-
gorithm is very complex with many functions, encoding and decoding techniques
to be implemented. Compression of high resolution images fails in most of the
cases with implementation of RLE algorithm. As LZW algorithm produces the
binary images as output, it cannot be considered as lossless and it requires modi-
fications to implement lossless compression and generate colour images as output.
Therefore, in study lossless compression algorithms have been compared and con-
sidering the subjective image quality it can be concluded that DWT algorithm
is best suited for lossless compression of high resolution images by considering
the simple execution and implementation of the algorithm in MATLAB without
much complexity. The file size can be reduced after lossless compression with de-
35
Chapter 7. Conclusions and Future Work 36
cent compression ratios. The subjective image quality after lossless compression
and reduction of file sizes remains the same as that of the original images. The
DCT algorithm can be combined with other algorithms or the matrix implemen-
tation can be changed without making the implementation complex. The RLE
and Huffman can be modified to perform lossless compression for high resolution
images without any interruptions and complexities.
The lossless algorithms were just compared in this study. However, the algo-
rithms can be combined with techniques like Machine Learning, Neural Networks
or combining them with other methods of image compression to produce more
efficient compression. The lossless compression can also be extended for the video
files, by compressing them frame by frame. The compression can also be tested
for compression speeds to modify the algorithms for faster compression and better
transmissions over a network.
References
[1] Fabian Mentzer, Eirikur Agustsson, Michael Tschannen, Radu Timofte, and
Luc Van Gool. Practical full resolution learned lossless image compression.
In Proceedings of the IEEE Conference on Computer Vision and Pattern
Recognition, pages 10629–10638, 2019.
[2] Chris Solomon and Toby Breckon. Fundamentals of Digital Image Processing:
A practical approach with examples in Matlab. John Wiley & Sons, 2011.
[3] Kenta Kurihara, Shoko Imaizumi, Sayaka Shiota, and Hitoshi Kiya. An
encryption-then-compression system for lossless image compression stan-
dards. IEICE transactions on information and systems, 100(1):52–56, 2017.
Publisher: The Institute of Electronics, Information and Communication
Engineers.
[6] James Townsend, Thomas Bird, Julius Kunze, and David Barber. HiLLoC:
Lossless Image Compression with Hierarchical Latent Variable Models. arXiv
preprint arXiv:1912.09953, 2019.
[8] Bin Xiao, Gang Lu, Yanhong Zhang, Weisheng Li, and Guoyin Wang. Loss-
less image compression based on integer Discrete Tchebichef Transform. Neu-
rocomputing, 214:587–593, 2016. Publisher: Elsevier.
[9] Bogdan Rusyn, Oleksiy Lutsyk, Yuriy Lysak, Adolf Lukenyuk, and Lubomyk
Pohreliuk. Lossless image compression in the remote sensing applications.
37
References 38
[11] Med Karim Abdmouleh, Atef Masmoudi, and Med Salim Bouhlel. A new
method which combines arithmetic coding with rle for Lossless image com-
pression. 2012. Publisher: Scientific Research Publishing.
[12] S. W. Chiang and L. M. Po. Adaptive lossy LZW algorithm for palettised
image compression. Electronics Letters, 33(10):852–854, 1997. Publisher:
IET.
[13] Paul G. Howard and Jeffrey Scott Vitter. Parallel lossless image compression
using Huffman and arithmetic coding. In Data Compression Conference,
1992., pages 299–308. IEEE, 1992.
[14] Rafeeq Al-Hashemi, Ayman Al-Dmour, Fares Fraij, and Ahmed Musa. A
Grayscale Semi-Lossless Image Compression Technique Using RLE. Journal
of Applied Computer Science & Mathematics, (10), 2011.
[17] Vipul Sharan, Naveen Keshari, and Tanay Mondal. Biomedical image de-
noising and compression in wavelet using MATLAB. International Journal
of Innovative Science and Modern Engineering (IJISME) ISSN, pages 2319–
6386, 2014. Publisher: Citeseer.
[20] Rachit Patel, Virendra Kumar, Vaibhav Tyagi, and Vishal Asthana. A fast
and improved Image Compression technique using Huffman coding. In 2016
References 39
[23] Amit Birajdar, Harsh Agarwal, Manan Bolia, and Vedang Gupte. Im-
age Compression using Run Length Encoding and its Optimisation. In
2019 Global Conference for Advancement in Technology (GCAT), pages 1–6.
IEEE, 2019.
[24] Nageswara Rao Thota and Srinivasa Kumar Devireddy. Image Compression
Using Discrete Cosine Transform. page 9.
[25] Andrew B. Watson. Image compression using the discrete cosine transform.
Mathematica journal, 4(1):81, 1994. Publisher: Redwood City, Ca.: Ad-
vanced Book Program, Addison-Wesley Pub. Co., c1990-.
[28] Kamrul Hasan Talukder and Koichi Harada. Haar Wavelet Based Ap-
proach for Image Compression and Quality Assessment of Compressed Im-
age. page 8.
[30] Memon Nasir, Guillemot Christine, and Ansari Rashid. 5.6 - The JPEG
Lossless Image Compression Standards. In AL BOVIK, editor, Handbook of
Image and Video Processing (Second Edition), Communications, Networking
References 40
and Multimedia, pages 733 – 745. Academic Press, Burlington, second edition
edition, 2005.
[31] Tretter Daniel, Memon Nasir, and Charles A. Bouman. 5.7 - Multispectral
Image Coding. In AL BOVIK, editor, Handbook of Image and Video Process-
ing (Second Edition), Communications, Networking and Multimedia, pages
747 – 760. Academic Press, Burlington, second edition edition, 2005.
[33] Navpreet Saroya and Prabhpreet Kaur. Analysis of image compression algo-
rithm using DCT and DWT transforms. International Journal of Advanced
Research in Computer Science and Software Engineering, 4(2), 2014.
[34] Nushwan Y. Baithoon. Combined DWT and DCT Image Compression Us-
ing Sliding RLE Technique. Baghdad Science Journal, 8(3):832–839, 2011.
Publisher: Baghdad University.
[36] Giridhar Mandyam, Nasir Ahmed, and Neeraj Magotra. Lossless Image
Compression Using the Discrete Cosine Transform. Journal of Visual Com-
munication and Image Representation, 8(1):21 – 26, 1997.
[37] D. Mehta and K. Chauhan. Image Compression using DCT and DWT-
Technique. International Journal of Engmeenng Sciences & Research Tech-
nology, 2(8):2133–2139, 2013.
[40] Ya-feng LIU, Bo-qing XU, and Jia JIA. Application of Wavelet Transform
in Image Compression Based on Matlab [J]. Computer and Modernization,
6, 2005.