-
Notifications
You must be signed in to change notification settings - Fork 0
/
svdexample.m
54 lines (49 loc) · 1.42 KB
/
svdexample.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
function svdexample(percent)
% To run, type svdexample(X), where
% X is 0 <= X <= 100, the percentage of
% singular values that you want to use in
% the compressed image. On output, a png file
% will be created and displayed. It will be the
% compressed image.
% Note: to find out the actual compression achieved in the
% png image, query the size of the original file png and the
% one produced by this function.
% This function requires you obtain the png file
% lena.png to run.
I = imread('lena.png');
figure(1)
imshow(I);
fprintf('FIGURE 1 shows uncompressed image \n')
[m n c] = size(I);
I = double(I);
R = I(:, :, 1);
G = I(:, :, 2);
B = I(:, :, 3);
[RU, RS, RV] = svd(R);
% TIPS: svd(R) returns diag(RS) because it is the most important
[GU, GS, GV] = svd(G);
[BU, BS, BV] = svd(B);
RdS = diag(RS);
GdS = diag(GS);
BdS = diag(BS);
D = length(RdS);
DIM = floor(D*(percent*0.01));
RdS(DIM+1:end) = 0; % reduce dimensions
GdS(DIM+1:end) = 0;
BdS(DIM+1:end) = 0;
RSR = sparse(1:D, 1:D, RdS, m, n); % reconstruct into matrix
GSR = sparse(1:D, 1:D, GdS, m, n);
BSR = sparse(1:D, 1:D, BdS, m, n);
RR = RU * RSR * RV'; % recover
GR = GU * GSR * GV';
BR = BU * BSR * BV';
IR = cat(3, RR, GR, BR);
IR = uint8(IR);
figure(2)
imshow(IR);
fprintf('FIGURE 2 shows compressed image. \n')
imwrite(IR, sprintf('lenasvd%02d.png', percent), 'PNG');
disp('hit any key to close off displayed image')
pause
close all
end