Skip to content

MATLAB - 2-D Inverse Cosine Transform

MATLAB - 2-D Inverse Discrete Cosine Transform (IDCT)

Section titled “MATLAB - 2-D Inverse Discrete Cosine Transform (IDCT)”

The 2-D Inverse Discrete Cosine Transform (IDCT) is a fundamental operation in digital signal and image processing. It reverses the process of the 2-D Discrete Cosine Transform (DCT), converting an image from its frequency-domain representation back into the spatial domain (i.e., pixels).

Its most famous application is in the JPEG image compression standard. After an image’s DCT coefficients are calculated and quantized (a step that discards less important frequency information to save space), the IDCT is used by the decoder to reconstruct the image. The function for this in MATLAB is idct2.

In MATLAB, the idct2 function computes the two-dimensional inverse discrete cosine transform of a matrix. It takes a matrix of DCT coefficients as input and returns the reconstructed matrix in the spatial domain.

B = idct2(A)
B = idct2(A, m, n)
B = idct2(A, [m n])
  • B = idct2(A): Computes the IDCT of the matrix A. The output matrix B will have the same dimensions as A.
  • B = idct2(A, m, n) or B = idct2(A, [m n]): Computes the IDCT, but sizes the output matrix B to be m-by-n. If A is larger than m-by-n, it is cropped. If A is smaller, it is padded with zeros before the transformation. This is useful in reconstruction scenarios where the original dimensions are known.

Let’s explore some practical examples of using idct2.

Practical Application: Image Filtering via Frequency Domain

Section titled “Practical Application: Image Filtering via Frequency Domain”

Example 1: Low-Pass Filtering to Blur an Image

Section titled “Example 1: Low-Pass Filtering to Blur an Image”

A common use of the DCT/IDCT is to perform filtering in the frequency domain. By manipulating the DCT coefficients, we can alter the image’s characteristics. In this example, we will create a simple low-pass filter by zeroing out high-frequency coefficients, which effectively blurs the image.

%% Modern MATLAB Best Practices Example: Image Filtering
% --- 1. Setup and Image Loading ---
% Using a built-in MATLAB image for reproducibility.
originalImage = imread('cameraman.tif');
% Best Practice: Ensure image is grayscale for 2D DCT.
% The 'cameraman.tif' is already grayscale, but this check is robust.
if size(originalImage, 3) > 1
grayImage = rgb2gray(originalImage);
else
grayImage = originalImage;
end
% --- 2. Forward DCT ---
% Best Practice: Convert image to a floating-point type (e.g., double)
% for numerical accuracy before performing Fourier-family transforms.
doubleImage = double(grayImage);
dctCoeffs = dct2(doubleImage);
% --- 3. Frequency Domain Filtering ---
% The top-left corner of the DCT matrix holds the low-frequency components.
% We define a cutoff to keep only the lowest 30x30 frequencies.
cutoff = 30;
dctCoeffsFiltered = dctCoeffs;
% Zero out high-frequency components.
dctCoeffsFiltered(cutoff+1:end, :) = 0;
dctCoeffsFiltered(:, cutoff+1:end) = 0;
% --- 4. Inverse DCT ---
% Reconstruct the image using the inverse DCT.
reconstructedImage = idct2(dctCoeffsFiltered);
% --- 5. Display Results ---
% Best Practice: Convert back to uint8 for proper image display.
figure;
subplot(1, 2, 1);
imshow(grayImage);
title('Original Image');
subplot(1, 2, 2);
imshow(uint8(reconstructedImage));
title('Filtered Image (Low-Pass)');
  • Load & Prepare: We load the image and ensure it’s grayscale. We then cast it to double to prevent data loss or clipping during the DCT calculation, which involves floating-point math.
  • Transform: dct2 converts the image into its frequency representation. The coefficients in dctCoeffs represent the magnitude of different frequency patterns in the image.
  • Filter: We set all high-frequency coefficients to zero. High frequencies correspond to sharp transitions, like edges and noise. Low frequencies represent smooth color gradients. By removing high frequencies, we are essentially removing fine details.
  • Inverse Transform: idct2 takes the modified coefficients and transforms them back into a spatial image.
  • Display: The final image is converted back to uint8, the standard 8-bit integer format for grayscale images, before being displayed. The result is a blurred version of the original, as the sharp details have been filtered out.

A figure window will appear with two images. On the left, you will see the original, sharp ‘cameraman’ image. On the right, the ‘Filtered Image’ will appear noticeably smoother and less detailed, demonstrating the effect of the low-pass filter.

  • Forgetting Data Type Conversion: Performing dct2 on a uint8 image can lead to inaccurate results because the transform math can exceed the 0-255 range. Always convert to double or single first.
  • Incorrect Display Range: If you call imshow on the double result of idct2, it will likely appear as a white rectangle. This is because imshow expects double values to be in the range [0, 1]. The reconstructed image will have values in the [0, 255] range. The correct way is to convert back to uint8 with uint8(reconstructedImage) before displaying.
  • Incorrect Cropping/Padding: When using idct2(A, m, n), be aware that padding with zeros can introduce artifacts at the edges of the reconstructed image, while cropping discards information.