Introduction

A discrete cosine transform (DCT) expresses a sequence of finitely many data points in terms of a sum of cosine functions oscillating at different frequencies. DCTs are important to numerous applications in science and engineering, from lossy compression of audio and images (where small high-frequency components can be discarded), to spectral methods for the numerical solution of partial differential equations. The use of cosine rather than sine functions is critical in these applications: for compression, it turns out that cosine functions are much more efficient (as explained below, fewer are needed to approximate a typical signal), whereas for differential equations the cosines express a particular choice of boundary conditions.

In particular, a DCT is a Fourier-related transform similar to the discrete Fourier transform (DFT), but using only real numbers. DCTs are equivalent to DFTs of roughly twice the length, operating on real data with even symmetry (since the Fourier transform of a real and even function is real and even), where in some variants the input and/or output data are shifted by half a sample. There are eight standard DCT variants, of which four are common.

Background

Here I am showing an implementation of 2D DCT. We generate the 2D DCT kernel first and then using matrix operations we perform DCT. I am very lazy to write the theory. Please refer to the proper books for the same.

Using the Code

Here we perform the following activities:

Select the image, scale the image so that it fits the picture box, this scaling is shown in Scaling percentage box.

Select a specific part of the image, we select image part of dimensions multiple of 2 (128*128, 256*256).

Read the image in 2D Array, we are using pointer arithmetic for this, we have to allow pointer operations in project properties.