application to image compression

Upload: jeysam

Post on 02-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 Application to Image Compression

    1/21

    Application to image compression

    This is a picture of a famous mathematician: Emmy Noether compressed in different ways

    Introduction When retrieved from the Internet, digital images take a considerableamount of time to download and use a large amount of computer memory. The Haarwavelet transform that we will discuss in this application is one way of compressingdigital images so they take less space when stored and transmitted. As we will seelater, the word ``wavelet stands for an orthogonal basis of a certain vector space.

    The basic idea behind this method of compression is to treat a digital image as anarray of numbers i.e., a matrix. Each image consists of a fairly large number of littlesquares called pixels (picture elements). The matrix corresponding to a digital imageassigns a whole number to each pixel. For example, in the case of a 256x256 pixelgray scale image, the image is stored as a 256x256 matrix, with each element of thematrix being a whole number ranging from 0 (for black) to 225 (for white). The JPEGcompression technique divides an image into 8x8 blocks and assigns a matrix to each

    block. One can use some linear algebra techniques to maximize compression of theimage and maintain a suitable level of detail

  • 8/10/2019 Application to Image Compression

    2/21

    Vector transform using Haar Wavelets Before we explain the transform of a

    matrix, let us see how the wavelets transform vectors (rows of a matrix). Suppose

    is one row of an 8x8 image matrix. In general, if the data string has length equal to 2 k ,then the transformation process will consist of k steps. In the above case, there will be3 steps since 8=2 3.

    We perform the following operations on the entries of the vector r: 1. Divide the entries of r into four pairs: (420, 680), (448, 708), (1260, 1410),

    (1600, 600).2. Form the average of each of these pairs:

    These will form the first four entries of the next step vector r 1.

  • 8/10/2019 Application to Image Compression

    3/21

    3. Subtract each average from the first entry of the pair to get the numbers:

    .

    These will form the last four entries of the next step vector r 1.

    4. Form the new vector:

    .

    Note that the vector r 1 can be obtained from r by multiplying r on the right by thematrix:

    The first four coefficients of r 1 are called the approximation coefficients and the lastfour entries are called the detail coefficients.

    For our next step, we look at the first four entries of r 1 as two pairs that we take theiraverages as in step 1 above. This gives the first two entries: 564, 1470 of the newvector r 2. These are our new approximation coefficients. The third and the fourthentries of r 2 are obtained by subtracting these averages from the first element of each

    pair. This results in the new detail coefficients: -14, -130. The last four entriesof r 2 are the same as the detail coefficients of r 1:

  • 8/10/2019 Application to Image Compression

    4/21

    Here the vector r 2 can be obtained from r 1 by multiplying r 1 on the right by thematrix:

    For the last step, average the first two entries of r 2, and as before subtract the answerfrom the first entry. This results in the following vector:

    As before, r 3 can be obtained from r 1 by multiplying r 2 on the wright by the matrix:

  • 8/10/2019 Application to Image Compression

    5/21

    As a consequence, one gets r 3 immediately from r using the following equation

    Let

    .

    Note the following: The columns of the matrix W 1 form an orthogonal subset of R

    8 (the vector space ofdimension 8 over R); that is these columns are pair wise orthogonal (try their dot

    products). Therefore, they form a basis of R8. As a consequence, W 1 is invertible. Thesame is true for W 2 and W 3.

    As a product of invertible matrices, W is also invertible and its columns form

    an orthogonal basis of R8

    . The inverse of W is given by:

    The fact the W is invertible allows us to retrieve our image from the compressed formusing the relation

    Suppose that A is the matrix corresponding to a certain image. The Haar transform iscarried out by performing the above operations on each row of the matrix A andthen by repeating the same operations on the columns of the resulting matrix. Therow-transformed matrix is AW . Transforming the columns of AW is obtained bymultiplying AW on the left by the matrix W T (the transpose of W ). Thus, the Haartransform takes the matrix A and stores it as W T AW . Let S denote the transformedmatrix:

  • 8/10/2019 Application to Image Compression

    6/21

    Using the properties of inverse matrix, we can retrieve our original matrix:

    This allows us to see the original image (decompressing the compressed image).

    Let us try an example.

    Example Suppose we have an 8x8 image represented by the matrix

    The row-transformed matrix is

    Transforming the columns of L is obtained as follows

  • 8/10/2019 Application to Image Compression

    7/21

    The point of doing Haar wavelet transform is that areas of the original matrix that

    contain little variation will end up as zero elements in the transformed matrix. Amatrix is considered spar se if it has a high proportion of zero entries. Sparsematrices take much less memory to store. Since we cannot expect the transformedmatrices always to be sparse, we decide on a non-negative threshold value known as, and then we let any entry in the transformed matrix whose absolute value is lessthan to be reset to zero. This will leave us with a kind of sparse matrix. If is zero,we will not modify any of the elements.

    Every time you click on an image to download it from the Internet, the sourcecomputer recalls the Haar transformed matrix from its memory. It first sends the

    overall approximation coefficients and larger detail coefficients and a bit later thesmaller detail coefficients. As your computer receives the information, it beginsreconstructing in progressively greater detail until the original image is fullyreconstructed.

    Linear algebra can make the compression process faster, more efficientLet us first recall that an nxn square matrix A is called orthogonal if its columns forman orthonormal basis of Rn, that is the columns of A are pairwise orthogonal and thelength of each column vector is 1. Equivalently, A is orthogonal if its inverse is equalto its transpose. That latter property makes retrieving the transformed image via the

    equation

    much faster.

  • 8/10/2019 Application to Image Compression

    8/21

    Another powerful property of orthogonal matrices is that they preserve magnitude. Inother words, if v is a vector of Rn and A is an orthogonal matrix, then || Av||=|| v||. Hereis how it works:

    This in turns shows that || Av||=|| v||. Also, the angle is preserved when thetransformation is by orthogonal matrices: recall that the cosine of the angle betweentwo vectors u and v is given by:

    so, if A is an orthogonal matrix, is the angle between the two vectors Au and Av,then

    Since both magnitude and angle are preserved, there is significantly less distortion produced in the rebuilt image when an orthogonal matrix is used. Since the

  • 8/10/2019 Application to Image Compression

    9/21

    transformation matrix W is the product of three other matrices, one cannormalize W by normalizing each of the three matrices. The normalized versionof W is

    Remark If you look closely at the process we described above, you will notice thatthe matrix W is nothing but a change of basis for R8. In other words, the columnsof W form a new basis (a very nice one) of R8. So when you multiply avector v (written in the standard basis) of R8 by W , what you get is the coordinatesof v in this new basis. Some of these coordinates can be neglected using ourthreshold and this what allows the transformed matrix to be stored more easily andtransmitted more quickly.

    Compression ratio If we choose our threshold value to be positive (i.e. greater thanzero), then some entries of the transformed matrix will be reset to zero and thereforesome detail will be lost when the image is decompressed. The key issue is then tochoose wisely so that the compression is done effectively with a minimumdamage to the picture. Note that the compression ratio is defined as the ratio ofnonzero entries in the transformed matrix ( S=W T AW) to the number of nonzeroentries in the compressed matrix obtained from S by applying the threshold . http://aix1.uottawa.ca/~jkhoury/haar.htm

    http://www.whydomath.org/node/wavlets/hwt.html

    Image Compression:How Math Led to the JPEG2000 Standard

    http://aix1.uottawa.ca/~jkhoury/haar.htmhttp://aix1.uottawa.ca/~jkhoury/haar.htmhttp://www.whydomath.org/node/wavlets/hwt.htmlhttp://www.whydomath.org/node/wavlets/hwt.htmlhttp://www.whydomath.org/node/wavlets/hwt.htmlhttp://aix1.uottawa.ca/~jkhoury/haar.htm
  • 8/10/2019 Application to Image Compression

    10/21

    Haar Wavelet Transformation

    Introduction The easiest of all discrete wavelet transformations is the Discrete Haar Wavelet Tranformation (HWT). Let's motivate it'sconstruction with the following example:

    Suppose you had the eight numbers 100, 200, 44, 50, 20, 20, 4, 2 (these could be grayscale intensities) and you wanted to sendan approximation of the list to a friend. Due to bandwidth constraints (this is a really old system!), you are only allowed to send yourfriend four values. What four values would you send your friend that might represent an approximation of the eight given values?

    There are obviously many possible answers to this question, but one of the most common solutions is to take the eight numbers,two at a time, and average them. This computation would produce the four values 150, 47, 20, and 3. This list would represent anapproximation to the original eight values.

    Unfortunately, if your friend receives the four values 150, 47, 20, and 3, she has no chance of producing the original eight valuesfrom them - more information is needed. Suppose you are allowed to send an additional four values to your friend. With thesevalues and the first four values, she should be able to reconstruct your original list of eight values. What values would you send her?

    Suppose we sent our friend the values 50, 3, 0, and -1. How did we arrive at these values? They are simply the directed distances

    from the pairwise average to the second number in each pair: 150 + 50 = 200, 47 + 3 = 50, 20 + 0 = 20, and 3 + (-1) = 2. Note that ifwe subtract the values in this list from the pairwise averages, we arrive at the first number in each pair: 150 - 50 = 100, 47 - 3 = 44,20 - 0 = 20, and 3 - (-1) = 4. So with the lists (150,47,20,3) and (50,3,0,-1), we can completely reconstruct the original list(100,200,44,50,20,20,4,2).

    Given two numbers a and b, we have the following transformation :

    (a, b) ( (b + a)/2, (b - a)/2 )

    We will call the first output the average and the second output the difference .

    So why would we consider sending (150,47,20,3 | 50, 3, 0, -1) instead of (100,200,44,50,20,20,4,2)? Two reasons quickly come to

    mind. The differences in the transformed list tell us about the trends in the data - big differences indicate large jumps betweenvalues while small values tell us that there is relatively little change in that portion of the input. Also, if we are interested in lossycompression, then small differences can be converted to zero and in this way we can improve the efficiency of the coder. Supposewe converted the last three values of the transformation to zero. Then we would transmit (150, 47, 20, 3 | 50, 0, 0, 0). The recipientcould invert the process and obtain the list

    (150-50, 150+50, 47-3, 47+3, 20-0, 20+0, 3-0, 3+0) = (100,200,44,50,20,20,3,3)

    The "compressed" list is very similar to the original list!

    Matrix FormulationFor an even-length list (vector) of numbers, we can also form a matrix product that computes this transformation. For the sake ofillustration, let's assume our list (vector) is length 8. If we put the averages as the first half of t he output and differences as the

    second half of the output, then we have the following matrix product:

    W 8 v = 1 2 0 0 0 1 2 0 0 0 1 2 0 0 0 1 2 0 0 0 0 1 2 0 0 0 1 2 0 0 0 1 2 0 0 0 1

    2 0 0 0 0 1 2 0 0 0 1 2 0 0 0 1 2 0 0 0 1 2 0 0 0 0 1 2 0 0 0 1 2 0 0 0 1 2 0 0 0 1 2

  • 8/10/2019 Application to Image Compression

    11/21

    v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8 = (v 1+v 2)

    2 (v 3+v 4) 2 (v 5+v 6) 2 (v 7+v 8) 2 (v 2v 1) 2 (v 4v 3) 2 (v 6v 5) 2 (v 8v 7) 2 =y

    Inverting the Process

    Inverting is easy - if we subtract y5 from y1, we obtain v1. If we add y5 and y1, we obtain v2. We can continue in a similar manneradding and subtracting pairs to completely recover v . We can also write the inverse process as a matrix product. We have:

    W 81 y =1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0

    0 1 1 ( v 1+v 2) 2 (v 3+v 4) 2 (v 5+v 6) 2 (v 7+v 8) 2 (v 2v 1)

    2 (v 4v 3) 2 (v 6v 5) 2 (v 8v 7) 2 = v 1 v 2 v 3 v 4 v 5 v 6 v 7 v 8

    =v

    The matrix W 8 satisfies another interesting property - we can compute the inverse by doubling the transpose! That is,

    W 81 =2W 8T

    For those of you who have taken a linear algebra course, you may remember that orthogonal matrices U satisfy U 1=U T . We almost

    have that with our transformation. Indeed if we construct W 8= 2W 8 , we have

    W 81 = = = = = = ( 2W 8 )1 (1 2) W 81 (1 2)2 W 8T 2W 8T ( 2W 8T )T W 8T

    Haar Wavelet Transform DefinedWe will define the HWT as the orthogonal matrix described above. That is, for N even, the Discrete Haar Wavelet Transformation isdefined as

    W N = 2 2 0 0 2 2 0 0 2 2 0 0 2 2 0 0 0 2 2 0 0

    2 2 0 0 2 2 0 0 2 2 0 0 0 2 2 0 0 2 2 0 0 2 2 0 0 2 2

    and the inverse HWT is W N 1=W NT . Analysis of the HWT

    The first N/2 rows of the HWT produce a weighted average of the input list taken two at a time. The weight factor is 2 . The last

    N/2 row of the HWT produce a weighted difference of the input list taken two at a time. The weight factor is also 2 .

    We define the Haar filter as the numbers used to form the first row of the transform matrix. That is, the Haar filter is h= h0 h 1 =

    2 2 2 2 . This filter is also called a lowpass filter - since it averages pairs of numbers, it tends to reproduce (modulo the 2 )two values that are similar and send to 0 to numbers that are (near) opposites of each other. Note also that the sum of the filter

    values is 2 .

    We call the filter that is used to build the bottom half of the HWT a highpass filter . In this case, we have g= g 0 g 1 = 2 2 2

    2 . Highpass filters process data exactly opposite of lowpass filters. If two numbers are near in value, the highpass filter will return

  • 8/10/2019 Application to Image Compression

    12/21

    a value near zero. If two numbers are (near) opposites of each other, then the highpass filter will return a weighted version of one ofthe two numbers.Fourier Series From the Filters

    An important tool for constructing filters for discrete wavelet transformations is Fourier series. To analyze a given filter h=(h0 h 1 h 2h L), engineers will use the coefficients to form a Fourier series

    H ( )=h0+h1ei +h2e2i + +h Le Li and then plot the absolute value of this series. It turns out that we can identify lowpass filters and highpass filters from these graphs.The plots for the filters for the HWT H ( )= 2 2+2 2ei and G( )=2 2+2 2ei appear below:

    H ( ) G( )

    Note that the first graph has value 2 at 0 and H ( )=0 . The graph for the highpass filter is just the opposite - G(0)=0 and G( )

    = 2 . This is typical of lowpass and highpass filters. We can also put other conditions on these graphs and that is often how moresophisticated lowpass/highpass filter pairs for the DWT are defined.HWT and Digital ImagesHow do we apply the HWT to a digital grayscale image? If the image is stored in matrix A with even dimensions M x N, then thenatural thing to try is to compute W M A. We can view this matrix multiplication as W M applied to each column of A so the outputshould be an M x N matrix where each column is M/2 weighted averages followed by M/2 weighted differences. The plots belowillustrate the process:

    A digital image. Fullsize version W 160 A. Fullsize version

    We have used the Haar matrix to process the columns of image matrix A. It is desirable to process the rows of the image as well. Weproceed by multiplying W M A on the right by W NT . Transposing the wavelet matrix puts the filter coefficients in the columns andmultiplication on the right by W NT means that we will be dotting the rows of W M A with the columns of W NT (columns of W N ). So the twodimensional HWT is defined as:

    B=W M AW NT The process is illustrated below.

    http://www.whydomath.org/node/wavlets/blowup/haarimage.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarimage.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarimage.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarleftwt.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarleftwt.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarleftwt.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarimage.html
  • 8/10/2019 Application to Image Compression

    13/21

    The two-dimensional HWT . Fullsize version

    Analysis of the Two-Dimensional HWTYou can see why the wavelet transformation is well-suited for image compression. The two-dimensional HWT of the image has mostof the energy conserved in the upper left-hand corner of the transform - the remaining three-quarters of the HWT consists primarilyof values that are zero or near zero. The transformation is local as well - it turns out any element of the HWT is constructed fromonly four elements of the original input image. If we look at the HWT as a block matrix product, we can gain further insight about thetransformation.

    Suppose that the input image is square so we will drop the subscripts that indicate the dimension of the HWT matrix. If we use H todenote the top block of the HWT matrix and G to denote the bottom block of the HWT, we can express the transformation as:

    B=WAW T = H G A H G T = H G A H T GT = HA GA H T GT = HAH T GAH T HAGT GAGT

    We now see why there are four blocks in the wavelet transform. Let's look at each block individually. Note that the matrix H isconstructed from the lowpass Haar filter and computes weighted averages while Gcomputes weighted differences.The upper left-hand block is HAH T - HA averages columns of A and the rows of this product are averaged by multiplication with H T .Thus the upper left-hand corner is an approximation of the entire image. In fact, it can be shown that elements in the upper left -handcorner of the HWT can be constructed by computing weighted averages of each 2 x 2 block of the input matrix. Mathematically, themapping is

    a c b d 2 ( a + b + c + d )/4The upper right-hand block is HAGT - HA averages columns of A and the rows of this product are differenced by multiplicationwith GT . Thus the upper right-hand corner holds information about vertical in the image - large values indicate a large verticalchange as we move across the image and small values indicate little vertical change. Mathematically, the mapping is

    a c b d 2 ( b + d - a - c)/4The lower left-hand block is GAH T - GA differences columns of A and the rows of this product are averaged by multiplication with H T .Thus the lower left-hand corner holds information about horizontal in the image - large values indicate a large horizontal change aswe move down the image and small values indicate little horizontal change. Mathematically, the mapping is

    a c b d 2 ( c + d - a - b )/4The lower right-hand block is differences across both columns and rows and the result is a bit harder to see. It turns out that thisproduct measures changes along 45-degree lines. This is diagonal differences. Mathematically, the mapping is

    a c b d 2 ( b + c - a - d )/4To summarize, the HWT of a digital image produces four blocks. The upper-left hand corner is an approximation or blur of theoriginal image. The upper-right, lower-left, and lower-right blocks measure the differences in the vertical, horizontal, and diagonaldirections, respectively.

    http://www.whydomath.org/node/wavlets/blowup/haarwt.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwt.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwt.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwt.html
  • 8/10/2019 Application to Image Compression

    14/21

    Iterating the ProcessIf there is not much change in the image, the difference blocks are comprised of (near) zero values. If we apply quantization andconvert near-zero values to zero, then the HWT of the image can be effectively coded and the storage space for the image can bedrastically reduced. We can iterate the HWT and produce an even better result to pass to the coder. Suppose we compute the HWTof a digital image. Most of the high intensities are contained in the blur portion of the transformation. We can iterate and apply theHWT to the blur portion of the transform. So in the composite transformation, we replace the blur by its transformation! The processis completely invertible - we apply the inverse HWT to the transform of the blur to obtain the blur. Then we apply the inverse HWT toobtain the original image. We can continue this process as often as we desire (and provided the dimensions of the data are divisibleby suitable powers of two). The illustrations below show two iterations and three iterations of the HWT.

    Two iterations of the HWT. Fullsize version Three iterations of the HWT Fullsize version

    The iterated HWT is an effective tool for conserving the energy of a digital image. The plot below shows the energy distribution forthe original image (green), one iteration of the HWT (brown), and three iterations of the HWT (orange). The horizontal scale is pixels(there are 38,400 pixels in the thumbnail of the image). For a given pixel value p , the height represents the percentage of energystored in the largest p pixels of the image. Note that the HWT gets to 1 (100% of the energy) much faster than the original image

    and the iterated HWT is much better than either the HWT or the original image.SummaryThe HWT is a wonderful tool for understanding how a discrete wavelet tranformation works. It is not desirable in practice becausethe filters are too short - since each filter is length two, the HWT decouples the data to create values of the transform. In particular,each value of the transform is created from a 2 x 2 block from the original input. If there is a large change between say row 6 androw 7, the HWT will not detect it. The HWT also send integers to irrational numbers and for lossless image compression, it is crucialthat the transform send integers to integers. For these reasons, researchers developed more sophisticated filters. Be sure to checkout the other subsections to learn more other types of wavelet filters.

    Energy distribution for the image and HWTs.

    http://www.whydomath.org/node/wavlets/blowup/haarwtiterated2.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwtiterated2.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwtiterated2.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwtiterated3.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwtiterated3.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwtiterated3.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwtiterated3.htmlhttp://www.whydomath.org/node/wavlets/blowup/haarwtiterated2.html
  • 8/10/2019 Application to Image Compression

    15/21

    http://www.cs.ucf.edu/~mali/haar/

    An Introduction to Wavelets and the Haar Transform

    by Musawir Ali

    In this article, I will present an introduction to wavelets and the 1D HaarTransform. Then I will show how the 1D Haar Transform can easily beextended to 2D. This article assumes that the reader has general knowledgeabout basis functions. If not, please look at my introductory article on basisfunctions before reading this one.

    What are Wavelets?

    Wavelets are a set of non-linear bases. When projecting (or approximating) afunction in terms of wavelets, the wavelet basis functions are chosenaccording to the function being approximated. Hence, unlike familiesof linear bases where the same, static set of basis functions are usedfor every input function, wavelets employ a dynamic set of basis functionsthat represents the input function in the most efficient way. Thus wavelets areable to provide a great deal of compression and are therefore very popular inthe fields of image and signal processing.

    Lets get in to the details of how this dynamic set of basis functions is chosenand how the input function is transformed into wavelets.

    Lets suppose that we have a string of numbers: ( 2, 2, 2, 2 ) and we want totransmit this over a network. Of course, we would like to do this in the fastestpossible way, which implies that we want to send the least amount of datapossible. So lets consider our options. Trivially, one of our options is to justsend all the four numbers, i.e., send the first '2', then the second '2', then thethird, and lastly the fourth. In doing so, we are implicitly choosing thefollowing basis:

    http://www.cs.ucf.edu/~mali/haar/http://www.cs.ucf.edu/~mali/haar/http://www.cs.ucf.edu/~mali/haar/
  • 8/10/2019 Application to Image Compression

    16/21

    But as you would suspect, this is not the best way of doing things. Can we dobetter? The trick is to choose a basis that represents our data efficiently andin a very compact fashion. Notice that our data is pretty uniform; in fact it is

    just a constant signal of 2. We would like to exploit this uniformity. If wechoose the basis vector , we can represent our data by just onenumber! We would only have to send the number 2 over the network, and ourentire data string could be reconstructed by just multiplying (or weighting)with the basis vector . This is great, but we still need three morebasis vectors to complete our basis since the space in our example is 4dimensional. Remember, that all basis vectors have to be orthogonal (orperpendicular). This means that if you take the dot (or scalar) product of anytwo basis vectors, the result should be zero. So our task is to find a vectorthat is orthogonal to . One such vector is . If youtake the dot product of these two vectors, the result is indeed zero.Graphically, these two vectors look like this:

    Notice that graphically these basis vectors look like waves, hence the namewavelets. Now that we have two basis vectors, we need two more. Haarconstructed the remaining basis vectors by a process of dilation and shifting .Dilation basically means squeezing ; therefore the remaining basis vectorswere constructed by squeezing and shifting. If we squeeze the vector , we get . The 1, 1 pair gets squeezed in to a single 1, andsimilarly the -1, -1 pair becomes a single -1. Next, we perform a shift on theresultant basis vector and get: which is our final basis vector.Graphically, these two vectors look like this:

  • 8/10/2019 Application to Image Compression

    17/21

    We now have a complete basis for our four dimensional space, comprised ofthe following basis vectors or wavelets .

    Take time to convince yourself that all four of these vectors are perpendicularto each other (take the dot product and see if it is zero). Even though these

    basis vectors are orthogonal, they are not orthonormal . However, we caneasily normalize them by calculating the magnitude of each of these vectorsand then dividing their components by that magnitude.

    --> magnitude = sqrt((1) 2 + (1) 2 +(1) 2 + (1) 2) = 2

    -->

    --> magnitude = sqrt((1) 2 + (1) 2 +(-1) 2 + (-1) 2) = 2

    -->

    --> magnitude = sqrt((1) 2 + (-1) 2 +(0) 2 + (0) 2) = 2

    -- >

    --> magnitude = sqrt((0) 2 + (0) 2 +(1) 2 + (-1) 2) = 2

    -- >

    Now that we have our basis, let us look at an example of how we can project

  • 8/10/2019 Application to Image Compression

    18/21

    an input vector in to wavelets. This is also known as the 1D Haar Transform.

    1D Haar Transform

    Suppose our input vector is: . To project this in to wavelets, wesimply take a dot product of the input vector with each of the basis vectors.

    dot ( , ) = 8

    dot ( , ) = -2

    dot ( , ) = 2/2

    dot ( , ) = 0

    Thus the input vector got transformed in to . Notice the 4thcomponent is 0! This means that we do not need the 4 th basis vector, we canreconstruct our original input vector with just the first three basis vectors. Inother words, we dynamically chose 3 basis vectors from a possible 4according to our input.

    8 * =

    -2 * =

    2/2 *

    =

    add the vectors =

    Until now, we had a 4 component input vector, and a corresponding set of 4component basis vectors. But what if we have a larger input vector, say with8 components? Would we need to find the new basis vectors? The answer is

  • 8/10/2019 Application to Image Compression

    19/21

    no. We can use the smaller basis that we already have. In fact, we can usethe simplest wavelet basis which consists of: and . These are the smallest wavelets, notice you cannot squeeze them anyfurther. However in choosing these smaller basis vectors for larger input, wecan no longer do the Haar wavelet transform in one pass as we did earlier. Wewill have to recursively transform the input vector until we get to our finalresult. As an example, let us use the simple, 2 component basis to transformthe 4 component input vector that we had in our previous example. Thealgorithm is outlined below, and our example is traced along side.

    # Example

    Input Vector:

    Transformed Vector (basiscoefficients):

    < ? , ? ,? , ? > (we are calculatingthis)

    Basis Vectors (wavelets): ,

    1. If size of input vector is less thansize of basis vector, place input intransformed vector and exit

    Size of input vector = 4Size of basis vector = 24 > 2, so continue

    2. Break input vector in to parts ofsize of the wavelet:

    ,

    3. Take dot product offirst basis

    with every part:

    dot( , )= 6/2 dot( , )= 10/2

    4. Take dot product of second basiswith every part:

    dot( , )= 2/2 dot( , )= 0

    Result of 3. is the new InputVector:

    Result of 4. fills part of theTransformed Vector:

  • 8/10/2019 Application to Image Compression

    20/21

    5. Go to 1. Size of input vector = 2Size of basis vector = 22 = 2, so continue

    dot( , ) = 8

    dot( , ) = -2

    Input Vector:

    Updated Transformed Vector: < ? , -2, 2/2, 0>

    Size of input vector = 1

    Size of basis vector = 21 < 2, so place input intransformed vector and exit

    Transformed Vector (final result):

    This algorithm is very simple. If you think about it, all it does is take the sumsand differences of every pair of numbers in the input vector and divides themby square root of 2. Then, the process is repeated on the resultant vector ofthe summed terms. Following is an implementation of the 1D Haar Transform

    in C++

    /* Inputs: vec = input vector, n = size of input vector */ void haar1d( float *vec, int n){

    int i=0;int w=n;float *vecp = new float [n];for (i=0;i 1){

    w/=2;for (i=0;i

  • 8/10/2019 Application to Image Compression

    21/21

    }

    for (i=0;i