Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 28

JPEG Still Image

Compression
Zeeshan Akhtar
Mtech (1st year )
14-LECM 018
GI-4784
A.M.U. aligarh

Encoding

JPEG:Basic Algorithm

Step 1-

Read the image


Convert it to grayscale
Offset level
Determine the size of
image

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

Perform Differential Coding


on DC part separated

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

Run length coding of AC


separated

L2 = (X*Y);
for i=1:L2
AC_array = AC_matrix(i,:);
coded_AC(i,:) = rle(AC_array);
end

Decoding

Step 1- AC decoding(Run length)

for i = 1:L2
decoded_AC(i,:) =
rle(coded_AC(i,:));
end

Step2- Diffrential Decoding

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

Inverse ZiG- ZaG Mapping


function inv=invzigzag(T);
inv=[
T(1,1)
T(1,16) T(1,28)
T(1,3)
T(1,27) T(1,30)
T(1,4)
T(1,31) T(1,42)
T(1,10)
T(1,41) T(1,45)
T(1,11)
T(1,46) T(1,53)
T(1,21)
T(1,52) T(1,56)
T(1,22)
T(1,57) T(1,60)
T(1,36)
T(1,59) T(1,63)

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)

T(1,13) T(1,18) T(1,26)


T(1,19) T(1,25) T(1,32)
T(1,24) T(1,33) T(1,40)
T(1,34) T(1,39) T(1,47)
T(1,38) T(1,48) T(1,51)
T(1,49) T(1,50) T(1,58)
]

Step 4 - Inverse Offset

uplevel_image = lowlevel_image + 128;


Image = uint8(uplevel_image);

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

You might also like