-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo1.m
162 lines (145 loc) · 6 KB
/
demo1.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
%% load images and define qTable for Y and Cb/Cr %%
clear all; close all; clc
load('img1_down.mat')
%~ Make the image to be exactly for blocks 8x8, whithout leftovers ~%
[N, M, ~] = size(img1_down); N = mod(N, 16); M = mod(M, 16);
img1_down = img1_down(1:end-N, 1:end-M, :);
load('img2_down.mat')
%~ Make the image to be exactly for blocks 8x8, whithout leftovers ~%
[N, M, ~] = size(img2_down); N = mod(N, 16); M = mod(M, 16);
img2_down = img2_down(1:end-N, 1:end-M, :);
%~ Define qTable for Y and Cb/Cr ~%
global qTableL qTableC;
[qTableL, qTableC] = changedTables(0);
%% First image for 4:2:2 subsampling. %%
subimg = [4 2 2];
[imageY, imageCb, imageCr] = convert2ycbcr(img1_down, subimg);
imageRGB = convert2rgb(imageY, imageCr, imageCb, subimg);
figure;
subplot(1,3,1)
imshow(img1_down)
title('Original Image', 'Interpreter', 'latex')
subplot(1,3,2)
imshow(imageRGB);
title('Reconstructed Image - Subsampling 4:2:2', 'Interpreter', 'latex')
subplot(1,3,3)
imshow(img1_down - imageRGB);
title('Error on reconstruction', 'Interpreter', 'latex')
%% Second image for 4:4:4 subsampling. %%
subimg = [4 4 4];
[imageY, imageCb, imageCr] = convert2ycbcr(img2_down, subimg);
imageRGB = convert2rgb(imageY, imageCr, imageCb, subimg);
figure;
subplot(1,3,1)
imshow(img2_down)
title('Original Image', 'Interpreter', 'latex')
subplot(1,3,2)
imshow(imageRGB);
title('Reconstructed Image - Subsampling 4:4:4', 'Interpreter', 'latex')
subplot(1,3,3)
imshow(img2_down - imageRGB);
title('Error on reconstruction', 'Interpreter', 'latex')
%% First image for 4:2:2 subsampling and qScale = 0.6%%
clear imageY imageCb imageCr
clear imageY_rec imageCr_rec imageCb_rec
subimg = [4 2 2]; qScale = 0.6;
[imageY, imageCb, imageCr] = convert2ycbcr(img1_down, subimg);
%~ For all the blocks compute DCT and quantize them.~%
[RowNumber, ColumnNumber] = size(imageY);
%~ For the Y component ~%
for row = 1:8:RowNumber
for column = 1:8:ColumnNumber
blockY = imageY(row:row+7, column:column+7);
dctblockY = blockDCT(blockY);
quantblockY = quantizeJPEG(dctblockY, qTableL, qScale);
%~ For all the quantized blocks compute idCT. ~%
de_quantblockY = dequantizeJPEG(quantblockY, qTableL, qScale);
idctblockY = iBlockDCT(de_quantblockY);
imageY_rec(row:row+7, column:column+7) = idctblockY;
end
end
[RowNumber, ColumnNumber] = size(imageCb);
%~ For the Cb, Cr components ~%
for row = 1:8:RowNumber
for column = 1:8:ColumnNumber
blockCr = imageCr(row:row+7, column:column+7);
blockCb = imageCb(row:row+7, column:column+7);
dctblockCr = blockDCT(blockCr);
dctblockCb = blockDCT(blockCb);
quantblockCr = quantizeJPEG(dctblockCr, qTableC, qScale);
quantblockCb = quantizeJPEG(dctblockCb, qTableC, qScale);
%~ For all the quantized blocks compute idCT. ~%
de_quantblockCr = dequantizeJPEG(quantblockCr, qTableC, qScale);
de_quantblockCb = dequantizeJPEG(quantblockCb, qTableC, qScale);
idctblockCr = iBlockDCT(de_quantblockCr);
idctblockCb = iBlockDCT(de_quantblockCb);
imageCr_rec(row:row+7, column:column+7) = idctblockCr;
imageCb_rec(row:row+7, column:column+7) = idctblockCb;
end
end
%~ Reconstruct RGB image. ~%
imageRGB = convert2rgb(imageY_rec, imageCr_rec, imageCb_rec, subimg);
figure;
subplot(1,3,1)
imshow(img1_down)
title('Original Image', 'Interpreter', 'latex')
subplot(1,3,2)
imshow(imageRGB);
title('Reconstructed Image - Subsampling 4:2:2 , qScale = 0.6', 'Interpreter', 'latex')
subplot(1,3,3)
imshow(img1_down - imageRGB);
title('Quantization and SubSampling error', 'Interpreter', 'latex')
MSE = sum((img1_down(:) - imageRGB(:)) .^2) / numel(img1_down);
fprintf('Mean Square Error for the 1st image is: %f. [qScale = 0.6, Subsampling 4:2:2]\n', MSE);
%% Second image for 4:4:4 subsampling and qScale = 5%%
clear imageY imageCb imageCr
clear imageY_rec imageCr_rec imageCb_rec
subimg = [4 4 4]; qScale = 5;
[imageY, imageCb, imageCr] = convert2ycbcr(img2_down, subimg);
%~ For all the blocks compute DCT and quantize them.~%
[RowNumber, ColumnNumber] = size(imageY);
%~ For the Y component ~%
for row = 1:8:RowNumber
for column = 1:8:ColumnNumber
blockY = imageY(row:row+7, column:column+7);
dctblockY = blockDCT(blockY);
quantblockY = quantizeJPEG(dctblockY, qTableL, qScale);
%~ For all the quantized blocks compute idCT. ~%
de_quantblockY = dequantizeJPEG(quantblockY, qTableL, qScale);
idctblockY = iBlockDCT(de_quantblockY);
imageY_rec(row:row+7, column:column+7) = idctblockY;
end
end
[RowNumber, ColumnNumber] = size(imageCb);
%~ For the Cb, Cr components ~%
for row = 1:8:RowNumber
for column = 1:8:ColumnNumber
blockCr = imageCr(row:row+7, column:column+7);
blockCb = imageCb(row:row+7, column:column+7);
dctblockCr = blockDCT(blockCr);
dctblockCb = blockDCT(blockCb);
quantblockCr = quantizeJPEG(dctblockCr, qTableC, qScale);
quantblockCb = quantizeJPEG(dctblockCb, qTableC, qScale);
%~ For all the quantized blocks compute idCT. ~%
de_quantblockCr = dequantizeJPEG(quantblockCr, qTableC, qScale);
de_quantblockCb = dequantizeJPEG(quantblockCb, qTableC, qScale);
idctblockCr = iBlockDCT(de_quantblockCr);
idctblockCb = iBlockDCT(de_quantblockCb);
imageCr_rec(row:row+7, column:column+7) = idctblockCr;
imageCb_rec(row:row+7, column:column+7) = idctblockCb;
end
end
%~ Reconstruct RGB image. ~%
imageRGB = convert2rgb(imageY_rec, imageCr_rec, imageCb_rec, subimg);
figure;
subplot(1,3,1)
imshow(img2_down)
title('Original Image', 'Interpreter', 'latex')
subplot(1,3,2)
imshow(imageRGB);
title('Reconstructed Image - Subsampling 4:4:4 , qScale = 5', 'Interpreter', 'latex')
subplot(1,3,3)
imshow(img2_down - imageRGB);
title('Quantization error', 'Interpreter', 'latex')
MSE = sum((img2_down(:) - imageRGB(:)) .^2) / numel(img2_down);
fprintf('Mean Square Error for the 2nd image is: %f. [qScale = 5, Subsampling 4:4:4]\n', MSE);