You are on page 1of 7

JPEG IMAGE

COMPRESSION
Abstract
In recent years, the development and demand of multimedia product grows
increasingly fast, contributing to insufficient bandwidth of network and storage of
memory device. Therefore, the theory of data compression becomes more and more
significant for reducing the data redundancy to save more hardware space and
transmission bandwidth. This is where jpeg image compression comes in the
picture, JPEG typically achieves 10:1 compression with little perceptible loss in image quality . In this
project we attempted to implement basic JPEG compression using only basic Matlab
functions.

INTRODUCTION
Image compression is an application of data compression that encodes the original
image with few bits. The objective of image compression is to reduce the
redundancy of the image and to store or transmit data in an efficient form.
JPEG stands for the Joint Photographic Experts Group, a standards committee that
had its origins within the International Standard Organization (ISO).JPEG provides a
compression method that is capable of compressing continuous-tone image data
with a pixel depth of 6 to 24 bits with reasonable speed and efficiency.JPEG may be
adjusted to produce very small, compressed images that are of relatively poor
quality in appearance but still suitable for many applications. Conversely, JPEG is
capable of producing very high-quality compressed images that are still far smaller
than the original uncompressed data.
JPEG Process:
Original image is divided into blocks of 8 x 8.
Pixel values of a black and white image range from 0-255 but DCT is designed to
work on pixel values ranging from -128 to 127.
Therefore each block is modified to work in the range.
This equation is used to calculate DCT matrix.

DCT is applied to each block by multiplying the modified block with DCT matrix on
the left and transpose of DCT matrix on its right.
Each block is then compressed through quantization.
Quantized matrix is then entropy encoded.
Compressed image is reconstructed through reverse process. Inverse DCT is used
for decompression.

PROCESS

Step1: Converting the base image to 8x8 matrices, DCT transform, quantizing The
2-D discrete cosine transform is done simply with the dct2() command. After
splitting the matrix into 8x8 matrices and performing the DCT, a simple piecewise
division by the quantization matrix obtains the quantized matrices needed for the
next step.
Coding of the DCT coefficients done in two parts
The DC coefficient (u, v) = (0, 0) is coded using difference encoding.

The AC coefficients (the rest of the coefficients) are run length encoded and then
Huffman coded.

function b = jpeg(file);

%
%
%

Usage : Input an image file and recieve the jpeg implementation

of the file.

%
%

b:

output binary stream

file:

input file

%
%

by Michael Christensen

December 14, 2005

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%

Step 1:

Read in the file, obtain parameters

%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

A = imread(file); %reads file into a matrix

B = imfinfo(file); %reads file info

%convert to YCbCr
%if B.Format=='bmp'
%

A=rgb2ycbcr(A)

width=B.Width;
height=B.Height;

%detirmine number of 8x8 matrices, use ceil to round up


W=ceil(width/8);
H=ceil(height/8);

%create a matrix of zeros and add the image to it to fill out the 8x8
%matrices

(matrix will stay the same size if height and width are

%divisible by 8
I=zeros(H*8,W*8,'uint8');
I(1:height,1:width)=A(1:height,1:width);

%divide numbers into WxH 8x8 matrices


X=zeros(H,W,8,8);
for J=1:H
for K=1:W
for j=1:8
for k=1:8
X(J,K,j,k)=I((J-1)*8+j,(K-1)*8+k);
end
end
end
end

%define luminance quantization matrix


Q=[...
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];

b=[];

vprev=[];
vcurrent=[];
for J=1:H
for K=1:W
temp=zeros(8,8,'uint8');%create temporary matrix
temp(:,:)=X(J,K,:,:);%add values from current 8x8 sector
temp=double(temp);%convert numbers to double format(floating point)
temp=temp-128;%shift mean to zero
temp=dct2(temp);%perform 2-D cosine transfer function
temp=temp./Q;%devide by quantization matrix
temp=round(temp);%round off the quantized matrix
vcurrent=zigzag(temp);%convert quantized matrix to 1-D vector
vcurrent=shorten(vcurrent);%remove extra zeros from vector
if J==1 && K==1
b=[b vecenc(vcurrent)];
else
vcurrent(1)=vcurrent(1) - vprev(1);%take difference of first value
b=[b vecenc(vcurrent)];
end
vprev=vcurrent;
end
end

b=[head(Q) b 1 1 1 1 1 1 1 1
EOF End of File

1 0 0 1 1 0 1 1];%last two bytes:ff d9 denote

Step 2: Zig-Zag Encoding of Quantized Matrices


Step 3: Conversion of quantized vectors into the JPEG defined bitstream
Step 4: Construction of the JPEG File header, Writing the File

You might also like