Main Content

Compress Image Using 2-D DCT

This example shows how to compress an image using a 2-D discrete cosine transform (DCT). The example computes the 2-D DCT of 8-by-8 nonoverlapping blocks of the input image, discards (sets to zero) all but 10 of the 64 DCT coefficients in each block, and then reconstructs the image using the 2-D inverse discrete cosine transform (IDCT) of each block.

Example Model

Open the Simulink® model.

modelname ='ex_blk2ddct.slx'; open_system(modelname)

The model reads an input image using theImage From Fileblock, and converts the data type of the image todoubleusing theImage Data Type Conversionblock.

Process Image Block-by-Block

TheBlock Processingblock performs the operations defined in this subsystem on each block of the input image.

The subsystem first computes the 2-D DCT of a block of the input image. TheProduct, Matrix Multiply(Simulink)block then multiplies the DCT coefficients by the elements of the mask, defined using theConstant(Simulink)block, to discard all but 10 of the 64 DCT coefficients. The2-D IDCTblock then reconstructs the processed image.

Simulate and Display Results

Simulate the model. TheVideo Viewerblocks display the input and output images. Notice that, despite some loss of quality, the output image is clearly recognizable even after discarding nearly 85% of the DCT coefficients of the input image.

sim(modelname);