Lab (6) - Image Processing

Discrete Cosine Transform (DCT)

Inverse Discrete Cosine Transform (IDCT)

Discrete Cosine Transform (DCT) is a common tool used in image processing.  In this transform, most of the visually significant information on the image will be concentrated in just few coefficients of the DCT.   For this reason, in recent years, the DCT has been used for image compression applications.   The international standard compression technique, JPEG, is a lossy compression that is designed based on the DCT.  The 1-D DCT is defined as:

(1)

for u= 0, 1, 2, ... , N - 1.  The inverse DCT (IDCT) is defined as:

(2)

for x = 0, 1, 2, ... , N - 1.  In both equation (1) and (2):

(3)

The corresponding 2-D DCT pair is:

(4)

for u, v = 0, 1, 2, ... , N - 1, and the inverse 2-d DCT is defined as:

(5)

Where x, y = 0, 1, 2, …, N-1.  Now let's see how DCT works.  The example that we want to try is a compression example.  In the JPEG compression process, the input image is divided into 8-by-8 or 16-by-16 blocks, and the 2-D DCT is computed for each block.  The DCT coefficients then will be quantized, coded, and transmitted.  The receiver will use a decoder to extract the quantized DCT coefficients.  The last step on the receiver side is to use the IDCT to get the original image back.

The Image Processing Toolbox in MATALB offers two different ways to compute the DCT.  The first method is to use the function dct2.  The dct2 command uses an algorithm similar to that of FFT for speedy computation with large inputs.  The second method is to use dctmtx.  The dctmtx is appropriate for small size square inputs such as 8x8  or 16x16.  In MATLAB the transform matrix is given as:

(6)

For a matrix, A, of NxN size, T*A is an NxN matrix whose columns contains the one dimensional DCT of the column of A.  The 2-D DCT for A can be computed as B = T*A*T'.

Note that A is real orthogonal matrix, thus, its inverse is the same as its transpose.  The inverse 2-D DCT of B is given as:  T'*B*T.

Example:

Suppose, we want to find the DCT of the lena.bmp image.

>> I = double(I)/255;

>> T = dctmtx(8);

>> B=blkproc(I,[8 8], 'P1*x*P2', T, T');

>> mask = [1 1 1 1 0 0 0 0

1 1 1 0 0 0 0 0

1 1 0 0 0 0 0 0

1 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0

0 0 0 0 0 0 0 0];

>> I2=blkproc(B2,[8 8], 'P1*x*P2', T', T);

>> imshow(I), figure, imshow(I2);

 Original Image DCT with about 15% of coefficients (10/64)

The image on the right was obtained with about 85% of DCT coefficients.  There is some loss in the image quality, but still that image is recognizable although it is represented with fewer coefficients than in the original image.

Let's create a MATLAB file (.m) file and try different number of coefficients and see how that changes the quality of our image.  Use the vile, vi, or pico editor on UNIX or any word processor software on your PC create a file called (mydct.m) and copy all the parts that are in font green above the images into that file.  Save the file and exit.  Now you type mydct at the MATLAB prompt to run the code.  You will see that the same two figures will be generated again.

Here is what you need to do.  Use the mask matrix to remove more coefficients.

Case 1) use only 6 coefficients, remove one 1, from the first 4 rows.

Case 2) use 3 coefficients, remove another 1 from the first three rows.

Do you see a big difference?