DCTN - N-D discrete cosine transform.
Y = DCTN(X) returns the discrete cosine transform (DCT) of X. The array Y is the same size as X and contains the discrete cosine transform coefficients. This transform can be inverted using IDCTN.
DCTN(X,DIM) applies the DCT operation across the dimension DIM.
Contents
Example #1: Remove high frequencies in an image using DCT
RGB = imread('autumn.tif');
I = rgb2gray(RGB);
DCT transform:
J = dctn(I);
imshow(log(abs(J)),[]), title('DCT coefficients (log scale)')
colormap(gca,jet(64)), colorbar
The commands below set values less than magnitude 10 in the DCT matrix to zero, then reconstruct the image using the inverse DCT.
J(abs(J)<10) = 0; spy(J), axis off, title('Non-zero coefficients'), snapnow disp(['number of zeros: ' int2str(100-round(nnz(J)/numel(J)*100)) '%']) K = idctn(J); imshow(I), title('Original image'), snapnow imshow(K,[0 255]), title('DCT-processed image')
number of zeros: 77%
Example #2: JPEG compression
I = imread('cameraman.tif'); imshow(I), title('Original image') I = int8(double(I)-128);
Compute the 2-D DCT of each 8-by-8 block of the image:
J = blockproc(I,[8 8],@(x) dctn(x.data));
imshow(J,[]), title('DCT coefficients')
colormap(gca,parula(64)), colorbar
The base quantization matrix is
Q50 = [16 11 10 16 24 40 51 61; 12 12 14 19 26 58 60 55; ... 14 13 16 24 40 57 69 56; 14 17 22 29 51 87 80 62; ... 18 22 37 56 68 109 103 77; 24 35 55 64 81 104 113 92; ... 49 64 78 87 103 121 120 101; 72 92 95 98 112 100 103 99]
Q50 =
16 11 10 16 24 40 51 61
12 12 14 19 26 58 60 55
14 13 16 24 40 57 69 56
14 17 22 29 51 87 80 62
18 22 37 56 68 109 103 77
24 35 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99
Quantization:
C = blockproc(J,[8 8],@(x) round(x.data./Q50)); imshow(C,[]), title('Compressed image') disp(['number of zeros: ' int2str(100-round(nnz(C)/numel(C)*100)) '%'])
number of zeros: 85%
Note that the coding and decoding processes are beyond the scope of this example.
Decompression:
J2 = blockproc(C,[8 8],@(x) round(x.data.*Q50));
I2 = uint8(blockproc(J2,[8 8],@(x) idctn(x.data))+128);
imshow(I2), title('Decompressed image')
Reference
Narasimha MJ and Peterson AM, On the computation of the discrete cosine transform, IEEE Trans. Comm. 1978;26:934-936.
See also
About the author
Damien Garcia, Eng., Ph.D. INSERM researcher Creatis, University of Lyon, France
website: BioméCardio