I have computed the discrete cosine transform (DCT) of an image in MATLAB. I got a matrix of discrete cosine transform coefficients in the workspace. How can I recover the original image from its discrete cosine transform coefficients in MATLAB?
1 Answer
If you used dct2
on an image, or on each image channel separately for color images, with the appropriate input format, then idct2
recover the original image (the transformation is invertible and orthogonal), up to rounding errors (similar discussion in Why is $\exp(\ln(x))−x\neq 0$ in floating point arithmetic?).
You can find for instance a demo below:
dataRGB = double(imread('autumn.tif'));
data = rgb2gray(dataRGB/255);
dataDct= dct2(data);
dataDctIdct = idct2(dataDct);
subplot(2,2,1)
imagesc(data);
xlabel('Original (grayscale)');
subplot(2,2,2)
imagesc(log(abs(dataDct)));
xlabel('Original DCT');
subplot(2,2,3)
imagesc(dataDctIdct);
xlabel('Original recovered');
subplot(2,2,4)
imagesc(data-dataDctIdct);
xlabel('Difference');
colorbar
colormap gray