This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


2-D discrete cosine transform


B = dct2(A)
B = dct2(A,m,n)
B = dct2(A,[m n])


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

B = dct2(A,m,n) pads the matrix A with 0's to size m-by-n before transforming. If m or n is smaller than the corresponding dimension of A, dct2 truncates A.

B = dct2(A,[m n]) same as above.

Class Support

A can be numeric or logical. The returned matrix B is of class double.


collapse all

This example shows how to remove high frequencies from an image using the two-dimensional discrete cosine transfer (DCT).

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

RGB = imread('autumn.tif');
I = rgb2gray(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.


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.

K = idct2(J);

Display the original grayscale image alongside the processed image.

title('Original Grayscale Image (Left) and Processed Image (Right)');


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


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


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

M and N are the row and column size of A, respectively. 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.

This transform can be inverted using idct2.


[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.

See Also

| |

Introduced before R2006a

Was this topic helpful?