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.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
2. The implementation of the cosine transform uses the type II DCT. Define DCT to be an analytic formula for this transform.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
3. Evaluate matrix DCT.
Click to copy this expression
4. Apply function dct to matrix M.
Click to copy this expression
5. Calculate the maximum error between DCT and the dct function.
Click to copy this expression
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.
Click to copy this expression
2. Apply the dct function to matrix M and evaluate it.
Click to copy this expression
Click to copy this expression
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.
Click to copy this expression
(mona.bmp)
2. Apply the dct function to transform the image.
Click to copy this expression
Click to copy this expression
(mona_noise.bmp)
3. Apply the inverse function to recover the image.
Click to copy this expression
Click to copy this expression
(mona_rec.bmp)
4. Verify the energy-compaction property of the DCT by looking at histograms of the transformed and original images.
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
Click to copy this expression
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.
Was this helpful?