Image Compression Using DCT Implementing Matlab
Image Compression Using DCT Implementing Matlab
A PROJECT REPORT
SUBMITTED BY :
HARISH KUMAR (07414)
AMAN PUNDIR (07429)
NIT HAMIRPUR, H.P.
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
ACKNOWLEDGEMENT
CONTENTS:
1) Introduction
2) The JPEG Process
3) The Discrete Cosine Transform
a) 1D DCT
b) 2D DCT
4) DCT Matrix
5) Quantization
6) Coding
7) Decompression
8) Properties of DCT
9) Conclusion
10) Matlab code
11) Output Images
12) References
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
INTRODUCTION:
In today’s technological world as our use of and reliance on computers
continues to grow, so too does our need for efficient ways of storing large amounts of data
and due to the bandwidth and storage limitations, images must be compressed before
transmission and storage.For example, someone with a web page or online catalog that
uses dozens or perhaps hundreds of images will more than likely need to use some form of
image compression to store those images. This is because the amount of space required to
hold unadulterated images can be prohibitively large in terms of cost. Fortunately, there
are several methods of image compression available today. This fall into two general
categories: lossless and lossy image compression.
However, the compression will reduce the image fidelity, especially when the
images are compressed at lower bit rates. The reconstructed images suffer from blocking
artifacts and the image quality will be severely degraded under the circumstance of high
compression ratios. In order to have a good compression ratio without losing too much of
information when the image is decompressed we use DCT.
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. The JPEG process
is a widely used form of lossy image compression that centers on the Discrete Cosine
Transform. DCT and Fourier transforms convert images from time-domain to frequency-
domain to decorrelate pixels. The DCT transformation is reversible .The DCT works by
separating images into parts of differing frequencies. During a step called quantization,
where part of compression actually occurs, the less important frequencies are discarded,
hence the use of the term “lossy“. Then, only the most important frequencies that remain
are used retrieve the image in the decompression process. As a result, reconstructed
images contain some distortion; but as we shall soon see, these levels of distortion can be
adjusted during the compression stage. The JPEG method is used for both color and black-
and-white images.
2. Working from left to right, top to bottom, the DCT is applied to each block.
4. The array of compressed blocks that constitute the image is stored in a drastically
reduced amount of space.
5. When desired, the image is reconstructed through decompression, a process that uses
the Inverse Discrete Cosine Transform (IDCT).
For u = 0, 1, 2, …, N −1.
Similarly, the inverse transformation is defined as
p (x, y) is the x,yth element of the image represented by the matrix p. N is the size of the
block that the DCT is done on. The equation calculates one entry (i, j th) of the transformed
image from the pixel values of the original image matrix. For the standard 8x8 block that
JPEG compression uses, N equals 8 and x and y range from 0 to 7. Therefore D (i, j ) would
be as in Equation (3).
Because the DCT uses cosine functions, the resulting matrix depends on
the horizontal and vertical frequencies. Therefore an image black with a lot of change in
frequency has a very random looking resulting matrix, while an image matrix of just one
color, has a resulting matrix of a large value for the first element and zeroes for the other
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
)
elements.
The first row (i : 1) of the matrix has all the entries equal to
1/ 8 as expected from Equation (4).The columns of T form an orthonormal set, so T is an
orthogonal matrix. When doing the inverse DCT the orthogonality of T is important, as the
inverse of T is T’ which is easy to calculate.
)
pixel values. This particular block was chosen from the very upper- left-hand corner of an
image.
Because the DCT is designed to work on pixel values ranging from -128 to 127, the original
block is “leveled off“ by subtracting 128 from each entry. This results in the following
matrix.
We are now ready to perform the Discrete Cosine Transform, which is accomplished by
matrix multiplication.
D = TMT’ -----(5)
In Equation (5) matrix M is first multiplied on the left by the DCT matrix T from the previous
section; this transforms the rows. The columns are then transformed by multiplying on the
right by the transpose of the DCT matrix. This yields the following matrix.
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
This block matrix now consists of 64 DCT coefficients, c (i, j), where i and j range from 0 to
7. The top-left coefficient, c (0, 0), correlates to the low frequencies of the original image
block. As we move away from c(0,0) in all directions, the DCT coefficients correlate to
higher and higher frequencies of the image block, where c(7, 7) corresponds to highest
frequency. Higher frequencies are mainly represented as lower number and Lower
frequencies as higher number. It is important to know that human eye is most sensitive to
lower frequencies.
QUANTIZATION:
Our 8x8 block of DCT coefficients is now ready for compression by
quantization. A remarkable and highly useful feature of the JPEG process is that in this step,
varying levels of image compression and quality are obtainable through selection of specific
quantization matrices. This enables the user to decide on quality levels ranging from 1 to
100, where 1 gives the poorest image quality and highest compression, while 100 gives the
best quality and lowest compression. As a result, the quality/compression ratio can be
tailored to suit different needs.
Subjective experiments involving the human visual system have resulted in the JPEG
standard quantization matrix. With a quality level of 50, this matrix renders both high
compression and excellent decompressed image quality.
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
For a quality level less than 50 (more compression, lower image quality), the
standard quantization matrix is multiplied by 50/quality level. The scaled quantization
matrix is then rounded and clipped to have positive integer values ranging from 1 to 255.
For example, the following quantization matrices yield quality levels of 10 and 90.
)
corresponding element in the quantization matrix, and then rounding to the nearest
integer value. For the following step, quantization matrix Q50 is used.
CODING:
The quantized matrix C is now ready for the final step of compression. Before
storage, all coefficients of C are converted by an encoder to a stream of binary data
(01101011...). In-depth coverage of the coding process is beyond the scope of this article.
However, we can point out one key aspect that the reader is sure to appreciate. After
quantization, it is quite common for most of the coefficients to equal zero. JPEG takes
advantage of this by encoding quantized coefficients in the zig-zag sequence shown in
Figure as under. The advantage lies in the consolidation of relatively large runs of zeros,
which compress very well. The sequence in Figure 1(4x4) continues for the entire 8x8 block.
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
DECOMPRESSION:
R i, j = Q i, j × C i, j
The IDCT is next applied to matrix R, which is rounded to the nearest integer. Finally, 128 is
added to each element of that result, giving us the decompressed JPEG version N of our
original 8x8 image block M.
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
PROPERTIES OF DCT:
Some properties of the DCT which are of particular value to image processing
applications:
This property, known as separability, has the principle advantage that D (i, j) can be
computed in two steps by successive 1-D operations on rows and columns of an image. The
arguments presented can be identically applied for the inverse DCT computation.
d) Symmetry: Another look at the row and column operations in above Equation
reveals that these operations are functionally identical. Such a transformation is
called a symmetric transformation. A separable and symmetric transform can be
expressed in the form
D = TMT’
where M is an N ×N symmetric transformation matrix
This is an extremely useful property since it implies that the transformation matrix
can be precomputed offline and then applied to the image thereby providing orders
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
)
of magnitude improvement in computation efficiency.
COMPARISON OF MATRICES:
Let us now see how the JPEG version of our original pixel block compares,
CONCLUSION:
If we look at the above two matrices, this is a remarkable result, considering that
nearly 70% of the DCT coefficients were discarded prior to image block
decompression/reconstruction. Given that similar results will occur with the rest of the
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
)
blocks that constitute the entire image, it should be no surprise that the JPEG image will be
scarcely distinguishable from the original. Remember, there are 256 possible shades of gray
in a black-and-white picture, and a difference of, say, 10, is barely noticeable to the human
eye. DCT takes advantage of redundancies in the data by grouping pixels with similar
frequencies together. And moreover if we observe as the resolution of the image is very
high, even after sufficient compression and decompression there is very less change in the
original and decompressed image. Thus, we can also conclude that at the same
compression ratio the difference between original and decompressed image goes on
decreasing as there is increase in image resolution.
MATLAB CODE:
h = size(o, 1);
samplesHalf = floor(h / 2);
samplesQuarter = floor(h / 4);
samplesEighth = floor(h / 8);
ci2f = [];
ci4f = [];
ci8f = [];
)
columnDCT2=dct(double(ci2(:,i,k)));
columnDCT4=dct(double(ci4(:,i,k)));
columnDCT8=dct(double(ci8(:,i,k)));
ci2f(:,i,k) = idct(columnDCT2(1:samplesHalf), h);
ci4f(:,i,k) = idct(columnDCT4(1:samplesQuarter), h);
ci8f(:,i,k) = idct(columnDCT8(1:samplesEighth), h);
end
end
100 100
200 200
300 300
100 100
200 200
300 300
b) DCT to rows*column:
100 100
200 200
300 300
100 100
200 200
300 300
)
2) Image of size 375 × 276 ×3(Low Resolution Image)
50 50
100 100
150 150
200 200
250 250
100 200 300 100 200 300
50 50
100 100
150 150
200 200
250 250
100 200 300 100 200 300
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
b) DCT to rows*column:
50 50
100 100
150 150
200 200
250 250
100 200 300 100 200 300
50 50
100 100
150 150
200 200
250 250
100 200 300 100 200 300
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
)
3) Image of size 1920×1200×3(High Resolution Image):
200 200
400 400
600 600
800 800
1000 1000
1200 1200
500 1000 1500 500 1000 1500
200 200
400 400
600 600
800 800
1000 1000
1200 1200
500 1000 1500 500 1000 1500
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
b) DCT to rows*column:
200 200
400 400
600 600
800 800
1000 1000
1200 1200
500 1000 1500 500 1000 1500
200 200
400 400
600 600
800 800
1000 1000
1200 1200
500 1000 1500 500 1000 1500
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB
REFERENCES:
1) Digital Image Processing by “Gonzalez and Woods”.
3) Wikipedia.org.
5) fip-Contents.html
x---------------------x-----------------x------------------------x