-
Notifications
You must be signed in to change notification settings - Fork 0
/
wavelet_script.m
44 lines (34 loc) · 1.09 KB
/
wavelet_script.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
step = 1000;
N_ks = 30;
errors = zeros(1, N_ks);
for i= 1:N_ks
% Coefficients to keep
k = i*step;
fprintf('Compressing Image using %s wavelet by keeping %d coefficients\n', wname, k);
% Compress and decompress
[C, S] = wavedec2(I, level, wname);
nz_before = nnz(C);
C_hat = threshold(C, k);
I_rec = waverec2(C_hat, S, wname);
nz_after = nnz(C_hat);
fprintf('Nonzero after: %d\n', nz_after);
fprintf('Compression Ratio: %f%% \n', nz_after/nz_before*100);
fprintf('Space Savings: %f%% \n\n', (1-nz_after/nz_before)*100);
% Save image
fname = strcat(img_name, '_', wname, '_', int2str(k));
imwrite(uint8(I_rec), filename(image_dir, fname, 'tiff'), 'tiff');
% Save relative L2 error
errors(i) = rel_error(I, I_rec);
end
%% Plot the results
h = figure;
plot(1:step:N_ks*step, errors);
% Save figure
fname = strcat(img_name, '_', wname);
print(h, '-depsc2', '-tiff', filename(figures_dir, fname, 'eps'));
% Plot to combined plot
h = figure(1);
hold all;
plot(1:step:N_ks*step, errors);
hold off;
labels = strvcat(labels, wname);