introduction jpeg image compression › darnold › math45 › laproj › f09 › isnyder ›...

17
Introduction Compression Compressible Image Types JPEG Technique The Discrete Cosine . . . Quantization Reordering Huffman Coding Complete Compression . . . Conclusion Home Page Title Page JJ II J I Page 1 of 17 Go Back Full Screen Close Quit JPEG Image Compression Ian Snyder December 14, 2009 Abstract This paper will outline the process of JPEG image compression and the use of linear algebra as part of this process. It will introduce the reasons for image compression and clearly demonstrate each step of the process used by JPEG as a sample image is compressed. Introduction In 1986 a committee known as the Joint Photographic Experts Group (JPEG) met to discuss and form a standard for image compression since most computers simply weren’t capable of handling the large files required for storing images. A universal standard was needed as well since products from different manufacturers of electronics needed to be interoperable [11]. A paper by the chairmain of the group, Gregory Wal- lace, on the JPEG’s compression process was subsequently published in 1991, outlining their proposed standard of image compression. Adopted in 1994, JPEG’s standard file compression format has become so widespread that it is now one of the most common image file types on the web.

Upload: others

Post on 07-Jun-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 1 of 17

Go Back

Full Screen

Close

Quit

JPEG Image Compression

Ian Snyder

December 14, 2009

Abstract

This paper will outline the process of JPEG image compression and the useof linear algebra as part of this process. It will introduce the reasons for imagecompression and clearly demonstrate each step of the process used by JPEG as asample image is compressed.

Introduction

In 1986 a committee known as the Joint Photographic Experts Group (JPEG) metto discuss and form a standard for image compression since most computers simplyweren’t capable of handling the large files required for storing images. A universalstandard was needed as well since products from different manufacturers of electronicsneeded to be interoperable [11]. A paper by the chairmain of the group, Gregory Wal-lace, on the JPEG’s compression process was subsequently published in 1991, outliningtheir proposed standard of image compression. Adopted in 1994, JPEG’s standard filecompression format has become so widespread that it is now one of the most commonimage file types on the web.

Page 2: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 2 of 17

Go Back

Full Screen

Close

Quit

Figure 1: Generalized Compression

This begs the question, why is JPEG’s standard so successful? The answer is sim-ple. The compression technique employed by JPEG allows a large image file to becompressed down to a much smaller size while retaining a substantial amount of theintegrity and quality of the image. The degree of compression in JPEG’s process canbe modified to suit the needs of the individual who is compressing the image, and itis possible to compress an image to one eighth or one ninth of its original size whileretaining enough quality for a decent image [1].

Compression

File compression basically has one goal: take a large file and condense it into a morecompact form for easier storage and/or transport. Image files are much larger thantext files on average, and therefore have a much greater need for compression. Figure 1briefly illustrates the idea behind compression of data for storage or transport and thenthe resulting decompression.

Many image compression techniques have been developed along two main lines:‘Lossless’ compression and ‘Lossy’ compression. ‘Lossless’ image compression takes

Page 3: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 3 of 17

Go Back

Full Screen

Close

Quit

image data and compacts it as best it can while retaining all of the information. ‘Lossy’image compression, on the other hand, is just what it sounds like: information is lostduring compression. This technique generally results in better compression since someinformation deemed uneccessary is discarded, resulting in an image with a drasticallyreduced file size. Since information is lost, this compression method would seem toresult in an image of poorer quality, but the beauty of JPEG compression is that muchof the visual information that is discarded is imperceptible to the human eye, so theresulting image can virtually be of the same quality.

Compressible Image Types

JPEG image compression is suitable for any type of bitmap image. Bitmap images arebasically m × n matrices (or layers of m × n matrices) with each entry in the matrixdetermining the color of one pixel in the image. Each entry, in turn, is represented bya given number of bits. If each pixel was represented by one bit, only two differentcolors could be represented since a computer working in binary could either assign thepixel a value of 0 or 1. If two bits were used to represent each pixel, four colors couldbe represented. The number of possible colors per pixel has 2n possibilities, with nrepresenting the number of bits per pixel. Images with either eight bits per pixel (onebyte) or twenty-four bits per pixel (three bytes) are most common [10].

Three different types of bitmap images are generally used. The first type are in-tensity (grayscale) images, where each entry in the matrix has a value between zeroand one, with zero being pure white and one being completely black. Next, there are256-color images, where each entry in the matrix is a digit between 0 and 255 thatcorresponds to a distinct color. In this case each pixel will require eight bits, or onebyte, of storage. The third image type is called truecolor. In truecolor images threematrices are used: one for the shade of red, another for the shade of green, and a thirdfor the shade of blue (See Figure 2). Truecolor images are also known as RGB (Red,

Page 4: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 4 of 17

Go Back

Full Screen

Close

Quit

Green, Blue) images because of this. A truecolor image is made by layering these threem× n matrices on top of each other, producing the required color for each pixel [9].

JPEG Technique

The method employed by JPEG exploits the limitations of the human eye to detectcolor and brightness changes in an image. When dealing with very bright or very dimcolors, the human eye cannot easily detect changes in the color from pixel to pixel,especially if the change is rather large. JPEG’s process takes advantage of this factand discards imperceptible changes in the color (or chrominance) of an image as well assome changes in the brightness (or luminance), which the human eye is a little betterat detecting. This basis for compression makes JPEG’s method ideal for compressingphotographs, which often have large variations in color and brightness from pixel topixel.

There are four main steps in the process of JPEG compression: the Discrete CosineTransform (DCT), quantization, reordering, and Huffman coding. The purpose of theDiscrete Cosine Transform is to change the matrix from its pixel color values into amatrix of values that describe the change of color from pixel to pixel, both by rowand by column. The quantization process then discards picture detail that is eithernearly or completely imperceptible to the human eye, and the quantized matrix is thenreordered and Huffman encoded to compress the file even further. All of these processeswill be performed step by step as the ‘Seal’ intensity image is compressed using JPEG’smethod. Since there is no color to deal with, the compression only has to deal with thebrightness, or luminance, coefficients. See Figure 3.

Page 5: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 5 of 17

Go Back

Full Screen

Close

Quit

Figure 2: Illustration of Layered Matrices in RBG Images

Page 6: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 6 of 17

Go Back

Full Screen

Close

Quit

Figure 3: Seal Image

Page 7: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 7 of 17

Go Back

Full Screen

Close

Quit

The Discrete Cosine Transform

The Discrete Cosine Transform (DCT) is the heart of JPEG’s compression technique.While it performs no compression by itself, it allows the image matrix to be changedinto a form that is more suitable for compression. A DCT matrix represents an image asa linear combination of sinusoidal functions of varying frequencies in two dimensions,changing an image from its spatial domain to its frequency domain [5]. In essence,the matrix no longer represents the intensity of each pixel directly, but instead it willrecords the changes in intensity across the picture. Large changes in intensity willcorrespond to higher DCT values, and lower changes will correspond to lower values.The two-dimensional DCT used in JPEG compression is defined as follows:

Cuv = αuαv

M−1∑m=0

N−1∑n=0

Amncosπ(2m+ 1)u

2Mcos

π(2n+ 1)v2N

,

where 0 ≤ u ≤M − 1 and 0 ≤ v ≤ N − 1.

αu =

{1/√M, if u = 0,√

2/M, if 1 ≤ u ≤M − 1αv =

{1/√M, if v = 0,√

2/M, if 1 ≤ v ≤M − 1

This equation produces DCT matrix C. The actual DCT of an image matrix isdefined as:

D = CACT ,

where A is the input image matrix and D is the final DCT of the image.Now, it would require quite a bit of computing power to perform this transform

directly on the 1152 × 1536 ‘Seal’ image, so JPEG’s method instead chops the image

Page 8: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 8 of 17

Go Back

Full Screen

Close

Quit

matrix into 8×8 blocks. Each block is then modified using the discrete cosine transform,which requires much less computing power. The DCT also has the quality that it tendsto concentrate the greater changes in intensity in the upper-left corner of the matrix,and this will prove to be vital in the next step of compression. The 8× 8 matrix in thetop left corner of the original image will be used as the example for JPEG’s method.This matrix is shown directly below.

O =

182 181 176 178 176 177 173 175182 184 177 176 179 174 176 173176 176 176 180 178 174 171 168182 174 176 180 177 169 168 168180 172 181 178 171 170 173 172181 170 171 170 170 178 174 174176 174 170 170 171 182 179 166176 173 170 176 173 181 179 168

Generally the original 8 × 8 matrices are shifted so that the mean of the entries

rests around 0 instead of 128. This simply requires that 128 be subtracted from everyentry in the matrix. This computation is simple and is easily reversed when the imageis reconstructed, so it is rather trivial to show the output here. The DCT can then beperformed on the original matrix from the ‘Seal’ image, and the result is shown below.

Page 9: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 9 of 17

Go Back

Full Screen

Close

Quit

D =

376.125 13.589 −.991 10.167 .375 6.701 .546 −2.0619.642 9.971 −1.753 −7.701 6.326 −6.778 −2.985 1.7795.244 −6.027 2.058 7.203 −7.092 −3.058 −3.761 −4.346.483 −2.142 8.938 −.529 −.659 .101 5.106 .05161.125 3.511 −1.915 −4.799 4.375 2.963 .929 −.199−3.622 .352 −2.448 3.881 .0422 −.700 .328 −1.942−2.037 −3.090 −1.011 .226 2.802 .224 2.942 −2.403−.345 −9.409 −3.362 −2.921 −3.739 1.894 .156 −.241

As mentioned previously, the greatest changes in luminance are concentrated in the

upper left corner. Another crucial property of the DCT that needs to be mentionedis that it is invertible, and the inverse DCT will allow the image to eventually bereconstructed from its compressed form.

Quantization

Quantization is where compression really happens. As described before, when theimage was sent throught the discrete cosine transform, most of the visually importantinformation was stored in the coefficients in the upper-left corner of the matrix. JPEGnow gets rid of the excess information found in the bottom-right part of the matrixby employing a ‘quantization matrix.’ Each coefficient in in the transformed imagematrix is divided by the corresponding coefficient of the quantization matrix, and thenrounded to the nearest integer value. This produces many zero coefficients in thequantized matrix.

R = round

(Dij

Qij

)

Page 10: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 10 of 17

Go Back

Full Screen

Close

Quit

While there are many quantization matrices that are employed in JPEG compres-sion, the one most commonly used for the luminance coefficients (and therefore mostuseful for the ‘Seal’ intensity image) is the following:

Q =

16 11 10 16 24 40 51 6112 12 14 19 26 58 60 5514 13 16 24 40 57 69 5614 17 22 29 51 87 80 6218 22 37 56 68 109 103 7724 35 55 64 81 104 113 9249 64 78 87 103 121 120 10172 92 95 98 112 100 103 99

This matrix can be scaled to achieve either a higher degree of compression or a

higher quality image. When the DCT matrix D of the ‘Seal’ image is quantized, thefollowing matrix is produced. Notice how sparse it is with almost all of the lower-rightcontaining zero entries.

R =

24 1 0 1 0 0 0 01 1 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 0

Page 11: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 11 of 17

Go Back

Full Screen

Close

Quit

Figure 4: Zig-Zag Reordering Sequence

Reordering

The 8× 8 matrix has now been DCT’ed and quantized, resulting in the sparse matrixabove. It can now be taken and reordered into a 64 × 1 vector that will concentrateall the non-zero entries from the matrix in the first few entries of the new vector. Thestring of zeros that follows the last non-zero entry can then be eliminated since it isunnecessary information. This will decrease the file size drastically. The reorderingpattern is shown in Figure 4.

The zig-zag reordering pattern turns the 8 × 8 matrix into a long string of matrix

Page 12: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 12 of 17

Go Back

Full Screen

Close

Quit

coefficients, which will be shown as a row vector.

V = [24 1 1 0 1 0 1 0 0 0 0 0 ...]

All the zeros following the final ‘1’ entry in the vector will be eliminated, and an‘end of block’ placeholder will be added to signify that the string is from one 8 × 8matrix.

Huffman Coding

Now that the matrix has been quantized, reordered, and excess strings of zeros havebeen deleted, it is ready for Huffman coding. It is rather trivial to try to apply Huffmancoding to a single block of the compressed image matrix, but it is an important part ofcompression and will be briefly explained. Huffman coding is based on the frequency ofthe symbols being encoded and not on the symbols themselves, so it allows redundanciesin the symbols to be compressed [3]. Code is generated with respect to the frequency ofrepetition of each symbol in the input, with smaller code values representing the mostcommon symbols. Huffman coding is also uniquely decodable, meaning that the codegenerated from a string cannot represent something other than the original input whenit is decoded.

Figure 5 is an example Huffman coding tree for characters in a string of typed words.The coding breaks down the symbols based on their frequency, and generates a uniquecode for each. Coding pixel values uses essentially the same process. However, the useHuffman coding effectively, the entire image must be reduced to a string of pixel values,not just one 8× 8 matrix.

Page 13: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 13 of 17

Go Back

Full Screen

Close

Quit

Figure 5: Example Huffman Coding Tree

Page 14: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 14 of 17

Go Back

Full Screen

Close

Quit

Complete Compression Process

So far only one 8× 8 example matrix has been compressed using the methods of JPEGcompression, but any given image will contain thousands of 8×8 block matrices makingone by one computation of the matrices as shown in the example impossible. A JPEGcompression algorithm takes an image and processes it by 8×8 block matrices as shownin the example. The DCT is applied to each block, the blocks are then quantized, andfinally reordered. Excess zeros are then removed, and the remaining entries are stored ina long string with an ‘end of block marker’ inserted between the coefficients to separatethe blocks. Huffman coding can then be implemented on the string, resulting in anotherreduction of the file size, and the image is stored.

When the image is accessed, it can be reconstructed through the reversal of theHuffman coding, reordering of the string into matrix blocks as designated by the endof block markers, and then put through the inverse quantization and inverse DCT. Areconstructed image that has been compressed using the JPEG compression procedurewill be an appoximation of the original due to the loss of information in the compres-sion procedure. However, the images (again, usually photographs) tend to retain asubstantial amount of quality.

The final result of the compression of the ‘Seal’ image is shown in Figure 6. Thecompression was done using JPEG’s standard compression rate, and it has retainedvery good visual quality compared with the original ‘Seal’ image shown in Figure 3.

The compression program was written using MATLAB, and the file sizes for theoriginal and compressed images were obtained from the MATLAB workspace. The filesize of the original ‘Seal’ image shown in Figure 3 is 1769 kilobytes, but the file size ofthe compressed image is a mere 158 kilobytes. This result includes implementing theHuffman coding procedure. This is a good compression result, considering the fact thatthe compressed image retains good quality and only has minor artifacting.

Page 15: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 15 of 17

Go Back

Full Screen

Close

Quit

Figure 6: Compressed Seal Image

Page 16: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 16 of 17

Go Back

Full Screen

Close

Quit

Conclusion

Overall, JPEG’s compression process is an extremely useful type of image compression.It is ideal for storing photographs since visually unimportant information can be easilydiscarded (of which photographs have quite a bit) and has become one of the most com-mon file types used. This paper has demonstrated the ability of the JPEG compressionprocess to effectively retain visual quality in an image while drastically reducing thefile size through the implementation of the Discrete Cosine Transform, quantization,reordering, and Huffman coding. As a final thought, I would like to thank my friendRyan Wortman for supplying the digital image.

References

[1] Austin, David. Image Compression: Seeing What’s Not There. American Mathe-matical Society. 2009. Accessed 24 Aug. 2009.

[2] “Discrete Cosine Transform.” Image Processing Toolbox. Mathworks. Accessed 4Sept. 2009.

[3] Gonzalez,Rafael C. and Richard E. Woods and Steven L. Eddis.Digital Image Pro-cessing Using MATLAB. Gatesmark Publishing. 2009. Pages 283-323.

[4] Harris, Greg A. and Darrel Hankerson. “Transform Methods and Image Compres-sion.” Web. January 1st, 1999. Accessed September 2, 2009.

[5] Holloway, Catherine. JPEG Image Compression: Transformation, Quantization,and Encoding. April 2008. Accessed 22 Sept. 2009.

[6] “Image Types.” Mathworks Helpdesk. Accessed 22 Sept. 2009.

Page 17: Introduction JPEG Image Compression › darnold › math45 › laproj › f09 › isnyder › paper.pdflace, on the JPEG’s compression process was subsequently published in 1991,

Introduction

Compression

Compressible Image Types

JPEG Technique

The Discrete Cosine . . .

Quantization

Reordering

Huffman Coding

Complete Compression . . .

Conclusion

Home Page

Title Page

JJ II

J I

Page 17 of 17

Go Back

Full Screen

Close

Quit

[7] O’Hanen, Ben and Matthew Wisan. “JPEG Compression”. Student Projects inLinear Algebra. Ed. David Arnold. 16 Dec. 2005. Accessed 2 Sept. 2009.

[8] Penfield, Paul. “Chapter 3: Compression.” Notes. 12 Feb. 2004. MIT. Accessed 6Sept. 2009.

[9] Poynton, Charles. Digital Video and HDTV: Algorithms and Interfaces

[10] “Types of Bitmaps.” MSDN. Microsoft Corporation. 2009. Accessed 2 Oct. 2009.

[11] Wallace, Gregory K. “The JPEG Still Picture Compression Standard”. Commu-nications of the ACM. 1 April 1991: 30-44. Accessed 4 Sept. 2009.