-
Notifications
You must be signed in to change notification settings - Fork 0
/
DCT.m
52 lines (50 loc) · 1.28 KB
/
DCT.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
clc;
clear ;
close all;
Q = [16 11 10 16 24 40 51 61 ;
12 12 14 19 26 28 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];
I = imread('hardik.bmp');
ImageSize = 8*numel(I);
Y_di = rgb2ycbcr( I );
% Downsample:
Y_di(:,:,2) = 2*round(Y_di(:,:,2)/2);
Y_di(:,:,3) = 2*round(Y_di(:,:,3)/2);
% DCT compress:
A = zeros(size(Y_di));
B = A;
for channel = 1:3
for j = 1:8:size(Y_di,1)-7
for k = 1:8:size(Y_di,2)-7
II = Y_di(j:j+7,k:k+7,channel);
freq = dct2(II);
freq = Q.*round(freq./Q);
A(j:j+7,k:k+7,channel) = freq;
% inverse at the same time:
B(j:j+7,k:k+7,channel) = idct2(freq);
end
end
end
b = A(:);
b = b(:);
b(b==0)=[];
b = floor(255*(b-min(b))/(max(b)-min(b)));
symbols = unique(b);
prob = histcounts(b,length(symbols))/length(b);
dict = huffmandict(symbols, prob);
enco = huffmanenco(b, dict);
FinalCompressedImage = length(enco);
disp('Compression Ratio is :');
CR = ImageSize/FinalCompressedImage;
fprintf(' %d bytes\n', CR);
subplot(1,2,1)
imshow(I)
title('Original Image')
subplot(1,2,2)
imshow(ycbcr2rgb(uint8(B)));
title('Compressed Image')