-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDLT.py
89 lines (70 loc) · 2.93 KB
/
DLT.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
import torch
import numpy as np
import cv2
import os
import torch
def get_grid(batch_size, H, W, start=0):
if torch.cuda.is_available():
xx = torch.arange(0, W).cuda()
yy = torch.arange(0, H).cuda()
else:
xx = torch.arange(0, W)
yy = torch.arange(0, H)
xx = xx.view(1, -1).repeat(H, 1)
yy = yy.view(-1, 1).repeat(1, W)
xx = xx.view(1, 1, H, W).repeat(batch_size, 1, 1, 1)
yy = yy.view(1, 1, H, W).repeat(batch_size, 1, 1, 1)
grid = torch.cat((xx, yy), 1).float()
grid[:, :2, :, :] = grid[:, :2, :, :] + start # add the coordinate of left top
return grid
def DLT_solve(src_p, off_set):
# src_p: shape=(bs, n, 4, 2)
# off_set: shape=(bs, n, 4, 2)
# can be used to compute mesh points (multi-H)
bs, _ = src_p.shape[:2]
divide = int(np.sqrt(len(src_p[0]) / 2) - 1)
row_num = (divide + 1) * 2
src_ps = src_p
off_sets = off_set
for i in range(divide):
for j in range(divide):
h4p = src_p[:, [2 * j + row_num * i, 2 * j + row_num * i + 1,
2 * (j + 1) + row_num * i, 2 * (j + 1) + row_num * i + 1,
2 * (j + 1) + row_num * i + row_num, 2 * (j + 1) + row_num * i + row_num + 1,
2 * j + row_num * i + row_num, 2 * j + row_num * i + row_num + 1]].reshape(bs, 1, 4, 2)
pred_h4p = off_set[:, [2 * j + row_num * i, 2 * j + row_num * i + 1,
2 * (j + 1) + row_num * i, 2 * (j + 1) + row_num * i + 1,
2 * (j + 1) + row_num * i + row_num, 2 * (j + 1) + row_num * i + row_num + 1,
2 * j + row_num * i + row_num, 2 * j + row_num * i + row_num + 1]].reshape(bs, 1, 4,
2)
if i + j == 0:
src_ps = h4p
off_sets = pred_h4p
else:
src_ps = torch.cat((src_ps, h4p), axis=1)
off_sets = torch.cat((off_sets, pred_h4p), axis=1)
bs, n, h, w = src_ps.shape
N = bs * n
src_ps = src_ps.reshape(N, h, w)
off_sets = off_sets.reshape(N, h, w)
dst_p = src_ps + off_sets
ones = torch.ones(N, h, 1)
if off_set.is_cuda:
ones = ones.to(off_set.device)
xy1 = torch.cat((src_ps, ones), 2)
zeros = torch.zeros_like(xy1)
if off_set.is_cuda:
zeros = zeros.to(off_set.device)
xyu, xyd = torch.cat((xy1, zeros), 2), torch.cat((zeros, xy1), 2)
M1 = torch.cat((xyu, xyd), 2).reshape(N, -1, 6)
M2 = torch.matmul(
dst_p.reshape(-1, 2, 1),
src_ps.reshape(-1, 1, 2),
).reshape(N, -1, 2)
A = torch.cat((M1, -M2), 2)
b = dst_p.reshape(N, -1, 1)
Ainv = torch.linalg.inv(A)
h8 = torch.matmul(Ainv, b).reshape(N, 8)
H = torch.cat((h8, ones[:, 0, :]), 1).reshape(N, 3, 3)
H = H.reshape(bs, n, 3, 3)
return H