Sie sind auf Seite 1von 23

IMAGE COMPRESSION USING DISCRETE

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

Apart from the efforts of us, the success of this project


depends largely on the encouragement and guidelines provided to us by
our Digital Image Processing lecturer from time to time. We take this opportunity
to express our gratitude to the person who has been instrumental in
the successful completion of this project. We would like to show our
greatest appreciation to

Mr. Amit Kaul


(Faculty of Electrical and Electronics Engineering)

Under the guidance of whom

We feel motivated and encouraged


every time we attend his lectures. Without his encouragement and guidance
this project would not have materialized. We can’t say thank you enough for his
tremendous support and help. We are grateful for his constant support and help.

Thanking you for your kind anticipation in our project.

Dated: Harish Kumar (07414)


Aman Pundir (07429)
NIT Hamirpur, H.P.
Place:
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

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.

THE JPEG PROCESS:


The following is a general overview of the JPEG process. JPEG stands for Joint
Photographic Experts Group which is a commonly used method of compression for
photographic images. The degree of compression can be adjusted, allowing a selectable
tradeoff between storage size and image quality. JPEG typically achieves 10:1 compression
with little perceptible loss in image quality. More comprehensive understanding of the
process may be acquired as such given under:
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

1. The image is broken into 8x8 blocks of pixels.

2. Working from left to right, top to bottom, the DCT is applied to each block.

3. Each block is compressed through quantization.

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

THE DISCRETE COSINE TRANSFORM:

Like other transforms, the Discrete Cosine Transform (DCT) attempts to


decorrelate the image data. After decorrelation each transform coefficient can be encoded
independently without losing compression efficiency. This section describes the DCT and
some of its important properties.

1) The One-Dimensional DCT:

The most common DCT definition of a 1-D sequence of length N is

For u = 0, 1, 2, …, N −1.
Similarly, the inverse transformation is defined as

For x = 0, 1, 2, …, N −1. In both equations as above, α (u) is defined as


PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

It is clear from first equation that for

Thus, the first transform coefficient is the


average value of the sample sequence. In literature, this value is referred to as the DC
Coefficient. All other transform coefficients are called the AC Coefficients.

2) The Two-Dimensional DCT:

The Discrete Cosine Transform (DCT) is one of many transforms that


takes its input and transforms it into a linear combination of weighted basis functions.
These basis functions are commonly the frequency. The 2-D Discrete Cosine Transform is
just a one dimensional DCT applied twice, once in the x direction, and again in the y
direction. One can imagine the computational complexity of doing so for a large image.
Thus, many algorithms, such as the Fast Fourier Transform (FFT), have been created to
speed the computation.
The DCT equation (Eq.1) computes the i, jth entry of the DCT of an
image.

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 DCT MATRIX:


To get the matrix form of Equation (1), we will use the
following equation,

For an 8x8 block it results in this matrix:

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.

DCT ON AN 8x8 BLOCK:


Before we begin, it should be noted that the pixel values of a
black-and-white image range from 0 to 255 in steps of 1, where pure black is represented
by 0, and pure white by 255. Thus it can be seen how a photo, illustration, etc. can be
accurately represented by these 256 shades of gray. Since an image comprises hundreds or
even thousands of 8x8 blocks of pixels, the following description of what happens to one
8x8 block is a microcosm of the JPEG process; what is done to one block of image pixels is
done to all of them, in the order earlier specified. Now, let‘s start with a block of image-
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

)
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

If, however, another level of quality and compression is desired, scalar


multiples of the JPEG standard quantization matrix may be used. For a quality level greater
than 50 (less compression, higher image quality), the standard quantization matrix is
multiplied by (100-quality level)/50.

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.

Quantization is achieved by dividing each element in the transformed image matrix D by


PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

)
corresponding element in the quantization matrix, and then rounding to the nearest
integer value. For the following step, quantization matrix Q50 is used.

Recall that the coefficients situated near the upper-left corner


correspond to the lower frequencies œ to which the human eye is most sensitive œ of the
image block. In addition, the zeros represent the less important, higher frequencies that
have been discarded, giving rise to the lossy part of compression. As mentioned earlier,
only the remaining nonzero coefficients will be used to reconstruct the image. It is also
interesting to note the effect of different quantization matrices; use of Q10 would give C
significantly more zeros, while Q90 would result in very few zeros.

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:

Reconstruction of our image begins by decoding the bit stream representing


the Quantized matrix C. Each element of C is then multiplied by the corresponding element
of the quantization matrix originally used

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

N = round (T’RT) + 128

PROPERTIES OF DCT:
Some properties of the DCT which are of particular value to image processing
applications:

a) Decorrelation: The principle advantage of image transformation is the removal


of redundancy between neighboring pixels. This leads to uncorrelated transform
coefficients which can be encoded independently. It can be inferred that DCT
exhibits excellent decorrelation properties.

b) Energy Compaction: Efficacy of a transformation scheme can be directly gauged


by its ability to pack input data into as few coefficients as possible. This allows the
quantizer to discard coefficients with relatively small amplitudes without introducing
visual distortion in the reconstructed image. DCT exhibits excellent energy
compaction for highly correlated images.

c) Separability: The DCT transform equation can be expressed as

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:

>>o = imread ('e:\img10.jpg');


w = size (o, 2);
samplesHalf = floor(w / 2);
samplesQuarter = floor(w / 4);
samplesEighth = floor(w / 8);
ci2 = [];
ci4 = [];
ci8 = [];

for k=1:3 % all color layers: RGB


for i=1:size(o, 1) % all rows
rowDCT = dct(double(o(i,:,k)));
ci2(i,:,k) = idct(rowDCT(1:samplesHalf), w);
ci4(i,:,k) = idct(rowDCT(1:samplesQuarter), w);
ci8(i,:,k) = idct(rowDCT(1:samplesEighth), w);
end
end

h = size(o, 1);
samplesHalf = floor(h / 2);
samplesQuarter = floor(h / 4);
samplesEighth = floor(h / 8);
ci2f = [];
ci4f = [];
ci8f = [];

for k=1:3 % all color layers: RGB


for i=1:size(o, 2) % all columns
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

)
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

subplot(2,2,1), image(uint8(o)), title('Original Image');


subplot(2,2,2), image(uint8(ci2)), title('Compression Factor 2');
subplot(2,2,3), image(uint8(ci4)), title('Compression Factor 4');
subplot(2,2,4), image(uint8(ci8)), title('Compression Factor 8');
figure

subplot(2,2,1), image(uint8(o)), title('Original Image');


subplot(2,2,2), image(uint8(ci2f)), title('Compression Factor 2 * 2');
subplot(2,2,3), image(uint8(ci4f)), title('Compression Factor 4 * 4');
subplot(2,2,4), image(uint8(ci8f)), title('Compression Factor 8 * 8');

THE OUTPUT IMAGES:


1) Image of size 352 × 352:
PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

a) DCT to rows only:

Original Image Compression Factor 2

100 100

200 200

300 300

100 200 300 100 200 300

Compression Factor 4 Compression Factor 8

100 100

200 200

300 300

100 200 300 100 200 300


PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

b) DCT to rows*column:

Original Image Compression Factor 2 * 2

100 100

200 200

300 300

100 200 300 100 200 300

Compression Factor 4 * 4 Compression Factor 8 * 8

100 100

200 200

300 300

100 200 300 100 200 300


PROJECT: IMAGE COMPRESSION USING DISCRETE COSINE TRANFORM IMPLEMENTING MATLAB

)
2) Image of size 375 × 276 ×3(Low Resolution Image)

a) DCT to rows only:

Original Image Compression Factor 2

50 50
100 100
150 150
200 200
250 250
100 200 300 100 200 300

Compression Factor 4 Compression Factor 8

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:

Original Image Compression Factor 2 * 2

50 50
100 100
150 150
200 200
250 250
100 200 300 100 200 300

Compression Factor 4 * 4 Compression Factor 8 * 8

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

a) DCT to rows only:

Original Image Compression Factor 2

200 200
400 400
600 600
800 800
1000 1000
1200 1200
500 1000 1500 500 1000 1500

Compression Factor 4 Compression Factor 8

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:

Original Image Compression Factor 2 * 2

200 200
400 400
600 600
800 800
1000 1000
1200 1200
500 1000 1500 500 1000 1500

Compression Factor 4 * 4 Compression Factor 8 * 8

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

2) IEEE Journals and Research papers.

3) Wikipedia.org.

4) Mathworks Matlab Central.

5) fip-Contents.html

x---------------------x-----------------x------------------------x