-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_testversion.m
140 lines (107 loc) · 3.64 KB
/
main_testversion.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
clear;
close all;
% Environment initialization
rate = 8; %[Bit/pixel]
cb_size = 2^rate; % codebook size
filename = 'images/10.tiff';
info = imfinfo(filename);
width = info.Width;
height = info.Height;
img_original = imread(filename);
x = im2double(reshape(img_original,[], 3));
%% CODEBOOK EVALUATION
codebook = mean(x);
coded_img = ones(size(x,1),1);
j = 2;
% Parameters set up
epsilon = [0.001 0.01 0.1];
gain = 1;
delta = 0.2;
for a = 1:3
codebook = mean(x);
coded_img = ones(size(x,1),1);
j = 2;
compressed_img = zeros(size(x,1),3);
distortion = zeros(log2(cb_size),1);
psnr_values = zeros(log2(cb_size),1);
l = figure;
axis tight manual
for i = 1:log2(cb_size)
codebook(j:size(codebook,1)*2,:) = mod(codebook+delta,1);
[codebook, coded_img] = LBG(x, codebook, coded_img, epsilon, gain);
frame = getframe(l);
im = frame2im(frame);
[imind,cm] = rgb2ind(im,256);
% Write to the GIF File
if i == 1
imwrite(imind,cm,'test.gif','gif', 'Loopcount',inf);
else
imwrite(imind,cm,'test.gif','gif','WriteMode','append');
end
for h=1:size(x,1)
compressed_img(h,:) = codebook(coded_img(h,1),:);
end
imwrite(reshape(compressed_img,width,height,3),sprintf('img_raw_%d.tiff',i),'tiff');
img_draft = imread(sprintf('img_raw_%d.tiff',i));
distortion(i) = immse(img_draft,img_original);
psnr_values(i) = psnr(img_draft,img_original,255);
j = size(codebook,1) + 1;
end
hold on
pcshow(im2double(img_original))
title('RGB pixel cloud')
legend('LBG Codebook', 'Image pixel cloud')
save(sprintf('distortion_10%d.mat',a),'distortion');
save(sprintf('psnr_10%d.mat',a),'psnr_values')
end
%%
imwrite(img_original,'img_jpg.jpeg','jpg','Quality',100);
img_jpg = imread('img_jpg.jpeg');
info2 = imfinfo('img_jpg.jpeg');
img_draft = imread('img_raw_8.tiff');
info3 = imfinfo('img_raw_8.tiff');
tiff_rate = (info3.FileSize * 8)/(size(x,1)*8);
%%
for i = 1:8
img_one = imread(sprintf('img_raw_%d.tiff',i));
imwrite(img_one,sprintf('img_png_%d.png',i),'png');
end
%%
jpg_rate = zeros(10,1);
jpg_distortion = zeros(10,1);
jpg_psnr = zeros(10,1);
for i = 1:10
imwrite(img_original,'X.jpeg','jpg','Quality',i*10);
xr = imread('X.jpeg');
infoxr = imfinfo('X.jpeg');
Size = infoxr.FileSize*8;
jpg_rate(i) = Size/size(x,1);
jpg_distortion(i) = immse(xr,img_original);
jpg_psnr(i) = psnr(xr,img_original,255);
end
dist1 = load('distortion_101');
dist2 = load('distortion_102');
dist3 = load('distortion_103');
figure
plot(log2([2 4 8 16 32 64 128 256]),dist1.distortion,'-x','Linewidth',1.5)
grid on
hold on
plot(log2([2 4 8 16 32 64 128 256]),dist2.distortion,'-x','Linewidth',1.5)
plot(log2([2 4 8 16 32 64 128 256]),dist3.distortion,'-x','Linewidth',1.5)
plot(jpg_rate, jpg_distortion,'-x','Linewidth',2)
ylabel('Distortion [mse]')
xlabel('Rate [bit/pixel]')
legend('LBG - \epsilon = 0.001', 'LBG - \epsilon = 0.01', 'LBG - \epsilon = 0.1', 'JPG','Location','northwest')
psnr1 = load('psnr_101');
psnr2 = load('psnr_102');
psnr3 = load('psnr_103');
figure
plot(log2([2 4 8 16 32 64 128 256]),psnr1.psnr_values,'-x','Linewidth',1.5)
grid on
hold on
plot(log2([2 4 8 16 32 64 128 256]),psnr2.psnr_values,'-x','Linewidth',1.5)
plot(log2([2 4 8 16 32 64 128 256]),psnr3.psnr_values,'-x','Linewidth',1.5)
plot(jpg_rate, jpg_psnr,'-x','Linewidth',2)
ylabel('PSNR [dB]')
xlabel('Rate [bit/pixel]')
legend('LBG - \epsilon = 0.001', 'LBG - \epsilon = 0.01', 'LBG - \epsilon = 0.1', 'JPG','Location','northwest')