-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimg_utils.py
581 lines (500 loc) · 32.3 KB
/
img_utils.py
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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
#! /usr/bin/env python
# -*- coding: utf-8 -*-
"""
Image deformation using moving least squares.
* Affine deformation
* Affine inverse deformation
* Similarity deformation
* Similarity inverse deformation
* Rigid deformation
* Rigid inverse deformation (* This algorithm is approximate, because the inverse formula
of the rigid deformation is not easy to infer)
For more details please reference the documentation:
Moving-Least-Squares/doc/Image Deformation.pdf
or the original paper:
Image deformation using moving least squares
Schaefer, Mcphail, Warren.
Note:
In the original paper, the author missed the weight w_j in formular (5).
In addition, all the formulars in section 2.1 miss the w_j.
And I have corrected this point in my documentation.
@author: Jarvis ZHANG
@date: 2017/8/8
@editor: VS Code
"""
import numpy as np
from skimage.transform import rescale
np.seterr(divide='ignore', invalid='ignore')
def mls_affine_deformation_1pt(p, q, v, alpha=1):
''' Calculate the affine deformation of one point.
This function is used to test the algorithm.
'''
ctrls = p.shape[0]
np.seterr(divide='ignore')
w = 1.0 / np.sum((p - v) ** 2, axis=1) ** alpha
w[w == np.inf] = 2**31-1
pstar = np.sum(p.T * w, axis=1) / np.sum(w)
qstar = np.sum(q.T * w, axis=1) / np.sum(w)
phat = p - pstar
qhat = q - qstar
reshaped_phat1 = phat.reshape(ctrls, 2, 1)
reshaped_phat2 = phat.reshape(ctrls, 1, 2)
reshaped_w = w.reshape(ctrls, 1, 1)
pTwp = np.sum(reshaped_phat1 * reshaped_w * reshaped_phat2, axis=0)
try:
inv_pTwp = np.linalg.inv(pTwp)
except np.linalg.linalg.LinAlgError:
if np.linalg.det(pTwp) < 1e-8:
new_v = v + qstar - pstar
return new_v
else:
raise
mul_left = v - pstar
mul_right = np.sum(reshaped_phat1 * reshaped_w * qhat[:, np.newaxis, :], axis=0)
new_v = np.dot(np.dot(mul_left, inv_pTwp), mul_right) + qstar
return new_v
def mls_affine_deformation(image, p, q, alpha=1.0, density=1.0):
''' Affine deformation
### Params:
* image - ndarray: original image
* p - ndarray: an array with size [n, 2], original control points
* q - ndarray: an array with size [n, 2], final control points
* alpha - float: parameter used by weights
* density - float: density of the grids
### Return:
A deformed image.
'''
height = image.shape[0]
width = image.shape[1]
# Change (x, y) to (row, col)
q = q[:, [1, 0]]
p = p[:, [1, 0]]
# Make grids on the original image
gridX = np.linspace(0, width, num=int(width*density), endpoint=False)
gridY = np.linspace(0, height, num=int(height*density), endpoint=False)
vy, vx = np.meshgrid(gridX, gridY)
grow = vx.shape[0] # grid rows
gcol = vx.shape[1] # grid cols
ctrls = p.shape[0] # control points
# Precompute
reshaped_p = p.reshape(ctrls, 2, 1, 1) # [ctrls, 2, 1, 1]
reshaped_v = np.vstack((vx.reshape(1, grow, gcol), vy.reshape(1, grow, gcol))) # [2, grow, gcol]
w = 1.0 / np.sum((reshaped_p - reshaped_v) ** 2, axis=1)**alpha # [ctrls, grow, gcol]
w[w == np.inf] = 2**31 - 1
pstar = np.sum(w * reshaped_p.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
phat = reshaped_p - pstar # [ctrls, 2, grow, gcol]
reshaped_phat1 = phat.reshape(ctrls, 2, 1, grow, gcol) # [ctrls, 2, 1, grow, gcol]
reshaped_phat2 = phat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
reshaped_w = w.reshape(ctrls, 1, 1, grow, gcol) # [ctrls, 1, 1, grow, gcol]
pTwp = np.sum(reshaped_phat1 * reshaped_w * reshaped_phat2, axis=0) # [2, 2, grow, gcol]
try:
inv_pTwp = np.linalg.inv(pTwp.transpose(2, 3, 0, 1)) # [grow, gcol, 2, 2]
flag = False
except np.linalg.linalg.LinAlgError:
flag = True
det = np.linalg.det(pTwp.transpose(2, 3, 0, 1)) # [grow, gcol]
det[det < 1e-8] = np.inf
reshaped_det = det.reshape(1, 1, grow, gcol) # [1, 1, grow, gcol]
adjoint = pTwp[[[1, 0], [1, 0]], [[1, 1], [0, 0]], :, :] # [2, 2, grow, gcol]
adjoint[[0, 1], [1, 0], :, :] = -adjoint[[0, 1], [1, 0], :, :] # [2, 2, grow, gcol]
inv_pTwp = (adjoint / reshaped_det).transpose(2, 3, 0, 1) # [grow, gcol, 2, 2]
mul_left = reshaped_v - pstar # [2, grow, gcol]
reshaped_mul_left = mul_left.reshape(1, 2, grow, gcol).transpose(2, 3, 0, 1) # [grow, gcol, 1, 2]
mul_right = reshaped_w * reshaped_phat1 # [ctrls, 2, 1, grow, gcol]
reshaped_mul_right =mul_right.transpose(0, 3, 4, 1, 2) # [ctrls, grow, gcol, 2, 1]
A = np.matmul(np.matmul(reshaped_mul_left, inv_pTwp), reshaped_mul_right) # [ctrls, grow, gcol, 1, 1]
reshaped_A = A.reshape(ctrls, 1, grow, gcol) # [ctrls, 1, grow, gcol]
# Calculate q
reshaped_q = q.reshape((ctrls, 2, 1, 1)) # [ctrls, 2, 1, 1]
qstar = np.sum(w * reshaped_q.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
qhat = reshaped_q - qstar # [ctrls, 2, grow, gcol]
# Get final image transfomer -- 3-D array
transformers = np.sum(reshaped_A * qhat, axis=0) + qstar # [2, grow, gcol]
# Correct the points where pTwp is singular
if flag:
blidx = det == np.inf # bool index
transformers[0][blidx] = vx[blidx] + qstar[0][blidx] - pstar[0][blidx]
transformers[1][blidx] = vy[blidx] + qstar[1][blidx] - pstar[1][blidx]
# Removed the points outside the border
transformers[transformers < 0] = 0
transformers[0][transformers[0] > height - 1] = 0
transformers[1][transformers[1] > width - 1] = 0
# Mapping original image
transformed_image = np.ones_like(image) * 255
new_gridY, new_gridX = np.meshgrid((np.arange(gcol) / density).astype(np.int16),
(np.arange(grow) / density).astype(np.int16))
transformed_image[tuple(transformers.astype(np.int16))] = image[new_gridX, new_gridY] # [grow, gcol]
return transformed_image
def mls_affine_deformation_inv(image, p, q, alpha=1.0, density=1.0):
''' Affine inverse deformation
### Params:
* image - ndarray: original image
* p - ndarray: an array with size [n, 2], original control points
* q - ndarray: an array with size [n, 2], final control points
* alpha - float: parameter used by weights
* density - float: density of the grids
### Return:
A deformed image.
'''
height = image.shape[0]
width = image.shape[1]
# Change (x, y) to (row, col)
q = q[:, [1, 0]]
p = p[:, [1, 0]]
# Make grids on the original image
gridX = np.linspace(0, width, num=int(width*density), endpoint=False)
gridY = np.linspace(0, height, num=int(height*density), endpoint=False)
vy, vx = np.meshgrid(gridX, gridY)
grow = vx.shape[0] # grid rows
gcol = vx.shape[1] # grid cols
ctrls = p.shape[0] # control points
# Compute
reshaped_p = p.reshape(ctrls, 2, 1, 1) # [ctrls, 2, 1, 1]
reshaped_q = q.reshape((ctrls, 2, 1, 1)) # [ctrls, 2, 1, 1]
reshaped_v = np.vstack((vx.reshape(1, grow, gcol), vy.reshape(1, grow, gcol))) # [2, grow, gcol]
w = 1.0 / np.sum((reshaped_p - reshaped_v) ** 2, axis=1)**alpha # [ctrls, grow, gcol]
w[w == np.inf] = 2**31 - 1
pstar = np.sum(w * reshaped_p.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
phat = reshaped_p - pstar # [ctrls, 2, grow, gcol]
qstar = np.sum(w * reshaped_q.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
qhat = reshaped_q - qstar # [ctrls, 2, grow, gcol]
reshaped_phat = phat.reshape(ctrls, 2, 1, grow, gcol) # [ctrls, 2, 1, grow, gcol]
reshaped_phat2 = phat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 2, 1, grow, gcol]
reshaped_qhat = qhat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
reshaped_w = w.reshape(ctrls, 1, 1, grow, gcol) # [ctrls, 1, 1, grow, gcol]
pTwq = np.sum(reshaped_phat * reshaped_w * reshaped_qhat, axis=0) # [2, 2, grow, gcol]
try:
inv_pTwq = np.linalg.inv(pTwq.transpose(2, 3, 0, 1)) # [grow, gcol, 2, 2]
flag = False
except np.linalg.linalg.LinAlgError:
flag = True
det = np.linalg.det(pTwq.transpose(2, 3, 0, 1)) # [grow, gcol]
det[det < 1e-8] = np.inf
reshaped_det = det.reshape(1, 1, grow, gcol) # [1, 1, grow, gcol]
adjoint = pTwq[[[1, 0], [1, 0]], [[1, 1], [0, 0]], :, :] # [2, 2, grow, gcol]
adjoint[[0, 1], [1, 0], :, :] = -adjoint[[0, 1], [1, 0], :, :] # [2, 2, grow, gcol]
inv_pTwq = (adjoint / reshaped_det).transpose(2, 3, 0, 1) # [grow, gcol, 2, 2]
mul_left = reshaped_v - qstar # [2, grow, gcol]
reshaped_mul_left = mul_left.reshape(1, 2, grow, gcol).transpose(2, 3, 0, 1) # [grow, gcol, 1, 2]
mul_right = np.sum(reshaped_phat * reshaped_w * reshaped_phat2, axis=0) # [2, 2, grow, gcol]
reshaped_mul_right =mul_right.transpose(2, 3, 0, 1) # [grow, gcol, 2, 2]
temp = np.matmul(np.matmul(reshaped_mul_left, inv_pTwq), reshaped_mul_right) # [grow, gcol, 1, 2]
reshaped_temp = temp.reshape(grow, gcol, 2).transpose(2, 0, 1) # [2, grow, gcol]
# Get final image transfomer -- 3-D array
transformers = reshaped_temp + pstar # [2, grow, gcol]
# Correct the points where pTwp is singular
if flag:
blidx = det == np.inf # bool index
transformers[0][blidx] = vx[blidx] + qstar[0][blidx] - pstar[0][blidx]
transformers[1][blidx] = vy[blidx] + qstar[1][blidx] - pstar[1][blidx]
# Removed the points outside the border
transformers[transformers < 0] = 0
transformers[0][transformers[0] > height - 1] = 0
transformers[1][transformers[1] > width - 1] = 0
# Mapping original image
transformed_image = image[tuple(transformers.astype(np.int16))] # [grow, gcol]
# Rescale image
transformed_image = rescale(transformed_image, scale=1.0 / density, mode='reflect')
return transformed_image
def mls_similarity_deformation(image, p, q, alpha=1.0, density=1.0):
''' Similarity deformation
### Params:
* image - ndarray: original image
* p - ndarray: an array with size [n, 2], original control points
* q - ndarray: an array with size [n, 2], final control points
* alpha - float: parameter used by weights
* density - float: density of the grids
### Return:
A deformed image.
'''
height = image.shape[0]
width = image.shape[1]
# Change (x, y) to (row, col)
q = q[:, [1, 0]]
p = p[:, [1, 0]]
# Make grids on the original image
gridX = np.linspace(0, width, num=int(width*density), endpoint=False)
gridY = np.linspace(0, height, num=int(height*density), endpoint=False)
vy, vx = np.meshgrid(gridX, gridY)
grow = vx.shape[0] # grid rows
gcol = vx.shape[1] # grid cols
ctrls = p.shape[0] # control points
# Compute
reshaped_p = p.reshape(ctrls, 2, 1, 1) # [ctrls, 2, 1, 1]
reshaped_v = np.vstack((vx.reshape(1, grow, gcol), vy.reshape(1, grow, gcol))) # [2, grow, gcol]
w = 1.0 / np.sum((reshaped_p - reshaped_v) ** 2, axis=1)**alpha # [ctrls, grow, gcol]
sum_w = np.sum(w, axis=0) # [grow, gcol]
pstar = np.sum(w * reshaped_p.transpose(1, 0, 2, 3), axis=1) / sum_w # [2, grow, gcol]
phat = reshaped_p - pstar # [ctrls, 2, grow, gcol]
reshaped_phat1 = phat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
reshaped_phat2 = phat.reshape(ctrls, 2, 1, grow, gcol) # [ctrls, 2, 1, grow, gcol]
reshaped_w = w.reshape(ctrls, 1, 1, grow, gcol) # [ctrls, 1, 1, grow, gcol]
mu = np.sum(np.matmul(reshaped_w.transpose(0, 3, 4, 1, 2) *
reshaped_phat1.transpose(0, 3, 4, 1, 2),
reshaped_phat2.transpose(0, 3, 4, 1, 2)), axis=0) # [grow, gcol, 1, 1]
reshaped_mu = mu.reshape(1, grow, gcol) # [1, grow, gcol]
neg_phat_verti = phat[:, [1, 0],...] # [ctrls, 2, grow, gcol]
neg_phat_verti[:, 1,...] = -neg_phat_verti[:, 1,...]
reshaped_neg_phat_verti = neg_phat_verti.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
mul_left = np.concatenate((reshaped_phat1, reshaped_neg_phat_verti), axis=1) # [ctrls, 2, 2, grow, gcol]
vpstar = reshaped_v - pstar # [2, grow, gcol]
reshaped_vpstar = vpstar.reshape(2, 1, grow, gcol) # [2, 1, grow, gcol]
neg_vpstar_verti = vpstar[[1, 0],...] # [2, grow, gcol]
neg_vpstar_verti[1,...] = -neg_vpstar_verti[1,...]
reshaped_neg_vpstar_verti = neg_vpstar_verti.reshape(2, 1, grow, gcol) # [2, 1, grow, gcol]
mul_right = np.concatenate((reshaped_vpstar, reshaped_neg_vpstar_verti), axis=1) # [2, 2, grow, gcol]
reshaped_mul_right = mul_right.reshape(1, 2, 2, grow, gcol) # [1, 2, 2, grow, gcol]
A = np.matmul((reshaped_w * mul_left).transpose(0, 3, 4, 1, 2),
reshaped_mul_right.transpose(0, 3, 4, 1, 2)) # [ctrls, grow, gcol, 2, 2]
# Calculate q
reshaped_q = q.reshape((ctrls, 2, 1, 1)) # [ctrls, 2, 1, 1]
qstar = np.sum(w * reshaped_q.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
qhat = reshaped_q - qstar # [ctrls, 2, grow, gcol]
reshaped_qhat = qhat.reshape(ctrls, 1, 2, grow, gcol).transpose(0, 3, 4, 1, 2) # [ctrls, grow, gcol, 1, 2]
# Get final image transfomer -- 3-D array
temp = np.sum(np.matmul(reshaped_qhat, A), axis=0).transpose(2, 3, 0, 1) # [1, 2, grow, gcol]
reshaped_temp = temp.reshape(2, grow, gcol) # [2, grow, gcol]
transformers = reshaped_temp / reshaped_mu + qstar # [2, grow, gcol]
# Removed the points outside the border
transformers[transformers < 0] = 0
transformers[0][transformers[0] > height - 1] = 0
transformers[1][transformers[1] > width - 1] = 0
# Mapping original image
transformed_image = np.ones_like(image) * 255
new_gridY, new_gridX = np.meshgrid((np.arange(gcol) / density).astype(np.int16),
(np.arange(grow) / density).astype(np.int16))
transformed_image[tuple(transformers.astype(np.int16))] = image[new_gridX, new_gridY] # [grow, gcol]
return transformed_image
def mls_similarity_deformation_inv(image, p, q, alpha=1.0, density=1.0):
''' Similarity inverse deformation
### Params:
* image - ndarray: original image
* p - ndarray: an array with size [n, 2], original control points
* q - ndarray: an array with size [n, 2], final control points
* alpha - float: parameter used by weights
* density - float: density of the grids
### Return:
A deformed image.
'''
height = image.shape[0]
width = image.shape[1]
# Change (x, y) to (row, col)
q = q[:, [1, 0]]
p = p[:, [1, 0]]
# Make grids on the original image
gridX = np.linspace(0, width, num=int(width*density), endpoint=False)
gridY = np.linspace(0, height, num=int(height*density), endpoint=False)
vy, vx = np.meshgrid(gridX, gridY)
grow = vx.shape[0] # grid rows
gcol = vx.shape[1] # grid cols
ctrls = p.shape[0] # control points
# Compute
reshaped_p = p.reshape(ctrls, 2, 1, 1) # [ctrls, 2, 1, 1]
reshaped_q = q.reshape((ctrls, 2, 1, 1)) # [ctrls, 2, 1, 1]
reshaped_v = np.vstack((vx.reshape(1, grow, gcol), vy.reshape(1, grow, gcol))) # [2, grow, gcol]
w = 1.0 / np.sum((reshaped_p - reshaped_v) ** 2, axis=1)**alpha # [ctrls, grow, gcol]
w[w == np.inf] = 2**31 - 1
pstar = np.sum(w * reshaped_p.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
phat = reshaped_p - pstar # [ctrls, 2, grow, gcol]
qstar = np.sum(w * reshaped_q.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
qhat = reshaped_q - qstar # [ctrls, 2, grow, gcol]
reshaped_phat1 = phat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
reshaped_phat2 = phat.reshape(ctrls, 2, 1, grow, gcol) # [ctrls, 2, 1, grow, gcol]
reshaped_qhat = qhat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
reshaped_w = w.reshape(ctrls, 1, 1, grow, gcol) # [ctrls, 1, 1, grow, gcol]
mu = np.sum(np.matmul(reshaped_w.transpose(0, 3, 4, 1, 2) *
reshaped_phat1.transpose(0, 3, 4, 1, 2),
reshaped_phat2.transpose(0, 3, 4, 1, 2)), axis=0) # [grow, gcol, 1, 1]
reshaped_mu = mu.reshape(1, grow, gcol) # [1, grow, gcol]
neg_phat_verti = phat[:, [1, 0],...] # [ctrls, 2, grow, gcol]
neg_phat_verti[:, 1,...] = -neg_phat_verti[:, 1,...]
reshaped_neg_phat_verti = neg_phat_verti.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
mul_right = np.concatenate((reshaped_phat1, reshaped_neg_phat_verti), axis=1) # [ctrls, 2, 2, grow, gcol]
mul_left = reshaped_qhat * reshaped_w # [ctrls, 1, 2, grow, gcol]
Delta = np.sum(np.matmul(mul_left.transpose(0, 3, 4, 1, 2),
mul_right.transpose(0, 3, 4, 1, 2)),
axis=0).transpose(0, 1, 3, 2) # [grow, gcol, 2, 1]
Delta_verti = Delta[...,[1, 0],:] # [grow, gcol, 2, 1]
Delta_verti[...,0,:] = -Delta_verti[...,0,:]
B = np.concatenate((Delta, Delta_verti), axis=3) # [grow, gcol, 2, 2]
try:
inv_B = np.linalg.inv(B) # [grow, gcol, 2, 2]
flag = False
except np.linalg.linalg.LinAlgError:
flag = True
det = np.linalg.det(B) # [grow, gcol]
det[det < 1e-8] = np.inf
reshaped_det = det.reshape(grow, gcol, 1, 1) # [grow, gcol, 1, 1]
adjoint = B[:,:,[[1, 0], [1, 0]], [[1, 1], [0, 0]]] # [grow, gcol, 2, 2]
adjoint[:,:,[0, 1], [1, 0]] = -adjoint[:,:,[0, 1], [1, 0]] # [grow, gcol, 2, 2]
inv_B = (adjoint / reshaped_det).transpose(2, 3, 0, 1) # [2, 2, grow, gcol]
v_minus_qstar_mul_mu = (reshaped_v - qstar) * reshaped_mu # [2, grow, gcol]
# Get final image transfomer -- 3-D array
reshaped_v_minus_qstar_mul_mu = v_minus_qstar_mul_mu.reshape(1, 2, grow, gcol) # [1, 2, grow, gcol]
transformers = np.matmul(reshaped_v_minus_qstar_mul_mu.transpose(2, 3, 0, 1),
inv_B).reshape(grow, gcol, 2).transpose(2, 0, 1) + pstar # [2, grow, gcol]
# Correct the points where pTwp is singular
if flag:
blidx = det == np.inf # bool index
transformers[0][blidx] = vx[blidx] + qstar[0][blidx] - pstar[0][blidx]
transformers[1][blidx] = vy[blidx] + qstar[1][blidx] - pstar[1][blidx]
# Removed the points outside the border
transformers[transformers < 0] = 0
transformers[0][transformers[0] > height - 1] = 0
transformers[1][transformers[1] > width - 1] = 0
# Mapping original image
transformed_image = image[tuple(transformers.astype(np.int16))] # [grow, gcol]
# Rescale image
transformed_image = rescale(transformed_image, scale=1.0 / density, mode='reflect')
return transformed_image
def mls_rigid_deformation(image, p, q, alpha=1.0, density=1.0):
''' Rigid deformation
### Params:
* image - ndarray: original image
* p - ndarray: an array with size [n, 2], original control points
* q - ndarray: an array with size [n, 2], final control points
* alpha - float: parameter used by weights
* density - float: density of the grids
### Return:
A deformed image.
'''
height = image.shape[0]
width = image.shape[1]
# Change (x, y) to (row, col)
q = q[:, [1, 0]]
p = p[:, [1, 0]]
# Make grids on the original image
gridX = np.linspace(0, width, num=int(width*density), endpoint=False)
gridY = np.linspace(0, height, num=int(height*density), endpoint=False)
vy, vx = np.meshgrid(gridX, gridY)
grow = vx.shape[0] # grid rows
gcol = vx.shape[1] # grid cols
ctrls = p.shape[0] # control points
# Compute
reshaped_p = p.reshape(ctrls, 2, 1, 1) # [ctrls, 2, 1, 1]
reshaped_v = np.vstack((vx.reshape(1, grow, gcol), vy.reshape(1, grow, gcol))) # [2, grow, gcol]
w = 1.0 / np.sum((reshaped_p - reshaped_v) ** 2, axis=1)**alpha # [ctrls, grow, gcol]
sum_w = np.sum(w, axis=0) # [grow, gcol]
pstar = np.sum(w * reshaped_p.transpose(1, 0, 2, 3), axis=1) / sum_w # [2, grow, gcol]
phat = reshaped_p - pstar # [ctrls, 2, grow, gcol]
reshaped_phat = phat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
reshaped_w = w.reshape(ctrls, 1, 1, grow, gcol) # [ctrls, 1, 1, grow, gcol]
neg_phat_verti = phat[:, [1, 0],...] # [ctrls, 2, grow, gcol]
neg_phat_verti[:, 1,...] = -neg_phat_verti[:, 1,...]
reshaped_neg_phat_verti = neg_phat_verti.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
mul_left = np.concatenate((reshaped_phat, reshaped_neg_phat_verti), axis=1) # [ctrls, 2, 2, grow, gcol]
vpstar = reshaped_v - pstar # [2, grow, gcol]
reshaped_vpstar = vpstar.reshape(2, 1, grow, gcol) # [2, 1, grow, gcol]
neg_vpstar_verti = vpstar[[1, 0],...] # [2, grow, gcol]
neg_vpstar_verti[1,...] = -neg_vpstar_verti[1,...]
reshaped_neg_vpstar_verti = neg_vpstar_verti.reshape(2, 1, grow, gcol) # [2, 1, grow, gcol]
mul_right = np.concatenate((reshaped_vpstar, reshaped_neg_vpstar_verti), axis=1) # [2, 2, grow, gcol]
reshaped_mul_right = mul_right.reshape(1, 2, 2, grow, gcol) # [1, 2, 2, grow, gcol]
A = np.matmul((reshaped_w * mul_left).transpose(0, 3, 4, 1, 2),
reshaped_mul_right.transpose(0, 3, 4, 1, 2)) # [ctrls, grow, gcol, 2, 2]
# Calculate q
reshaped_q = q.reshape((ctrls, 2, 1, 1)) # [ctrls, 2, 1, 1]
qstar = np.sum(w * reshaped_q.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
qhat = reshaped_q - qstar # [2, grow, gcol]
reshaped_qhat = qhat.reshape(ctrls, 1, 2, grow, gcol).transpose(0, 3, 4, 1, 2) # [ctrls, grow, gcol, 1, 2]
# Get final image transfomer -- 3-D array
temp = np.sum(np.matmul(reshaped_qhat, A), axis=0).transpose(2, 3, 0, 1) # [1, 2, grow, gcol]
reshaped_temp = temp.reshape(2, grow, gcol) # [2, grow, gcol]
norm_reshaped_temp = np.linalg.norm(reshaped_temp, axis=0, keepdims=True) # [1, grow, gcol]
norm_vpstar = np.linalg.norm(vpstar, axis=0, keepdims=True) # [1, grow, gcol]
transformers = reshaped_temp / norm_reshaped_temp * norm_vpstar + qstar # [2, grow, gcol]
# Removed the points outside the border
transformers[transformers < 0] = 0
transformers[0][transformers[0] > height - 1] = 0
transformers[1][transformers[1] > width - 1] = 0
# Mapping original image
transformed_image = np.ones_like(image) * 255
new_gridY, new_gridX = np.meshgrid((np.arange(gcol) / density).astype(np.int16),
(np.arange(grow) / density).astype(np.int16))
transformed_image[tuple(transformers.astype(np.int16))] = image[new_gridX, new_gridY] # [grow, gcol]
return transformed_image
def mls_rigid_deformation_inv(image, p, q, alpha=1.0, density=1.0):
''' Rigid inverse deformation
### Params:
* image - ndarray: original image
* p - ndarray: an array with size [n, 2], original control points
* q - ndarray: an array with size [n, 2], final control points
* alpha - float: parameter used by weights
* density - float: density of the grids
### Return:
A deformed image.
'''
height = image.shape[0]
width = image.shape[1]
# Change (x, y) to (row, col)
q = q[:, [1, 0]]
p = p[:, [1, 0]]
# Make grids on the original image
gridX = np.linspace(0, width, num=int(width*density), endpoint=False)
gridY = np.linspace(0, height, num=int(height*density), endpoint=False)
vy, vx = np.meshgrid(gridX, gridY)
grow = vx.shape[0] # grid rows
gcol = vx.shape[1] # grid cols
ctrls = p.shape[0] # control points
# Compute
reshaped_p = p.reshape(ctrls, 2, 1, 1) # [ctrls, 2, 1, 1]
reshaped_q = q.reshape((ctrls, 2, 1, 1)) # [ctrls, 2, 1, 1]
reshaped_v = np.vstack((vx.reshape(1, grow, gcol), vy.reshape(1, grow, gcol))) # [2, grow, gcol]
w = 1.0 / np.sum((reshaped_p - reshaped_v) ** 2, axis=1)**alpha # [ctrls, grow, gcol]
w[w == np.inf] = 2**31 - 1
pstar = np.sum(w * reshaped_p.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
phat = reshaped_p - pstar # [ctrls, 2, grow, gcol]
qstar = np.sum(w * reshaped_q.transpose(1, 0, 2, 3), axis=1) / np.sum(w, axis=0) # [2, grow, gcol]
qhat = reshaped_q - qstar # [ctrls, 2, grow, gcol]
reshaped_phat1 = phat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
reshaped_phat2 = phat.reshape(ctrls, 2, 1, grow, gcol) # [ctrls, 2, 1, grow, gcol]
reshaped_qhat = qhat.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
reshaped_w = w.reshape(ctrls, 1, 1, grow, gcol) # [ctrls, 1, 1, grow, gcol]
mu = np.sum(np.matmul(reshaped_w.transpose(0, 3, 4, 1, 2) *
reshaped_phat1.transpose(0, 3, 4, 1, 2),
reshaped_phat2.transpose(0, 3, 4, 1, 2)), axis=0) # [grow, gcol, 1, 1]
reshaped_mu = mu.reshape(1, grow, gcol) # [1, grow, gcol]
neg_phat_verti = phat[:, [1, 0],...] # [ctrls, 2, grow, gcol]
neg_phat_verti[:, 1,...] = -neg_phat_verti[:, 1,...]
reshaped_neg_phat_verti = neg_phat_verti.reshape(ctrls, 1, 2, grow, gcol) # [ctrls, 1, 2, grow, gcol]
mul_right = np.concatenate((reshaped_phat1, reshaped_neg_phat_verti), axis=1) # [ctrls, 2, 2, grow, gcol]
mul_left = reshaped_qhat * reshaped_w # [ctrls, 1, 2, grow, gcol]
Delta = np.sum(np.matmul(mul_left.transpose(0, 3, 4, 1, 2),
mul_right.transpose(0, 3, 4, 1, 2)),
axis=0).transpose(0, 1, 3, 2) # [grow, gcol, 2, 1]
Delta_verti = Delta[...,[1, 0],:] # [grow, gcol, 2, 1]
Delta_verti[...,0,:] = -Delta_verti[...,0,:]
B = np.concatenate((Delta, Delta_verti), axis=3) # [grow, gcol, 2, 2]
try:
inv_B = np.linalg.inv(B) # [grow, gcol, 2, 2]
flag = False
except np.linalg.linalg.LinAlgError:
flag = True
det = np.linalg.det(B) # [grow, gcol]
det[det < 1e-8] = np.inf
reshaped_det = det.reshape(grow, gcol, 1, 1) # [grow, gcol, 1, 1]
adjoint = B[:,:,[[1, 0], [1, 0]], [[1, 1], [0, 0]]] # [grow, gcol, 2, 2]
adjoint[:,:,[0, 1], [1, 0]] = -adjoint[:,:,[0, 1], [1, 0]] # [grow, gcol, 2, 2]
inv_B = (adjoint / reshaped_det).transpose(2, 3, 0, 1) # [2, 2, grow, gcol]
vqstar = reshaped_v - qstar # [2, grow, gcol]
reshaped_vqstar = vqstar.reshape(1, 2, grow, gcol) # [1, 2, grow, gcol]
# Get final image transfomer -- 3-D array
temp = np.matmul(reshaped_vqstar.transpose(2, 3, 0, 1),
inv_B).reshape(grow, gcol, 2).transpose(2, 0, 1) # [2, grow, gcol]
norm_temp = np.linalg.norm(temp, axis=0, keepdims=True) # [1, grow, gcol]
norm_vqstar = np.linalg.norm(vqstar, axis=0, keepdims=True) # [1, grow, gcol]
transformers = temp / norm_temp * norm_vqstar + pstar # [2, grow, gcol]
# Correct the points where pTwp is singular
if flag:
blidx = det == np.inf # bool index
transformers[0][blidx] = vx[blidx] + qstar[0][blidx] - pstar[0][blidx]
transformers[1][blidx] = vy[blidx] + qstar[1][blidx] - pstar[1][blidx]
# Removed the points outside the border
transformers[transformers < 0] = 0
transformers[0][transformers[0] > height - 1] = 0
transformers[1][transformers[1] > width - 1] = 0
# Mapping original image
transformed_image = image[tuple(transformers.astype(np.int16))] # [grow, gcol]
# Rescale image
transformed_image = rescale(transformed_image, scale=1.0 / density, mode='reflect')
return transformed_image