Professional Documents
Culture Documents
JPEG - Still Image Compression
JPEG - Still Image Compression
Compression
Zeeshan Akhtar
Mtech (1st year )
14-LECM 018
GI-4784
A.M.U. aligarh
Encoding
JPEG:Basic Algorithm
Step 1-
image = imread('leena.jpg');
gray = rgb2gray(image);
d = double(gray);
D = d-128;%offset
[M,N] = size(D);
Step 2
Zero Padding
Determine the new size
of image
Rem_M = mod(M,8);
Rem_N = mod(N,8);
Extra_M = 8 - Rem_M;
Extra_N = 8 - Rem_N;
if Extra_M==0
M_new = M;
else
M_new = M + Extra_M;
end
if Extra_N==0
N_new = N;
else N_new = N + Extra_N;
end
D_new = padarray(D,[Extra_M
Extra_N],'post');
Step 3
Define Quantization table
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 36 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99];
Step 4
Break image in small blocks of 8*8
dimension
Perform DCT on each block
Perform quantization
Separate AC and DC part of the image
Zig zag Scanning
X = M_new/8;
Y = N_new/8;
DC_array = zeros(1,X*Y);
AC_matrix= zeros(X*Y,63);
c=1; d=8; t = 1;
for j = 1:X
a=1;
b=8;
for i = 1:Y
small_D = D_new(c:d,a:b);
dct = round(dct2(small_D));
Quant_D = round(dct./Q);
DC_array(t) = Quant_D(1,1);
AC_matrix(t,1:63) =
zigzag2(Quant_D);
t = t + 1;
a = a+8;
b = b+8;
end
c = c+8;
d = d+8;
end
function file=zigzag(T);
file=[
T(1,2) T(2,1) T(3,1) T(2,2) ...
T(1,3) T(1,4) T(2,3) T(3,2) T(4,1) ...
T(5,1) T(4,2) T(3,3) T(2,4) T(1,5) ...
T(1,6) T(2,5) T(3,4) T(4,3) T(5,2) ...
T(6,1) T(7,1) T(6,2) T(5,3) T(4,4) ...
T(3,5) T(2,6) T(1,7) T(1,8) T(2,7) ...
T(3,6) T(4,5) T(5,4) T(6,3) T(7,2) ...
T(8,1) T(8,2) T(7,3) T(6,4) T(5,5) ...
T(4,6) T(3,7) T(2,8) T(3,8) T(4,7) ...
T(5,6) T(6,5) T(7,4) T(8,3) T(8,4) ...
T(7,5) T(6,6) T(5,7) T(4,8) T(5,8) ...
T(6,7) T(7,6) T(8,5) T(8,6) T(7,7) ...
T(6,8) T(7,8) T(8,7) T(8,8)]
Step5
coded_DC = zeros(1,X*Y);
L1 = (X*Y)-1;
for i=1:L1
coded_DC(i+1) =
DC_array(i+1)- DC_array(i);
end
coded_DC(1) = DC_array(1);
Step 6
L2 = (X*Y);
for i=1:L2
AC_array = AC_matrix(i,:);
coded_AC(i,:) = rle(AC_array);
end
Decoding
for i = 1:L2
decoded_AC(i,:) =
rle(coded_AC(i,:));
end
decoded_DC(1) = coded_DC(1);
for i=1:L1
decoded_DC(i+1) = decoded_DC(i)
+ coded_DC(i+1);
end
Step3
IDCT
INVERSE ZIG ZAG SCANNING
IMAGE RECONSTRUCTION
lowlevel_image = zeros(N_new,M_new);
a=1;
b=8;
c=1;
d=8;
t=1;
for i = 1:X
for j= 1:Y
AC_DC_array = AC_DC(t,:);
q=zeros(8,8);
q = invzigzag(AC_DC_array);
Inv_Q = round(q.*Q);
lowlevel_image(a:b,c:d) = round(idct2(Inv_Q));
c=c+8;
d=d+8;
t=t+1;
end
c=1;
d=8;
a=a+8;
b=b+8;
end
T(1,2)
T(1,29)
T(1,5)
T(1,43)
T(1,9)
T(1,44)
T(1,12)
T(1,54)
T(1,20)
T(1,55)
T(1,23)
T(1,61)
T(1,35)
T(1,62)
T(1,37)
T(1,64)
T(1,6)
T(1,7)
T(1,15)
T(1,8)
T(1,14) T(1,17)
Step 5
Display both the original and
compressed images
Compare both the images
Find and display the difference between
two.
figure;
imshow(gray);
title('original image')
figure;
imshow(Image);
title('after decoding')
difference= (d)(uplevel_image);
figure;
imshow(difference);
Thank You