Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> MATLAB

MATLAB 2-D Inverse Cosine Transform: Restore Images from Frequency Data

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 −

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 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

MATLAB

  1. MATLAB: Comprehensive Overview of a Leading Numerical Computing Environment
  2. MATLAB Loop Types: Mastering Repeated Code Execution
  3. Mastering MATLAB Graphics: Advanced Plotting Techniques
  4. MATLAB M-Files: Master Scripts and Functions for Powerful Programming
  5. MATLAB: Laplacian of Gaussian Filter for Edge Detection
  6. Plotting Functions in MATLAB: A Step-by-Step Guide
  7. Essential MATLAB Commands for Efficient Computing
  8. Visualize Mathematical Expressions in MATLAB: 2D & 3D Plotting Guide
  9. Understanding Polynomials in MATLAB: Representation and Evaluation
  10. Computing Symbolic Derivatives in MATLAB with the diff Command