-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_video_simulate_interactive.m
431 lines (302 loc) · 9.82 KB
/
demo_video_simulate_interactive.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
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
function demo_transform_objects
% warping the lung lobes
% translation
I = double(imread('data/01_Fixed_y161ud.png'));
Ilabel = double(imread('data/01_Fixed_y161ud_label.png'));
Ifinal = double(imread('data/01_Fixed_y161ud_final_warped.png'));
figure(16); clf;
imagesc(Ilabel); axis image;
cnt = 0;
for manual_ratio = 0:0.1:1
cnt = disp_transform_lung(I, Ilabel, Ifinal, manual_ratio, 1, cnt);
end;
for manual_ratio = 0.1:0.1:1
cnt = disp_transform_lung(I, Ilabel, Ifinal, manual_ratio, 2, cnt);
end;
for manual_ratio = 0.25:0.25:1
cnt = disp_transform_lung(I, Ilabel, Ifinal, manual_ratio, 3, cnt);
end;
function cnt = disp_transform_lung(I, Ilabel, Ifinal, manual_ratio, step_id, cnt)
% print -dpng -f16 'data/01_Fixed_y161ud_lable_colored.png';
leftB = 1;
rightB = size(I, 2);
bottomB = 1;
topB = size(I, 1);
% define the boundary boxes
% [dim1a, dim1b; dim2a, dim2b];
g.boundary.box = [leftB, rightB; bottomB, topB];
g.boundary.s = 10;
g.Ilabel = Ilabel;
[X, Y] = meshgrid(leftB:1:rightB, bottomB:1:topB);
% test on the Gaussian smoothing for velocity field
% this is used for decreasing from boundaries
g.vfield_smooth_sigma = 0.1;
% g.s2 is to combine weight
% g.s2 = 1;
switch step_id
case 1,
g = get_control_point_and_transform_multibeam_lobes(g, manual_ratio);
case 2,
g = get_control_point_and_transform_multibeam_lobes_step2(g, manual_ratio);
case 3,
g = get_control_point_and_transform_multibeam_lobes_step3(g, manual_ratio);
end;
nb_cps = g.nb_cps;
cps = g.cps;
dim = g.dim;
% two collision boundaries
% r1 = intra-beam collision, r1 \approx 1
% r2 = inter-beam collision, r2 can be fairly big, like 10^2 or 20^2
% r is the collision boundary
r1 = 1;
g.r1 = 1;
r2 = 225;
g.r2 = r2;
% dv is the difference of the velocity when conflicted
g.dv = 1;
% solve iniitial ode on p and q
% pqhat0 = [p; q];
cpshat0 = reshape(cps, [nb_cps*dim, 1]);
tlist = 0:0.01:1;
tlist = 0:0.05:1;
[cpslist, vcpslist] = predefine_trajectory_multibeam(g, tlist);
g.nb_T = length(tlist);
clist = collision_detection_conflicted_velocity_multibeam(g, cpslist, vcpslist);
clist
xfield_0 = cat(3, X, Y);
yfield_current = xfield_0;
cpspiecelist = zeros(length(clist), 2, nb_cps);
for ii = 1:nb_cps
cpspiecelist(1, :, ii) = cps(:, ii)';
end;
% precompute the decreasing ratio
% compute the alpha decresing ratio from trajectory
g.h = 20; % h is the sampling radius of the points in the mask
g.sigma2 = 20; % local decreasing sigma
g.sigma1 = 1; % the sigma to combine affine fields, need to be smaller than collision radius
% descreasing weight map from trajectory
maskall = scatter_binary_trajectory_nearest_neighbor(g.boundary.box, cpslist);
logalpha = get_log_weight_using_distance_transform_tablegaussian(maskall, g.h, g.sigma2);
alpha = exp(logalpha);
figure(20); clf;
subplot(2,2,3);
% figure(13); clf;
imagesc(I); axis image; colormap gray;
for ii = 1: length(clist)-1
ind_t1 = clist(ii);
ind_t2 = clist(ii+1);
% create velocity field using RBF decreasing function
vfield = get_stationary_vield_copy_paste_decreasing(g, xfield_0, cpslist(ind_t1:ind_t2, :, :), alpha);
q=2;
yfield_delta = exp_mapping(vfield, X, Y, tlist(ind_t2)-tlist(ind_t1), 10);
yfield_current = compose_phi(yfield_current, yfield_delta, X, Y);
for k = 1:nb_cps
idx_p = find(X==cps(1,k) & Y==cps(2,k));
y1 = yfield_current(:,:,1);
y2 = yfield_current(:,:,2);
cpspiecelist(ii+1, :, k) = [y1(idx_p), y2(idx_p)];
end;
end;
pad=0;
fil=15;
% plot trajectory of control points (cps) using ode solution
hold on;
clrs='gbr';
for ii = 1:nb_cps
% plot(squeeze(cpslist(:, 1, ii)), squeeze(cpslist(:, 2, ii)), ['-', clrs(mod(ii, length(clrs))+1), '*']);
% plot(squeeze(cpslist([1, end], 1, ii)), squeeze(cpslist([1, end], 2, ii)), [clrs(mod(ii, length(clrs))+1), '*'], 'MarkerSize', 10);
end;
hold off;
% meshplot(X(pad*fil:fil:end-pad*fil, pad*fil:fil:end-pad*fil), Y(pad*fil:fil:end-pad*fil, pad*fil:fil:end-pad*fil), 'Color', 'g');
% meshplot(yfield_current(pad*fil:fil:end-pad*fil, pad*fil:fil:end-pad*fil, 1), yfield_current(pad*fil:fil:end-pad*fil, pad*fil:fil:end-pad*fil, 2), 'Color', 'b');
% meshplot(X(pad*fil+1:fil:end-pad*fil, pad*fil+1:fil:end-pad*fil), Y(pad*fil+1:fil:end-pad*fil, pad*fil+1:fil:end-pad*fil), 'Color', 'g');
meshplot(yfield_current(pad*fil+1:fil:end-pad*fil, pad*fil+1:fil:end-pad*fil, 1), yfield_current(pad*fil+1:fil:end-pad*fil, pad*fil+1:fil:end-pad*fil, 2), 'Color', 'g', 'LineWidth', 1);
axis([leftB, rightB, bottomB, topB]);
axis image;
axis ij;
axis off;
title('Deformation Field on Moving Image');
Ilabelwarped = griddata(yfield_current(:, :, 1), yfield_current(:, :, 2), Ilabel, X, Y, 'nearest');
subplot(2,2,1);
% figure(12); clf;
IlabelRGB = uint8(ind2rgb(Ilabelwarped+1, [0 0 0; 1 0 0; 0 1 0]) * 255);
h =imshow(IlabelRGB);
hold on;
h = imshow(Ifinal);
hold off;
set(h, 'AlphaData', 255-Ifinal);
title('Interactive Mask on Fix Image');
subplot(2,2,2);
% figure(14); clf;
imagesc(Ifinal); axis image; colormap gray;
title('Desired Fixed Image');
axis off;
Iwarped = griddata(yfield_current(:, :, 1), yfield_current(:, :, 2), I, X, Y);
subplot(2,2,4);
%figure(15); clf;
imagesc(Iwarped); axis image; colormap gray;
title('Interactive Warped Moving Image');
axis off;
% imwrite(uint8(Iwarped), 'data/01_Fixed_y161ud_final_warped.png');
cnt = cnt + 1;
imgname = sprintf('data/interactive_%05d.png', cnt);
print('-dpng', '-f20', imgname);
function g = get_control_point_and_transform_multibeam_lobes(g, manual_ratio)
% control point
% column vector
% cps = [-10 10 0 0;
% 0 0 10 -10 ];
% cps = [-10 0;
% 0 30];
% s is the sigma to computer affine velocity decreasing
% s can be removed in future implementation
s = 1;
dim = 2;
Ilabel = double(imread('data/01_Fixed_y161ud_label.png'));
% find control points inside each label
imgw = size(Ilabel, 2);
imgh = size(Ilabel, 1);
[X, Y] = meshgrid(1:20:imgw, 1:20:imgh);
ind_all = sub2ind(size(Ilabel), Y(:), X(:));
X1 = X(Ilabel(ind_all)==1);
Y1 = Y(Ilabel(ind_all)==1);
cps1 = [X1(:)'; Y1(:)'];
ind1 = ones(1, size(cps1, 2));
cps = cps1;
ind = ind1;
X1 = X(Ilabel(ind_all)==2);
Y1 = Y(Ilabel(ind_all)==2);
cps1 = [X1(:)'; Y1(:)']; % dim * nb_pts
ind1 = 2 * ones(1, size(cps1, 2));
cps = [cps, cps1];
ind = [ind, ind1];
g.cps = cps;
g.ind = ind;
nb_cps = size(cps, 2);
% define the affine transform for two masks
g.aff = cell(0);
% [A, t] = get_A_and_t(-15, [94; 141], [10; -10], [0.8,0.8])
[A, t] = get_A_and_t(0, [94; 141], [0; 0], [1, 1])
[L, v] = get_Lv_from_At(A, t);
g.aff{end+1}.A = A;
g.aff{end}.t = t;
g.aff{end}.L = L;
g.aff{end}.v = v;
g.aff{end}.s = s;
[A, t] = get_A_and_t(0, [332;142], [-60 * manual_ratio; -20], [1, 1])
[L, v] = get_Lv_from_At(A, t);
g.aff{end+1}.A = A;
g.aff{end}.t = t;
g.aff{end}.L = L;
g.aff{end}.v = v;
g.aff{end}.s = s;
g.dim = dim;
g.nb_cps = nb_cps;
g.cps = cps;
g.Ilabel = Ilabel;
function g = get_control_point_and_transform_multibeam_lobes_step2(g, manual_ratio)
% control point
% column vector
% cps = [-10 10 0 0;
% 0 0 10 -10 ];
% cps = [-10 0;
% 0 30];
% s is the sigma to computer affine velocity decreasing
% s can be removed in future implementation
s = 1;
dim = 2;
Ilabel = double(imread('data/01_Fixed_y161ud_label.png'));
% find control points inside each label
imgw = size(Ilabel, 2);
imgh = size(Ilabel, 1);
[X, Y] = meshgrid(1:20:imgw, 1:20:imgh);
ind_all = sub2ind(size(Ilabel), Y(:), X(:));
X1 = X(Ilabel(ind_all)==1);
Y1 = Y(Ilabel(ind_all)==1);
cps1 = [X1(:)'; Y1(:)'];
ind1 = ones(1, size(cps1, 2));
cps = cps1;
ind = ind1;
X1 = X(Ilabel(ind_all)==2);
Y1 = Y(Ilabel(ind_all)==2);
cps1 = [X1(:)'; Y1(:)']; % dim * nb_pts
ind1 = 2 * ones(1, size(cps1, 2));
cps = [cps, cps1];
ind = [ind, ind1];
g.cps = cps;
g.ind = ind;
nb_cps = size(cps, 2);
% define the affine transform for two masks
g.aff = cell(0);
% [A, t] = get_A_and_t(-15, [94; 141], [10; -10], [0.8,0.8])
[A, t] = get_A_and_t(-30 * manual_ratio, [94; 141], [0; 0], [1, 1])
[L, v] = get_Lv_from_At(A, t);
g.aff{end+1}.A = A;
g.aff{end}.t = t;
g.aff{end}.L = L;
g.aff{end}.v = v;
g.aff{end}.s = s;
[A, t] = get_A_and_t(0, [332;142], [-60 * 1; -20], [1, 1])
[L, v] = get_Lv_from_At(A, t);
g.aff{end+1}.A = A;
g.aff{end}.t = t;
g.aff{end}.L = L;
g.aff{end}.v = v;
g.aff{end}.s = s;
g.dim = dim;
g.nb_cps = nb_cps;
g.cps = cps;
g.Ilabel = Ilabel;
function g = get_control_point_and_transform_multibeam_lobes_step3(g, manual_ratio)
% control point
% column vector
% cps = [-10 10 0 0;
% 0 0 10 -10 ];
% cps = [-10 0;
% 0 30];
% s is the sigma to computer affine velocity decreasing
% s can be removed in future implementation
s = 1;
dim = 2;
Ilabel = g.Ilabel; % double(imread('data/01_Fixed_y161ud_label.png'));
% find control points inside each label
imgw = size(Ilabel, 2);
imgh = size(Ilabel, 1);
[X, Y] = meshgrid(1:20:imgw, 1:20:imgh);
ind_all = sub2ind(size(Ilabel), Y(:), X(:));
X1 = X(Ilabel(ind_all)==1);
Y1 = Y(Ilabel(ind_all)==1);
cps1 = [X1(:)'; Y1(:)'];
ind1 = ones(1, size(cps1, 2));
cps = cps1;
ind = ind1;
X1 = X(Ilabel(ind_all)==2);
Y1 = Y(Ilabel(ind_all)==2);
cps1 = [X1(:)'; Y1(:)']; % dim * nb_pts
ind1 = 2 * ones(1, size(cps1, 2));
cps = [cps, cps1];
ind = [ind, ind1];
g.cps = cps;
g.ind = ind;
nb_cps = size(cps, 2);
% define the affine transform for two masks
g.aff = cell(0);
% [A, t] = get_A_and_t(-15, [94; 141], [10; -10], [0.8,0.8])
[A, t] = get_A_and_t(-30, [94; 141], [0; 0], [1 - 0.2*manual_ratio, 1 - 0.2*manual_ratio])
[L, v] = get_Lv_from_At(A, t);
g.aff{end+1}.A = A;
g.aff{end}.t = t;
g.aff{end}.L = L;
g.aff{end}.v = v;
g.aff{end}.s = s;
[A, t] = get_A_and_t(0, [332;142], [-60 * 1; -20], [1, 1])
[L, v] = get_Lv_from_At(A, t);
g.aff{end+1}.A = A;
g.aff{end}.t = t;
g.aff{end}.L = L;
g.aff{end}.v = v;
g.aff{end}.s = s;
g.dim = dim;
g.nb_cps = nb_cps;
g.cps = cps;
g.Ilabel = Ilabel;