主要内容

Discrete Cosine Transform

DCT Definition

The discrete cosine transform (DCT) represents an image as a sum of sinusoids of varying magnitudes and frequencies. Thedct2function computes the two-dimensional discrete cosine transform (DCT) of an image. The DCT has the property that, for a typical image, most of the visually significant information about the image is concentrated in just a few coefficients of the DCT. For this reason, the DCT is often used in image compression applications. For example, the DCT is at the heart of the international standard lossy image compression algorithm known as JPEG. (The name comes from the working group that developed the standard: the Joint Photographic Experts Group.)

The two-dimensional DCT of an M-by-N matrixAis defined as follows.

B p q = α p α q m = 0 M 1 n = 0 N 1 A m n cos π ( 2 m + 1 ) p 2 M cos π ( 2 n + 1 ) q 2 N , 0 p M 1 0 q N 1 α p = { 1 / M , 2 / M , p = 0 1 p M 1 α q = { 1 / N , 2 / N , q = 0 1 q N 1

The valuesBpq称为DCT coefficientsA. (Note that matrix indices in MATLAB®always start at 1 rather than 0; therefore, the MATLAB matrix elementsA(1,1)B(1,1)对应于数学数量A00B00, respectively.)

The DCT is an invertible transform, and its inverse is given by

A m n = p = 0 M 1 q = 0 N 1 α p α q B p q cos π ( 2 m + 1 ) p 2 M cos π ( 2 n + 1 ) q 2 N , 0 m M 1 0 n N 1 α p = { 1 / M , 2 / M , p = 0 1 p M 1 α q = { 1 / N , 2 / N , q = 0 1 q N 1

The inverse DCT equation can be interpreted as meaning that any M-by-N matrixA可以写成的总和Mnfunctions of the form

α p α q cos π ( 2 m + 1 ) p 2 M cos π ( 2 n + 1 ) q 2 N , 0 p M 1 0 q N 1

These functions are called the基础功能的the DCT. The DCT coefficientsBpq,那么,可以被视为权重应用于每个基础函数。对于8 x 8矩阵,该图像说明了64个基本函数。

The 64 Basis Functions of an 8-by-8 Matrix

Horizontal frequencies increase from left to right, and vertical frequencies increase from top to bottom. The constant-valued basis function at the upper left is often called the直流基函数, and the corresponding DCT coefficientB00is often called the直流系数.

The DCT Transform Matrix

有两种方法可以计算使用图像DCTProcessing Toolbox™ software. The first method is to use thedct2function.dct2使用基于FFT的算法进行大量输入的快速计算。第二种方法是使用DCTtransform matrix, which is returned by the functiondctmtx和might be more efficient for small square inputs, such as 8-by-8 or 16-by-16. The M-by-M transform matrixTis given by

T p q = { 1 M 2 M cos π ( 2 q + 1 ) p 2 M p = 0 , 1 p M 1 , 0 q M 1 0 q M 1

For an M-by-M matrixA,T*A是一个M-BY-M矩阵,其列包含列的一维DCTA. The two-dimensional DCT ofAcan be computed asb = t*a*t'. SinceTis a real orthonormal matrix, its inverse is the same as its transpose. Therefore, the inverse two-dimensional DCT ofBis given byT'*B*T.

Image Compression with the Discrete Cosine Transform

此示例显示了如何使用离散余弦变换(DCT)压缩图像。该示例在输入图像中计算8乘8块的二维DCT,在每个块中丢弃(设置为零),除10个dct系数中的10个块,然后使用二维逆DCT重建图像每个块。该示例使用转换矩阵计算方法。

DCT用于JPEG图像压缩算法中。输入图像分为8乘8或16 x-16块,并为每个块计算二维DCT。然后对DCT系数进行量化,编码和传输。JPEG接收器(或JPEG文件读取器)解码量化的DCT系数,计算每个块的逆二维DCT,然后将块放回单个图像中。对于典型图像,许多DCT系数的值接近零。这些系数可以丢弃,而不会严重影响重建图像的质量。

在工作区中阅读图像并将其转换为类double.

I = imread('cameraman.tif');i = im2double(i);

Compute the two-dimensional DCT of 8-by-8 blocks in the image. The functiondctmtxreturns the N-by-N DCT transform matrix.

T = dctmtx(8); dct = @(block_struct) T * block_struct.data * T'; B = blockproc(I,[8 8],dct);

Discard all but 10 of the 64 DCT coefficients in each block.

mask = [1 1 1 1 0 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]; B2 = blockproc(B,[8 8],@(block_struct) mask .* block_struct.data);

Reconstruct the image using the two-dimensional inverse DCT of each block.

invdct = @(block_struct) T' * block_struct.data * T; I2 = blockproc(B2,[8 8],invdct);

显示原始图像和并排的重建图像。尽管重建图像的质量有所损失,但显然可以识别,即使丢弃了近85%的DCT系数。

imshow(I)

Figure contains an axes object. The axes object contains an object of type image.

figure imshow(I2)

Figure contains an axes object. The axes object contains an object of type image.