Image Compression using Haar Wavelet Transform
To quote Stefan Banach, ‘Mathematics is the most beautiful and the most powerful creation of the human spirit.’ This might be a controversial statement, but rest assured, we can all agree upon the importance of mathematics in our everyday lives. From electrical engineering to fiscal policies, mathematics plays a pivotal role in almost every field the human mind can think of. Image compression is one such field where the applications of mathematics are abundant.
It is safe to say that the role of digital technology in our world is growing exponentially. This rise in digital technology also impacted the use of digital media, especially digital images. The photos we see on the pages of internet and the pictures we take with our mobile phones and cameras are some examples of digital images. But these images require a lot of storage space and take time when sent through the internet. This limitation motivated the need to compress images. The technology of image compression has revolutionized the way we communicate, store and use data in the digital world as well as the real. In this regard, wavelets provide a mathematical way of compressing numerical information (data) so that it can be stored using a lot less space than the original data, and in such situations where storage is limited, data compression is very useful. Haar wavelet transform uses the properties of linear algebra and is one of the many ways to compress images.
The Haar wavelet was introduced by Alfred Haar and is known as the first ever wavelet. Digital images require a lot of storage space and time because these images have redundant data. Data redundancy is a condition created within a database or data storage technology in which the same piece of data is kept in two separate places. By using compression techniques, we can compress images in a way that the pixels are compressed and the image quality is not all that affected. This speeds up the process of sending images through the internet.
There are two types of compressions: lossless and lossy. Data compressed with the former can be recovered with absolutely no loss in information. On the other hand, data compressed with the latter is altered in some way. Better results are obtained using lossy compression, but these come at the expense of lost information from the original data. Lossy compression utilizes the Haar wavelet transform.
To motivate compression through Haar wavelet transform, let us consider eight numbers (100, 200, 44, 50, 20, 20, 4, 2). These numbers could be grayscale intensities. Grayscale intensities range from 0 to 255, where 0 is the most intense black and 255 is the most intense white. Suppose we are to send those eight numbers over the internet to a friend, we can use the following transformation:
(a,b) --> ( (b+a)/2, (b-a)/2)
By using this transformation, we arrive at eight new numbers which are (150, 47, 20, 3 | 50, 3, 0, -1). One may notice that the original list can be easily derived using these 8 numbers. The differences in this new list tell us the trends in the data. Huge differences indicate large jumps in data while small differences point out that there is less change in that portion of the input. Suppose we convert the last three values in our new list to zero since they have very small values, we could transmit (150, 47, 20, 3 | 50, 0, 0, 0) over the internet to our friend. Our friend who receives this list could invert the process and arrive at (100, 200, 47, 47, 20, 20, 3, 3). The compressed list is very close to the original list of numbers! A similar technique is used to compress images. Large images are condensed into much smaller ones by removing the data that isn’t needed to retain the image.
Matrices are used to compress images. A matrix is a rectangular array of numbers with dimensions (m x n). A matrix ‘W’ can be formed in such a way that the matrix product computes the transformation mentioned previously. This matrix will have averages as its first half and differences as its second half. Let us consider a vector ‘v’ with 8 entries. The product of the matrix ‘W’ and the vector ‘v’ gives us ‘y’ which contains the averages and differences of the entries of ‘v’ (Wv = y).
The matrix ‘W’ has an interesting property, i.e. the inverse of the matrix can be computed by doubling the transpose (W^-1 = 2W^T). Using this property, the inverse of ‘W’ can be easily calculated and multiplied with ‘y’ to get back the original vector ‘v’. ((W^-1)y = v)
An orthogonal matrix is a square matrix with real entries where columns and rows are orthogonal unit vectors. The inverse of an orthogonal matrix is equal to the transpose (U^-1 = U^T). If we multiply the matrix ‘W’ with the square root of two, we satisfy the condition for orthogonality and arrive at the following matrix:
This matrix is defined as the Haar transformation matrix and now since ‘W’ is orthogonal, the inverse of ‘W’ is equal to the transpose of ‘W’.
The first half rows of the Haar transformation matrix produce weighted averages of the input while the lower half produce weighted differences. The weight factor in both cases is the square root of two. The filter used to build the top half of Haar transformation matrix is called the Haar filter. This filter is also called the lowpass filter since it averages pairs of numbers. The filter used to build the second half of the matrix is called the Haar transform filter. It is also called the highpass filter and processes data in a manner that is opposite to that of lowpass filters.
We now have all the requirements to compress digital grayscale images. If a grayscale image (Figure 1) is stored in a matrix ‘A’ with dimensions (m x n), we first compute WA (Figure 2). This matrix multiplication applies W to each column of ‘A’ and hence the output is a matrix of dimensions (m x n) where the top half of the rows are weighted averages and the bottom half are weighted differences. Now since the Haar wavelet transform is applied to the columns, it must be applied to the rows of the matrix as well. We compute this by multiplying the product of the matrix ‘A’ and the Haar transformation matrix by the transpose of Haar transformation matrix. This gives us a matrix ‘B’ such that B = WAW^T (Figure 3). The two dimensional Haar wavelet transform of the image has most of the energy conserved in the upper left-hand corner of the transform while the remaining three quarters contain values that are zero or close to zero.
Figure 1: A gray scale input image
Figure 2: The grayscale image multiplied with the Haar transformation matrix
Figure 3: The grayscale image multiplied with the Haar transformation matrix and its transpose
To analyze this, let ‘H’ denote the top half of HWT and ‘G’ denote the bottom half. The transformation can now be expressed as:
HAH^T, HAG^T, GAH^T, GAG^T are the four blocks of the wavelet transform ‘B’. Recall that ‘H’ denotes weighted averages and ‘G’ denotes weighted differences.
The upper left-hand block is HAH^T in which the columns of ‘A’ are averaged by the multiplication of ‘H’ while the rows are averaged by the multiplication of H^T. Thus, this block is the approximation of the entire image. The upper right-hand block is HAG^T: H averages the columns and G^T differences the rows. The upper right-hand corner holds information about the vertical transitions in the image. Large values indicate a large vertical change as we move across the image while small values indicate little vertical change. The lower left-hand block is GAH^T in which the columns of ‘A’ are differenced by ‘G’ and the rows are averaged by H^T. This block contains information about the horizontal transitions. Just like the vertical transition, large values indicate large horizontal change as we move down the image and small values indicate little horizontal change. The lower right-hand corner GAG^T differences both the columns and rows of the matrix. This block measures changes along the diagonal of the image. All in all, the upper right-hand corner approximates the entire image while the remaining three blocks measure changes in the vertical, horizontal and diagonal directions of the matrix.
If there is no change in the image, or very little change, the difference blocks will have zero or values close to zero, respectively. The near-zero values can be converted to zero. By doing so, we compress the image and the storage space of the image is drastically reduced. We can iterate the process to compress the image even further. Most of the high intensities are contained in the blur portion of the transformation. We can iterate and apply the HWT to the blur portion of the transform. Hence, we replace the blur by its transformation! The process is completely invertible - we apply the inverse HWT to the transform of the blur to obtain the blur. Then we apply the inverse HWT to obtain the original image. We can continue this process if we desire. Figure 4 and Figure 5 show two iterations and three iterations of the Haar wavelet transform respectively.
Figure 4: Two iterations of the Haar Wavelet Transform
Figure 5: Three iterations of the Haar Wavelet Transform
Altogether, image is something that is very useful and accessed by everyone in today’s world. The Haar wavelet transform is a powerful tool which could be used to compress images by multiplying the Haar transformation matrix and its transpose to an image represented by a matrix so that they are easy to store and send.
Image compression is very helpful and is used by NASA. Image and video compression is a very critical technology for many operations in NASA. Firstly, compression saves a lot of space. NASA receives millions of bits of data each day that require a huge storage facility. Image compression helps save enormous amount of hard drive space. Secondly, image compression saves transmission time. The NASA Mars Rovers sends back pictures and data which could take up to years to reach Earth if uncompressed due to the massive distance between the two. Compression saves a lot of money by saving hard drive space and time.
Mathematics has a lot of applications in very diverse fields and digital image compression is one of them. Math has changed the way we store and send images. It is fascinating to see how simple matrix operations have such wide applications. The Haar wavelet transform is a basic yet very powerful tool which compresses redundant data. Digital images can now be sent across the globe and beyond conveniently. The ease of data compression has brought tremendous advancements not just to photography and videography but also to scientific research. Mathematics has and will always continue to improve the world we live in. It is sheer folly to say that mathematics is of no use.
...(download the rest of the essay above)