Monday, 2 December 2013

How to apply DCT to Color Image & Grayscale Image in MATLAB?

AIM: Study and Implementation of Discrete Cosine Transform of given image.

THEORY:
Discrete Cosine Transform (DCT) has been an important achievement in the field of image compression. The DCT can be considered as a discrete time version of the Fourier Cosine series. The discrete cosine transforms (DCT) is a technique for converting a signal into elementary frequency components. The transformed array obtained through DCT is also of the size N x N, same as that of the original image block.
Such fast computational approaches and use of real arithmetic has made DCT popular for image compression applications. Since all natural images exhibits spatial redundancy, not all coefficients in the transformed array have significant values.

Applications:

1. Popular JPEG file format, and most video compression methods are generally based on it.
2. DCT make an image compression standard in still-image and multimedia coding standards.

1. DCT uses image independent transformations.
2. DCT has high energy compaction property.
3. Fast algorithms and architectures are available for DCT
4. DCT uses real computations, unlike the complex computations used in DFT. This makes DCT hardware simpler, as compared to that of DFT.

Limitations:
Despite excellent energy compaction capabilities, mean-square reconstruction error performance closely matching that of KLT and availability of fast computational approaches, DCT offers a few limitations which restrict its use in very low bit rate applications. The limitations are listed below:

1. Truncation of higher spectral coefficients results in blurring of the images, especially wherever the details are high.
2. Coarse quantization of some of the low spectral coefficients introduces graininess in the smooth portions of the images.
3. Serious blocking artifacts are introduced at the block boundaries, since each block is independently encoded, often with a different encoding strategy and the extent of quantization.

Following equation shows 2D DCT:
Following equation shows 2D IDCT:

Syntax for DCT and IDCT:

For DCT:

2-D discrete cosine transforms.
 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.
For IDCT:
IDCT2 2-D inverse discrete cosine transform.
 B = IDCT2(A)
Returns the two-dimensional inverse discrete cosine transform of A.
 B = IDCT2 (A,[M N]) or B = IDCT2(A,M,N)
Pads A with zeros (or truncates A) to create a matrix of size M-by-N before transforming.

PROGRAM:
Function of DCT for Color & Grayscale image:

 function out=mydct2(img) [r c p]=size(img); if(p==3)     out(:,:,1)=dct2(img(:,:,1));     out(:,:,2)=dct2(img(:,:,2));     out(:,:,3)=dct2(img(:,:,3)); else     out=dct2(img); end

Function of IDCT for Color & Grayscale image:

 function rec=myidct2(img) [r c p]=size(img); if(p==3)     rec(:,:,1)=idct2(img(:,:,1));     rec(:,:,2)=idct2(img(:,:,2));     rec(:,:,3)=idct2(img(:,:,3)); else     rec=idct2(img); end

Test Application:
 [file path]=uigetfile('*.*'); img=imread(file); out=mydct2(img); rec=myidct2(out); subplot(1,3,1);imshow(img);title('Original Image') subplot(1,3,2);imshow(uint8(out));title('DCT of I/P Image') subplot(1,3,3);imshow(uint8(rec));title('Rec image')

Outputs:

For Color Image:

For Grayscale Image: