Functions > Image Processing > Transform Domain > Example: Discrete Cosine Transform
Example: Discrete Cosine Transform
Use the dct and idct functions to calculate the discrete cosine transform and the inverse transform, respectively. This transform has the advantage of concentrating most of the image "energy," or information, in a few frequency components, making a good choice for encoding.
For information on using this example, refer to About Image Processing Examples.
1. Define a matrix M of size N x P.
 N rows M n 0 N 1 u 0 N 1 P cols M p 0 P 1 v 0 P 1
2. The implementation of the cosine transform uses the type II DCT. Define DCT to be an analytic formula for this transform.
3. Evaluate matrix DCT.
4. Apply function dct to matrix M.
5. Calculate the maximum error between DCT and the dct function.
Energy Compaction
The cosine transform has very good energy-compaction properties, which means that it concentrates most of the energy in the first few coefficients of the transform.
1. Define an input matrix.
2. Apply the dct function to matrix M and evaluate it.
The resulting matrix shows that most of the energy, which is related to the coefficient values, is concentrated in the first few elements.
idct
The inverse function is used to recover an original image from its transform.
1. Read in a black-and-white version of the Mona Lisa.
(mona.bmp)
2. Apply the dct function to transform the image.
(mona_noise.bmp)
3. Apply the inverse function to recover the image.
(mona_rec.bmp)
4. Verify the energy-compaction property of the DCT by looking at histograms of the transformed and original images.
The transformed image concentrates most of the energy in the first few bins of the histogram, while the distribution of the original image is spread out. This makes the transformed image a better candidate for encoding or transmission, since most of the image information can be relayed with very few frequency components. For this reason the DCT is used for image compression.