Main Content

dct2

2-D discrete cosine transform

Description

B = dct2(A) returns the two-dimensional discrete cosine transform of A. The matrix B contains the discrete cosine transform coefficients B(k1,k2).

example

B = dct2(A,m,n) and

B = dct2(A,[m n]) zero-pads or crops the matrix A to size m-by-n before applying the transformation.

Examples

collapse all

Read an image into the workspace, then convert the image to grayscale.

RGB = imread('autumn.tif');
I = im2gray(RGB);

Perform a 2-D DCT of the grayscale image using the dct2 function.

J = dct2(I);

Display the transformed image using a logarithmic scale. Notice that most of the energy is in the upper left corner.

imshow(log(abs(J)),[])
colormap parula
colorbar

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

Set values less than magnitude 10 in the DCT matrix to zero.

J(abs(J) < 10) = 0;

Reconstruct the image using the inverse DCT function idct2. Rescale the values to the range [0, 1] expected of images of data type double.

K = idct2(J);
K = rescale(K);

Display the original grayscale image alongside the processed image. The processed image has fewer high frequency details, such as in the texture of the trees.

montage({I,K})
title('Original Grayscale Image (Left) and Processed Image (Right)');

Figure contains an axes object. The hidden axes object with title Original Grayscale Image (Left) and Processed Image (Right) contains an object of type image.

Input Arguments

collapse all

Input matrix, specified as a 2-D numeric matrix.

Number of image rows, specified as a positive integer. dct2 pads image A with 0s or truncates image A so that it has m rows. By default, m is equal to size(A,1).

Number of image columns, specified as a positive integer. dct2 pads image A with 0s or truncates image A so that it has n columns. By default, n is equal to size(A,2)

Output Arguments

collapse all

Transformed matrix using a two-dimensional discrete cosine transform, returned as an m-by-n numeric matrix.

Data Types: double

More About

collapse all

Discrete Cosine Transform

The discrete cosine transform (DCT) is closely related to the discrete Fourier transform. It is a separable linear transformation; that is, the two-dimensional transform is equivalent to a one-dimensional DCT performed along a single dimension followed by a one-dimensional DCT in the other dimension. The definition of the two-dimensional DCT for an input image A and output image B is

Bpq=αpαqm=0M1n=0N1Amncosπ(2m+1)p2Mcosπ(2n+1)q2N, 0pM10qN1

where

αp={1M, p=0           2M, 1pM-1

and

αq={1N, q=0          2N, 1qN-1

M and N are the row and column size of A, respectively.

Tips

  • If you apply the DCT to real data, the result is also real. The DCT tends to concentrate information, making it useful for image compression applications.

  • To invert the DCT transformation, use idct2.

References

[1] Jain, Anil K., Fundamentals of Digital Image Processing, Englewood Cliffs, NJ, Prentice Hall, 1989, pp. 150–153.

[2] Pennebaker, William B., and Joan L. Mitchell, JPEG: Still Image Data Compression Standard, Van Nostrand Reinhold, 1993.

Extended Capabilities

Version History

Introduced before R2006a

expand all

See Also

| |