MATLAB - 2-D Inverse Cosine Transform



The Inverse Cosine Transform, often denoted as ICT or IDCT, is a mathematical operation that reverses the process of the Cosine Transform. It is particularly useful in signal and image processing for reconstructing signals or images from their frequency domain representations.

In the context of 2-D signals or images, the 2-D Inverse Cosine Transform (2-D ICT or 2-D IDCT) converts a matrix of cosine coefficients (representing the frequency content of the signal or image) back to the spatial domain, yielding the original signal or image.

The 2-D Inverse Cosine Transform in MATLAB is used to convert a matrix of cosine values into a spatial domain image. It is the inverse operation of the 2-D Cosine Transform and is commonly used in image processing and compression. The idct2 function is used to perform the 2-D Inverse Cosine Transform in MATLAB.

2-D inverse Discrete Cosine Transform

In MATLAB, the idct2 function is used to perform the 2-D Inverse Cosine Transform. It takes a matrix of cosine coefficients as input and returns the spatial domain representation of the signal or image. The result is a reconstructed image that can be displayed or further processed.

The Inverse Cosine Transform is crucial in various applications, including image compression (e.g., in JPEG compression), image reconstruction, and signal processing tasks where signals or images need to be transformed back to their original form after frequency domain manipulation.

Syntax

B = idct2(A)
B = idct2(A,m,n)
B = idct2(A,[m n])

Syntax Explanation

B = idct2(A) − Calculates the two-dimensional Inverse Discrete Cosine Transform (IDCT) of matrix A, returning the result in matrix B. This operation effectively reconstructs the spatial domain image from its frequency domain representation in A.

B = idct2(A, m,n) − calculates the two-dimensional Inverse Discrete Cosine Transform (IDCT) of matrix A and specifies the size of the output matrix B as m-by-n. This operation effectively reconstructs the spatial domain image from its frequency domain representation in A, resizing it to the specified dimensions m-by-n.

B = idct2(A, [m,n]) − calculates the two-dimensional Inverse Discrete Cosine Transform (IDCT) of matrix A and resizes the output matrix B to have m rows and n columns. This operation reconstructs the spatial domain image from its frequency domain representation in A, resizing it to the specified dimensions [m n].

Let us see few examples for 2-D inverse discrete cosine transform

Example 1: To Remove High Frequencies From Image Using idct2() Function

The code we have is −

img = imread('autumn.tif');

% Convert to grayscale if necessary
if size(img, 3) == 3
    img = rgb2gray(img);
end

% Compute 2-D DCT
dct_img = dct2(double(img));

% Set a threshold to remove high frequencies (e.g., keep only the first 50 coefficients)
threshold = 50;
dct_img_thresh = dct_img;
dct_img_thresh(threshold+1:end, :) = 0;
dct_img_thresh(:, threshold+1:end) = 0;

% Compute the inverse 2-D DCT to get the filtered image
filtered_img = uint8(idct2(dct_img_thresh));

% Display the original and filtered images
figure;
subplot(1, 2, 1);
imshow(img);
title('Original Image');

subplot(1, 2, 2);
imshow(filtered_img);
title('Filtered Image');

In the example −

  • The imread function is used to read the image from a file ('autumn.tif' in this case) and store it in the variable img.
  • If the image is a color image (i.e., it has three channels for red, green, and blue), it is converted to grayscale using the rgb2gray function. This is done because the 2-D DCT is typically applied to grayscale images.
  • The dct2 function computes the 2-D DCT of the image. The result is stored in the variable dct_img. The DCT represents the image in the frequency domain, where high-frequency components correspond to rapid changes in pixel values.
  • A threshold value is chosen to determine which DCT coefficients to keep and which to discard. In this example, we choose to keep only the first 50 coefficients (in a 256x256 image, these coefficients represent the lowest frequencies). The high-frequency coefficients (those beyond the threshold) are set to zero.
  • The idct2 function computes the inverse 2-D DCT of the modified DCT coefficients (dct_img_thresh). This operation effectively reconstructs the spatial domain image from its frequency domain representation. The result is stored in filtered_img.
  • Finally, the original image and the filtered image are displayed side by side using the imshow function. The original image is displayed on the left, and the filtered image (with high frequencies removed) is displayed on the right.

On execution the output we get is −

Example 2: Resizing Image Using B = idct2(A, m,n)

The code we have is −

% Read the image
img = imread('autumn.tif');

% Convert the image to grayscale
if size(img, 3) == 3
    img = rgb2gray(img);
end

% Compute the 2-D DCT of the image
dct_img = dct2(double(img));

% Resize the DCT coefficients matrix (frequency domain representation) to a smaller size
% Let's resize it to half the original size
new_size = size(img) / 2;
dct_resized = imresize(dct_img, new_size);

% Compute the inverse 2-D DCT to get the resized image
resized_img = uint8(idct2(dct_resized, size(img, 1), size(img, 2)));

% Display the original and resized images
subplot(1, 2, 1);
imshow(img);
title('Original Image');

subplot(1, 2, 2);
imshow(resized_img);
title('Resized Image using 2-D IDCT');

In the example we have −

  • The imread function is used to read the image from a file ('autumn.tif' in this case) and store it in the variable img.
  • If the image is a color image (i.e., it has three channels for red, green, and blue), it is converted to grayscale using the rgb2gray function. This step is important because the 2-D DCT is typically applied to grayscale images.
  • The dct2 function computes the 2-D DCT of the image. The result is stored in the variable dct_img. The DCT represents the image in the frequency domain, where different frequencies of the image are represented by different coefficients.
  • To resize the image, we resize the DCT coefficients matrix (dct_img) to a smaller size. This is done using the imresize function, specifying the new size as half the original size (new_size = size(img) / 2). This effectively reduces the frequency domain representation of the image.
  • The idct2 function computes the inverse 2-D DCT of the resized DCT coefficients (dct_resized). This operation effectively reconstructs the resized spatial domain image from its resized frequency domain representation.
  • Finally, the original image and the resized image are displayed side by side using the imshow function. The original image is displayed on the left, and the resized image (obtained using 2-D IDCT) is displayed on the right.

The output on execution is −

Example 3: Resizing a Matrix Using 2-D Inverse Discrete Cosine Transform (IDCT)

The code we have is as follows −

% Create a sample matrix A
A = [
    10, 20, 30, 40;
    50, 60, 70, 80;
    90, 100, 110, 120;
    130, 140, 150, 160
];

% Display the original matrix A
disp('Original Matrix A:');
disp(A);

% Compute the 2-D IDCT of A and resize it to a 3x2 matrix
B = idct2(A, [3, 2]);

% Display the resized matrix B
disp('Resized Matrix B (3x2):');
disp(B);

In this example, we create a 4x4 sample matrix A. We then apply the 2-D IDCT to A and resize the result to a 3x2 matrix [m, n] = [3, 2]. The resized matrix B is displayed after the transformation.

The output we get is as follows −

Original Matrix A:
    10    20    30    40
    50    60    70    80
    90   100   110   120
   130   140   150   160

Resized Matrix B (3x2):
  122.0957  -11.9692
  -97.4491    1.6910
   12.0957   -1.9692
Advertisements