• Aucun résultat trouvé

The Discrete Cosine

Dans le document File Formats (Page 92-98)

The Discrete Cosine Transform

Equation 7.7 The Discrete Cosine

Transform Matrix

Basic Matrix Operations

For those of you who are unfamiliar with matrices we provide a brief introduc-tion. For more information you should find an introductory textbook on linear algebra. A matrix is simply an array of numbers. Generally, when we refer to a matrix we mean a two-dimensional array. In giving the dimensions of a matrix, the number of rows is traditionally specified first. An N × M matrix is a matrix with N rows and M columns. Elements within a matrix are specified using sub-scripts. Thus, AMN refers to the element in row N and column M in matrix A.

A one-dimensional array of numbers is known as a vector, on which a fre-quently used operation is the dot product. The dot product operation takes two vectors with the same dimension as input and produces a number. To generate the dot product from two vectors you multiply the corresponding entries in the two vectors together and add all the products. This is an example of taking the dot product of two vectors A and B:

86 The Discrete Cosine Transform

Matrices can be multiplied only when they are compatible. Two matrices are compatible if the number of columns in the first matrix is equal to the number of rows in the second. All of the matrices we will be dealing with are square with the dimensions 8 × 8, so compatibility is not an issue. Matrix multiplication is performed by separating each matrix into a set of vectors. Each row in the first matrix forms a vector; each column in the second does also. To create the ele-ment in the Nth row and Mth column of the destination matrix we take the dot product of the vectors in the Nth row of first matrix and the Mth column of the second matrix. This is an example of multiplying the 2 × 2 matrices A and B:

Matrix multiplication is not commutative. If A and B are matrices, we can-not say that AB = BA.

Matrix multiplication is associative. If A, B, and C are matrices, then (AB)C = A(BC)

The matrix transpose operation is denoted by AT. If B = AT, then for every element in B, BNM = AMN as in the following example.

A matrix is multiplied by a number by multiplying each clement in the matrix by the number.

Using the 2-D Forward DCT 87 That is all the linear algebra we need to get through the rest of the chapter.

However, before we move on I would like to point out an interesting property of the DCT matrix M shown previously. M is what is known as an orthogonal matrix. If you take the dot product of any row or column with itself the result is 1. If you take the dot product of any row with any other row or any column and any other column the result is 0. If you multiply M by its transpose you get

MMT =

Any square matrix with values of 1 on the diagonal running from the upper left to the bottom right and values of 0 everywhere else is known as an identity matrix. Multiplying any matrix A by the identity matrix results in A.

Using the 2-D Forward DCT

Now we return to the image IRENE.JPG to show what happens to an image using the 2-dimensional DCT. Figure 7.6 contains an 8 8 block of Y component sam-ples from the area around Irene's eye and the 2-dimensional DCT coefficients generated from them.

In the figure the larger coefficient values are concentrated in the upper left corner. The trend is that the farther you get from the DC coefficient in the upper left corner, the smaller the values become. The DC coefficient is nearly three times as large as any of the AC coefficients.

Figure 7.6

88 The Discrete Cosine Transform

Quantization

We saw with the 1-dimensional DCT that you do not always need to use all the of the DCT coefficients with the Inverse DCT to reconstruct a close approxima-tion of the original data. We also saw that there are situaapproxima-tions when we need to use most, if not all, of the coefficients to reproduce something that is close. The same is true with the 2-dimensional DCT. After calculating the DCT, the next step is to find and discard the coefficients that contribute the least to the image.

The JPEG standard defines a simple mechanism for doing this known as quantization, which is a fancy name for division. To quantize the DCT coeffi-cients we simply divide them by another value and round to the nearest integer.

To reverse the process multiply

Coefficient = Quantized Value × Quantum value

Choosing a quantum value as small as 20 would convert over half the coef-ficients in Figure 7.6 to zeros.

JPEG uses a 64-element array called a quantization table to define quantum values for an image. Quantization tables are defined in the DQT marker described in Chapter 5. It is possible to use multiple quantization tables so that not all components need be quantized using the same values. Each value in a quantization table is used to quantize the corresponding DCT coefficient.

The JPEG standard does not specify the quantization values to be used. This is left up to the application. However, it does provide a pair of sample quantiza-tion tables that it says have been tested empirically and found to generate good results. These tables are shown in Figure 7.7.

Figure 7.7 Sample

Quantization Tables from the JPEG Standard

Cb and Cr Component Quantization Table

Zigzag Ordering

The result from quantizing sample values from Figure 7.6 with the sample Y quantization table in Figure 7.7 is shown in Figure 7.8. After quantization only 19 out of 64 DCT coefficients values are nonzero. Chapter 8 covers how JPEG compresses runs of zero AC coefficient values.

Zigzag Ordering

In order to group as many of the quantized zero-value coefficients together to produce the longest runs of zero values, AC coefficients in a data unit are encoded using a zigzag path.

Figure 7.9 shows the zigzag order defined by the JPEG standard. This was briefly mentioned in Chapter 5 in the section on the DQT marker. Quantization values within quantization tables are stored in JPEG files using this zigzag order-ing also. This is the orderorder-ing of the quantized AC coefficients in Figure 7.8:

18 -9 -3 -8 1 -3 1 -2 4 -2 4 0 3 -1 0 1 1 0 -1 -1 0 0 0 -1 (39 Zeros)

Figure 7.9

Zigzag Order for AC Coefficients

The Discrete Cosine Transform

Conclusion

In this chapter we described the Discrete Cosine Transform (DCT) and quantiza-tion. The DCT is used to represent an 8 x 8 block of sample values as a sum of cosine functions. The DCT coefficient in the upper left corner represents a con-stant value and is known as the DC coefficient. All other coefficients are AC coefficients.

We used matrix operations to calculate the DCT and its inverse. In Chapter 10 we will take another look at the DCT and show how it is possible to make it faster.

More information on the DCT can be found in Rao and Yip (1990), an entire book devoted to the subject. Nelson (1992) presents a simplified, JPEG-like ver-sion of DCT compresver-sion. The sample quantization tables in this chapter come from JPEG (1994). Any introductory book on linear algebra, such as Anton (1981), will contain more information on basic matrix operations.

The source code for this chapter consists of classes for representing quanti-zation tables and data units during compression. These classes use matrix opera-tions to implement the DCT.

90

Decoding

Dans le document File Formats (Page 92-98)