% Color JPEG Compression Standart
clear all;clc;close all;
%1. Read Input Image %
im = imread('lena.tif');
[rows, cols, color_space] = size(im);
%2. Convert RGB to YUV (Y'CbCr) %
RGB_image = double(im);
Y = 0.299*RGB_image(:,:,1) + 0.587*RGB_image(:,:,2) + 0.114*RGB_image(:,:,3)
+ 16; % Y = Y' %
U = -0.169*RGB_image(:,:,1) - 0.331*RGB_image(:,:,2) + 0.500*RGB_image(:,:,3)
+ 128; % U = Cb ~ Y-B %
V = 0.500*RGB_image(:,:,1) - 0.419*RGB_image(:,:,2) - 0.081*RGB_image(:,:,3)
+ 128; % V = Cr ~ Y-R %
YUV_image(:,:,1) = Y;
YUV_image(:,:,2) = U;
YUV_image(:,:,3) = V;
%3. Blocking 8x8 %
MM = ceil(cols/8); % MM = Horizontal Block %
NN = ceil(rows/8); % NN = Vertical Block
%
Y_Block = zeros(8,8,MM*NN);
U_Block = Y_Block;
V_Block = Y_Block;
n=0;
for i=0:NN-1
for j=0:MM-1
n=n+1;
Y_Block(:,:,n) = YUV_image(j*8+1:j*8+8, i*8+1:i*8+8, 1);
U_Block(:,:,n) = YUV_image(j*8+1:j*8+8, i*8+1:i*8+8, 2);
V_Block(:,:,n) = YUV_image(j*8+1:j*8+8, i*8+1:i*8+8, 3);
end;
end;
%4. Discrete Cosine Transform (DCT) %
Gy = zeros(8,8,MM*NN);
Gu = Gy;
Gv = Gy;
for i=1:MM*NN
Gy(:,:,i) = dct2(Y_Block(:,:,i)-128);
Gu(:,:,i) = dct2(Y_Block(:,:,i)-128);
Gv(:,:,i) = dct2(Y_Block(:,:,i)-128);
end;
%5. Quantization %
% Luminance Quantization Tables
Ql_Table = [
16 11 10 16 24 40
12 12 14 19 26 58
14 13 16 24 40 57
14 17 22 29 51 87
18 22 37 56 68 109
24 35 55 64 81 104
49 64 78 87 103 121
72 92 95 98 112 100
(8x8) %
51 61
60 55
69 56
80 62
103 77
113 92
120 101
103 99
];
% Chrominance Quantization
Qc_Table = [
17 18 24 47 99
18 21 26 66 99
24 26 56 99 99
47 66 99 99 99
99 99 99 99 99
99 99 99 99 99
99 99 99 99 99
99 99 99 99 99
];
Tables (8x8) %
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
99
By = zeros(8,8,MM*NN);
Bu = By;
Bv = By;
for i=1:MM*NN
By(:,:,i) = round(Gy(:,:,i)./Ql_Table);
Bu(:,:,i) = round(Gu(:,:,i)./Qc_Table);
Bv(:,:,i) = round(Gv(:,:,i)./Qc_Table);
end;
% Proses Decoding %
%6. Dequantization
Dy = zeros(8,8,MM*NN);
Du = Dy;
Dv = Dy;
for i=1:MM*NN
Dy(:,:,i) = round(By(:,:,i).*Ql_Table);
Du(:,:,i) = round(Bu(:,:,i).*Qc_Table);
Dv(:,:,i) = round(Bv(:,:,i).*Qc_Table);
end;
%7. Invers Discrete Cosine Transform (IDCT)
Iy =
Iu =
Iv =
for
zeros(8,8,MM*NN);
Iy;
Iy;
i=1:MM*NN
Iy(:,:,i) = idct2(Dy(:,:,i));
Iu(:,:,i) = idct2(Du(:,:,i));
Iv(:,:,i) = idct2(Dv(:,:,i));
end;
%8. Tambahkan dengan nilai skalar 128 %
Sy = zeros(8,8,MM*NN);
Su = Sy;
Sv = Sy;
for i=1:MM*NN
Sy(:,:,i) = (Iy(:,:,i)+128);
Su(:,:,i) = (Iu(:,:,i)+128);
Sv(:,:,i) = (Iv(:,:,i)+128);
end;
Keterangan :
Hasil DCT & IDCT hanya bisa dilihat dalam bentuk desimal. Belum
mengetahui bagaimana cara menampilkan dalam bentuk image (foto).
Untuk rumus DCT & IDCT mengambil langsung dari fungsi Matlab yaitu dct2
& idct2.