forked from malevolentstrix/SVD_Image_Compression
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SVD_code.m
205 lines (202 loc) · 6.3 KB
/
SVD_code.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
close all
clear all
clc
inImage=imread('bridge.png');
X=im2gray(inImage); % Convert RBG to gray, 256 bit to double.
Y=im2double(X);
filename = 'bridge.png';
inImage=rgb2gray(inImage);
inImageD=double(inImage);
[a,b,c] = svd(Y);
[X, map] = imread(filename);
figure('Name','ORIGINAL component of the imported image');
imshow(X);
imwrite(X, '!original.png');
R = X(:,:,1);
G = X(:,:,2);
B = X(:,:,3);
Rimg = cat(3, R, zeros(size(R)), zeros(size(R)));
Gimg = cat(3, zeros(size(G)), G, zeros(size(G)));
Bimg = cat(3, zeros(size(B)), zeros(size(B)), B);
%figure('Name','RED component of the imported image');
%imshow(Rimg);
%imwrite(Rimg, '!red.jpg');
%figure('Name','GREEN component of the imported image');
%imshow(Gimg);
%imwrite(Gimg, '!green.jpg');
%figure('Name','BLUE component of the imported image');
%imshow(Bimg);
%imwrite(Bimg, '!blue.jpg');
Red =double(R);
Green = double(G);
Blue = double(B);
dispEr = [];
numSVals = [];
N = 1;
% Compute values for the red image
[U,S,V]=svd(Red);
C = S;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Dr=U*C*V';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Red image output using %d singular values', N);
Rimg = cat(3, Dr, zeros(size(Dr)), zeros(size(Dr)));
%imshow(uint8(Rimg));
%imwrite(uint8(Rimg), sprintf('%dred.jpg', N));
%title(buffer);
% Compute values for the green image
[U2, S2, V2]=svd(Green);
C = S2;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Dg=U2*C*V2';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Green image output using %d singular values', N);
Gimg = cat(3, zeros(size(Dg)), Dg, zeros(size(Dg)));
%imshow(uint8(Gimg));
%imwrite(uint8(Gimg), sprintf('%dgreen.jpg', N));
%title(buffer);
% Compute values for the blue image
[U3, S3, V3]=svd(Blue);
C = S3;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Db=U3*C*V3';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Blue image output using %d singular values', N);
Bimg = cat(3, zeros(size(Db)), zeros(size(Db)), Db);
%imshow(uint8(Bimg));
%imwrite(uint8(Bimg), sprintf('%dblue.jpg', N));
%title(buffer);
% Thake the data from the Red, Green, and Blue image
% Rebuild a colored image with the corresponding data and show it
figure;
buffer = sprintf('Colored image output using %d singular values', N);
Cimg = cat(3, Dr, Dg, Db);
imshow(uint8(Cimg));
imwrite(uint8(Cimg), sprintf('%dcolor.png', N));
title(buffer);
error=sum(sum((inImageD-Db).^2));
dispEr = [dispEr; error];
numSVals = [numSVals; N];
for N=10:10:100
% Recompute modes for the red image - already solved by SVD above
C = S;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Dr=U*C*V';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Red image output using %d singular values', N);
Rimg = cat(3, Dr, zeros(size(Dr)), zeros(size(Dr)));
%imshow(uint8(Rimg));
%imwrite(uint8(Rimg), sprintf('%dred.jpg', N));
%title(buffer);
% Recompute modes for the green image - already solved by SVD above
C = S2;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Dg=U2*C*V2';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Green image output using %d singular values', N);
Gimg = cat(3, zeros(size(Dg)), Dg, zeros(size(Dg)));
%imshow(uint8(Gimg));
%imwrite(uint8(Gimg), sprintf('%dgreen.jpg', N));
%title(buffer);
% Recompute modes for the blue image - already solved by SVD above
C = S3;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Db=U3*C*V3';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Blue image output using %d singular values', N);
Bimg = cat(3, zeros(size(Db)), zeros(size(Db)), Db);
%imshow(uint8(Bimg));
%imwrite(uint8(Bimg), sprintf('%dblue.jpg', N));
%title(buffer);
% Thake the data from the Red, Green, and Blue image
% Rebuild a colored image with the corresponding data and show it
figure;
buffer = sprintf('Colored image output using %d singular values', N);
Cimg = cat(3, Dr, Dg, Db);
imshow(uint8(Cimg));
imwrite(uint8(Cimg), sprintf('%dcolor.png', N));
title(buffer);
error=sum(sum((inImageD-Db).^2));
dispEr = [dispEr; error];
numSVals = [numSVals; N];
end
for N=100:50:300
% Recompute modes for the red image - already solved by SVD above
C = S;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Dr=U*C*V';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Red image output using %d singular values', N);
Rimg = cat(3, Dr, zeros(size(Dr)), zeros(size(Dr)));
%imshow(uint8(Rimg));
%imwrite(uint8(Rimg), sprintf('%dred.jpg', N));
%title(buffer);
% Recompute modes for the green image - already solved by SVD above
C = S2;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Dg=U2*C*V2';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Green image output using %d singular values', N);
Gimg = cat(3, zeros(size(Dg)), Dg, zeros(size(Dg)));
%imshow(uint8(Gimg));
%imwrite(uint8(Gimg), sprintf('%dgreen.jpg', N));
%title(buffer);
% Recompute modes for the blue image - already solved by SVD above
C = S3;
C(N+1:end,:)=0;
C(:,N+1:end)=0;
Db=U3*C*V3';
% Rebuild the data back into a displayable image and show it
%figure;
%buffer = sprintf('Blue image output using %d singular values', N);
Bimg = cat(3, zeros(size(Db)), zeros(size(Db)), Db);
%imshow(uint8(Bimg));
%imwrite(uint8(Bimg), sprintf('%dblue.jpg', N));
%title(buffer);
% Thake the data from the Red, Green, and Blue image
% Rebuild a colored image with the corresponding data and show it
figure;
buffer = sprintf('Colored image output using %d singular values', N);
Cimg = cat(3, Dr, Dg, Db);
imshow(uint8(Cimg));
imwrite(uint8(Cimg), sprintf('%dcolor.png', N));
title(buffer);
error=sum(sum((inImageD-Db).^2));
% store vals for display
dispEr = [dispEr; error];
numSVals = [numSVals; N];
end
figure;
title('Error in compression');
plot(numSVals, dispEr);
grid on
xlabel('Number of Singular Values used');
ylabel('Error between compress and original image');
r=1:450;
for i=1:450
Xap=a(:,1:i)*b(1:i,1:i)*c(:,1:i)';
%error(i)=immse(Xap,Y);
psne(i)=psnr(Xap,Y);
end
figure
plot(r,psne);
hold on
ylabel('PSNR')
xlabel('r value')
title('r vs PSNR')