-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathn_convolution.m
More file actions
executable file
·451 lines (390 loc) · 14.6 KB
/
n_convolution.m
File metadata and controls
executable file
·451 lines (390 loc) · 14.6 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
function [rec, Frames] = n_convolution(cols,rows,values,ss,factor, imOrig, noiseCorrect, TwoPass)
% NORMALIZEDCONVOLUTION - reconstruct high resolution image using normalized convolution algorithm
% [rec, Frames] = n_convolution(cols,rows,values,ss,factor, imOrig, noiseCorrect, TwoPass)
% reconstruct an image with FACTOR times more pixels in both dimensions
% using normalized convolution and using the shift and rotation
% information from DELTA_EST and PHI_EST
% in:
% s: images in cell array (s{1}, s{2},...)
% delta_est(i,Dy:Dx) estimated shifts in y and x
% factor: gives size of reconstructed image
%% -----------------------------------------------------------------------
% SUPERRESOLUTION - Graphical User Interface for Super-Resolution Imaging
% Copyright (C) 2005-2007 Laboratory of Audiovisual Communications (LCAV),
% Ecole Polytechnique Federale de Lausanne (EPFL),
% CH-1015 Lausanne, Switzerland
%
% This program is free software; you can redistribute it and/or modify it
% under the terms of the GNU General Public License as published by the
% Free Software Foundation; either version 2 of the License, or (at your
% option) any later version. This software is distributed in the hope that
% it will be useful, but without any warranty; without even the implied
% warranty of merchantability or fitness for a particular purpose.
% See the GNU General Public License for more details
% (enclosed in the file GPL).
%
% Written by Karim Krichane, August 2006
% %% LITTLE TEST CODE TO SEE DIFFERENT APPLICABILITY FUNCTIONS
% n=8; %work with basis matrices of size 2n+1 by 2n+1
% [X, Y] = meshgrid(-n:n, -n:n);
% I = ones(2*n+1);
% x = X;
% y = Y;
% x2 = X.^2;
% y2 = Y.^2;
% xy = X.*Y;
% alphas = [0 1 2];
% betas = [0 0.5 1 1.5 2 2.5];
% for i = 1:length(alphas)
% figure('name', ['a = ' num2str(alphas(i))], 'NumberTitle', 'off');
% for j = 1:length(betas)
% subplot(2,3,j);
% a = applicability(i,j,n);
% surf(x,y,a); title(['b=' num2str(betas(j))]);
% end
% end
% %% ---------------------------------------------------
%% Initialization
%set outputFrames to true if you need every frame of the process as an output. This is
%useful for creating a movie showing the effect of the HR processing.
if nargout > 1
outputFrames = true;
else
outputFrames = false;
end
if nargin < 6
errordlg('Not enough input arguments', 'Error...');
elseif nargin == 6
noiseCorrect = false;
TwoPass = false;
end
rec = zeros(ss);
%By default, all certainties are set to 1
certainty = ones(length(rows),1);
%Parameters for the applicability function
alpha = 2;
beta = 2;
r_max = 4; %radius of the filters used in the convolution
% -- End of initialization
%% Certainty optimization for noise robustness
if noiseCorrect %optional noise cancelation
values_hat = zeros(length(rows),1);
sigma_noise = 1;
numRows = length(rows);
for k = 1:numRows
i = rows(k);
j = cols(k);
q11coord = find(abs(i-rows) <= r_max);
rows_temp = rows(q11coord);
cols_temp = cols(q11coord);
values_temp = values(q11coord);
certainty_temp = certainty(q11coord);
coord_temp = find(abs(j-cols_temp) <= r_max);
% if(length(coord_temp)<1)
% length(coord_temp)
% end
x_temp = rows_temp(coord_temp);
y_temp = cols_temp(coord_temp);
dx = abs(i - x_temp);
dy = abs(j - y_temp);
r = sqrt(dx.^2 + dy.^2); %distance from (i,j) to every other point of interest (x,y)
a = r.^(-alpha).*cos((pi*r)/(2*r_max)).^beta; %applicability function
a(isinf(a))=1;
%basis functions
B = zeros(length(dx), 6);
B(:,1) = ones(length(dx),1);
B(:,2) = x_temp - i; %x
B(:,3) = y_temp - j; %y
B(:,4) = dx.^2; %x^2
B(:,5) = B(:,2).*B(:,3); %xy
B(:,6) = dy.^2; %y^2
F = values_temp(coord_temp);
C = certainty_temp(coord_temp);
W = diag(C.*a);
% -- Optimization of the built-in pinv function --
% t = pinv(B' * W * B) * B' * W * F;
[u,s,v]=svd(B'*W*B);
%invert singular values only if they're greater than an epsylon
if(s(1,1)>1e-5)
s(1,1)=1./s(1,1);
if(s(2,2)>1e-5)
s(2,2)=1./s(2,2);
if(s(3,3)>1e-5)
s(3,3)=1./s(3,3);
if(s(4,4)>1e-5)
s(4,4)=1./s(4,4);
if(s(5,5)>1e-5)
s(5,5)=1./s(5,5);
if(s(6,6)>1e-5)
s(6,6)=1./s(6,6);
end
end
end
end
end
end
pin = u*s*v';
t = pin * B' * W * F;
% -- End of pinv optimization -------
values_hat(k) = t(1);
end %k
certainty = robustnorm2(values, values_hat, sigma_noise);
certainty = certainty > 0.98;
end %if
% -- End of certainty optimization
%% Movie variables
movieCounter = 1;
imOrigBig = imresize(imOrig, factor, 'nearest');
rec = imOrigBig;
if(outputFrames)
figure;
end
% -- End of Movie Variables
%% HR Reconstruction using normalized convolution
for i = 1:ss(1) %For all lines of the HR image...
q11coord = find(abs(i-rows) <= r_max);
rows_temp = rows(q11coord);
cols_temp = cols(q11coord);
values_temp = values(q11coord);
certainty_temp = certainty(q11coord);
% --- Save each movie frame ---
if(outputFrames)
imshow(rec);
Frames(movieCounter) = getframe;
movieCounter = movieCounter + 1;
end
% -----------------------------
for j = 1:ss(2) %For all columns of the HR image...
coord_temp = find(abs(j-cols_temp) <= r_max);
% if(length(coord_temp)<1)
% length(coord_temp)
% end
x_temp = rows_temp(coord_temp);
y_temp = cols_temp(coord_temp);
dx = abs(i - x_temp);
dy = abs(j - y_temp);
r = sqrt(dx.^2 + dy.^2); %distance from (i,j) to every other point of interest (x,y)
a = r.^(-alpha).*cos((pi*r)/(2*r_max)).^beta; %applicability function
a(isinf(a))=1;
%basis functions
B = zeros(length(dx), 6);
B(:,1) = ones(length(dx),1);
B(:,2) = x_temp - i; %x
B(:,3) = y_temp - j; %y
B(:,4) = dx.^2; %x^2
B(:,5) = B(:,2).*B(:,3); %xy
B(:,6) = dy.^2; %y^2
F = values_temp(coord_temp);
C = certainty_temp(coord_temp);
W = diag(C.*a);
% -- Optimization of the built-in pinv function --
% t = pinv(B' * W * B) * B' * W * F;
[u,s,v]=svd(B'*W*B);
%invert singular values only if they're greater than an epsylon
if(s(1,1)>1e-5)
s(1,1)=1./s(1,1);
if(s(2,2)>1e-5)
s(2,2)=1./s(2,2);
if(s(3,3)>1e-5)
s(3,3)=1./s(3,3);
if(s(4,4)>1e-5)
s(4,4)=1./s(4,4);
if(s(5,5)>1e-5)
s(5,5)=1./s(5,5);
if(s(6,6)>1e-5)
s(6,6)=1./s(6,6);
end
end
end
end
end
end
pin = u*s*v';
t = pin * B' * W * F;
% -- End of pinv optimization -------
rec(i,j) = t(1);
end %j
end %i
% -- End of HR Reconstruction
%% Structure-Adaptive Normalized Convolution
% This final processing is done as a second pass, only on pixels that have
% a high anisotropy
if TwoPass % optional second pass, which will sharpen all edges
derivY = [0 0 0;...
-1 0 1;...
0 0 0];
derivX = [0 -1 0;...
0 0 0;...
0 1 0];
gaussFilter = gausswin(7)*gausswin(7)';
gaussFilter = gaussFilter(2:6, 2:6);
gaussFilter = gaussFilter / sum(gaussFilter(:));
Ix = (imfilter(rec, derivX, 'symmetric'));
Iy = (imfilter(rec, derivY, 'symmetric'));
Ix2 = Ix .^ 2;
Iy2 = Iy .^ 2;
IxIy = Ix .* Iy;
Ix2 = imfilter(Ix2, gaussFilter, 'symmetric');
Iy2 = imfilter(Iy2, gaussFilter, 'symmetric');
IxIy = imfilter(IxIy, gaussFilter, 'symmetric');
% Creation of the density image. To create it, the certainty of each
% irregular sample is split to its four nearest HR grid points in a
% bilinear-weighting fashion.
D = zeros(ss);
for k = 1:length(values)
x_temp = rows(k);
y_temp = cols(k);
c_temp = certainty(k);
x1 = floor(x_temp);
x2 = x1 + 1;
y1 = floor(y_temp);
y2 = y1 + 1;
p = y_temp - y1;
q = x_temp - x1;
D(max(min(x1, ss(1)), 1), max(min(y1, ss(2)), 1)) = ...
D(max(min(x1, ss(1)), 1), max(min(y1, ss(2)), 1)) + (1-p)*(1-q)*c_temp;
D(max(min(x1, ss(1)), 1), max(min(y2, ss(2)), 1)) = ...
D(max(min(x1, ss(1)), 1), max(min(y2, ss(2)), 1)) + p*(1-q)*c_temp;
D(max(min(x2, ss(1)), 1), max(min(y1, ss(2)), 1)) = ...
D(max(min(x2, ss(1)), 1), max(min(y1, ss(2)), 1)) + (1-p)*q*c_temp;
D(max(min(x2, ss(1)), 1), max(min(y2, ss(2)), 1)) = ...
D(max(min(x2, ss(1)), 1), max(min(y2, ss(2)), 1)) + p*q*c_temp;
end %k
% -- End of density image creation
% Scale-space responses
i_try = [];
for i = -1:0.1:3
i_try = [i_try i];
end
SSR = zeros(ss(1),ss(2),length(i_try));
for i = 1:length(i_try)
SSR(:,:,i) = imfilter(D, gausswin(5, 2^(-2*i_try(i)))*gausswin(5, 2^(-2*i_try(i)))'); %Filter with a gaussian of sigma 2^i
end %i
[x i_opt] = min(abs(3-SSR), [], 3);
sigma_c = 2.^i_try(i_opt);
% -- End of scale-space responses
% Reconstruction process
r_max = 4; %redefine a new r_max now that the applicability function will be oriented
A = zeros(ss);
phi = zeros(ss);
%rec = imOrigBig;
for i = 1:ss(1) %For all lines of the HR image...
q11coord = find(abs(i-rows) <= r_max);
rows_temp = rows(q11coord);
cols_temp = cols(q11coord);
values_temp = values(q11coord);
certainty_temp = certainty(q11coord);
% --- Save each movie frame ---
if(outputFrames)
imshow(rec);
Frames(movieCounter) = getframe;
movieCounter = movieCounter + 1;
end
% -----------------------------
for j = 1:ss(2) %For all columns of the HR image...
tempMat = [Ix2(i,j) IxIy(i,j); IxIy(i,j) Iy2(i,j)];
[v, d] = eig(tempMat);
% if(d(1,1) ~= 0)
% [d(1,1) d(2,2)]
% end
%[i j]
% if(d(1,1)==0 && d(2,2)==0)
% disp(['all eig values zero: ' num2str(i) ' ' num2str(j)]);
% end
if(abs(d(1,1)) >= abs(d(2,2)))
lambda1 = d(1,1);
lambda2 = d(2,2);
vp1 = v(:,1);
else
lambda1 = d(2,2);
lambda2 = d(1,1);
vp1 = v(:,2);
end
if(abs(lambda1)>0.00001)
if(vp1(1) ~= 0)
phi(i,j) = atan(vp1(2)/vp1(1));
%phi(i,j) = pi-pi/6;
else
phi(i,j) = atan(Inf);
%phi(i,j) = pi-pi/6;
end
A(i,j) = (lambda1 - lambda2)/(lambda1 + lambda2);
else
phi(i,j) = 0;
A(i,j) = 0;
end
%phi(i,j) = - phi(i,j) - pi/2;
%phi(i,j) = pi/2;
%phi(i,j) = (-(phi(i,j)+pi/4)) + pi/4;
if(A(i,j)<0)
disp('Problem! Negative anisotropy...')
end
if(A(i,j)>0.5) %Only do the second pass where the anisotropy is high
coord_temp = find(abs(j-cols_temp) <= r_max);
% if(length(coord_temp)<1)
% length(coord_temp)
% end
x_temp = rows_temp(coord_temp);
y_temp = cols_temp(coord_temp);
dx = x_temp - i;
dy = y_temp - j;
%r = sqrt(dx.^2 + dy.^2); %distance from (i,j) to every other point of interest (x,y)
alpha_T = 0.5; %Tuning parameter to set an upper-bound on the eccentricity of the applicability function
sigma_u = (alpha_T/(alpha_T+A(i,j))) * 3 * sigma_c(i,j);
sigma_v = ((alpha_T+A(i,j))/alpha_T) * 3 * sigma_c(i,j);
a = exp( ...
-( (dx.*cos(phi(i,j)) + dy.*sin(phi(i,j))) ./ sigma_u ).^2 ...
-( (-dx.*sin(phi(i,j)) + dy.*cos(phi(i,j))) ./ sigma_v ).^2 ...
);%Structure-adaptive applicability function
% a(isinf(a))=1;
% a(isnan(a))=0;
%a = a > 0.6;
%basis functions
B = zeros(length(dx), 6);
B(:,1) = ones(length(dx),1);
B(:,2) = x_temp - i; %x
B(:,3) = y_temp - j; %y
B(:,4) = dx.^2; %x^2
B(:,5) = B(:,2).*B(:,3); %xy
B(:,6) = dy.^2; %y^2
F = values_temp(coord_temp);
C = certainty_temp(coord_temp);
W = diag(C.*a);
% -- Optimization of the built-in pinv function --
% t = pinv(B' * W * B) * B' * W * F;
[u,s,v]=svd(B'*W*B);
%invert singular values only if they're greater than an epsylon
if(s(1,1)>1e-5)
s(1,1)=1./s(1,1);
if(s(2,2)>1e-5)
s(2,2)=1./s(2,2);
if(s(3,3)>1e-5)
s(3,3)=1./s(3,3);
if(s(4,4)>1e-5)
s(4,4)=1./s(4,4);
if(s(5,5)>1e-5)
s(5,5)=1./s(5,5);
if(s(6,6)>1e-5)
s(6,6)=1./s(6,6);
end
end
end
end
end
end
pin = u*s*v';
t = pin * B' * W * F;
% -- End of pinv optimization -------
rec(i,j) = t(1);
%[i j]
%dbstop if i=34 && j==55;
end %if
end %j
end %i
end %if
% -- End of Reconstruction process
% -- End of Structure-Adaptive Normalized Convolution
%% Final adjustments
if(outputFrames == false)
Frames = [];
end