forked from ycjungSubhuman/DeepDeformable3DCaricatures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransform.py
More file actions
executable file
·241 lines (187 loc) · 5.67 KB
/
Copy pathtransform.py
File metadata and controls
executable file
·241 lines (187 loc) · 5.67 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
import numpy as np
import scipy
from math import sin, cos, atan2
from scipy.spatial.transform import Rotation
def align_3d_to_2d(pt3d, pt2d):
"""
Get pose of 3D pointset based on its 2D projections
Kemelmacher-Shlizerman & Seitz. Face Reconstruction in the Wild. ICCV. 2011.
https://grail.cs.washington.edu/3dfaces/paper.pdf
"""
q = pt2d.copy().T
q_bar = np.mean(q, axis=1)
p = (q.T - q_bar.T).T
Q = pt3d.copy().T # Use mean face with features for pose optimization
Q_bar = np.mean(Q, axis=1)
P = (Q.T - Q_bar.T).T
PPTinv = scipy.linalg.inv(P@P.T)
A = p@P.T@PPTinv
# translation
t = q_bar - A@Q_bar
row_third = np.cross(A[0].T, A[1].T)
A_prime = np.vstack([A, row_third.T])
# rotation
u, s, vh = np.linalg.svd(A_prime)
R = u @ vh
# scale
s = 0.5*(np.linalg.norm(A[0]) + np.linalg.norm(A[1]))
t = np.array([t[0], t[1], 0.0])
return R, s, t
def ralign(X,Y):
"""
Rigid alignment between two pointsets using Umeyama algorithm
X (n x m) points (i.e., m=3 or 2)
Y (n x m) points
"""
X = X.T
Y = Y.T
m, n = X.shape
mx = X.mean(1)
my = Y.mean(1)
Xc = X - np.tile(mx, (n, 1)).T
Yc = Y - np.tile(my, (n, 1)).T
sx = np.mean(np.sum(Xc*Xc, 0))
Sxy = np.dot(Yc, Xc.T) / n
U,D,V = np.linalg.svd(Sxy,full_matrices=True,compute_uv=True)
V=V.T.copy()
S = np.eye(m)
R = np.dot( np.dot(U, S ), V.T)
s = np.trace(np.dot(np.diag(D), S)) / sx
t = my - s * np.dot(R, mx)
return R,s,t
def rigid_registeration(src_array, tar_array, index = None):
'''
apply rigid registeration between 2 meshes.
'''
if index is not None:
src_array = src_array[index]
tar_array = tar_array[index]
M = np.matmul((src_array - np.mean(src_array, axis=0)).T, (tar_array - np.mean(tar_array, axis=0)))
u,s,v= np.linalg.svd(M)
# print(np.dot(np.dot(u,np.diag(s)), v))
sig = np.ones(s.shape)
sig[-1] = np.linalg.det(np.dot(u,v))
R = np.matmul(np.matmul(v.T, np.diag(sig)), u.T)
t = np.mean(tar_array, axis=0) - np.matmul(R,np.mean(src_array, axis=0))
return R, t
def apply_trans(X, params):
return params['s'] * X @ params['R'].T + params['t']
def rot2angle(R):
"""
Convert 2x2 rotation matrix to angle (radians)
"""
return atan2(R[1, 0], R[0, 0])
class Affine2D:
"""
2D affine transformation
(similarity)
"""
LENGTH=1+1+2
def __init__(self, arr):
# scale
self.s = arr[0]
# rotation
self.theta = arr[1]
# translation
self.tx = arr[2]
self.ty = arr[3]
@staticmethod
def identity():
return Affine2D([1.0, 0.0, 0.0, 0.0])
@staticmethod
def optimize(X, Y):
"""
X (N x 2) source points
Y (N x 2) target points
Returns)
A transformation applied to X.
"""
R, s, t = ralign(X, Y)
theta = rot2angle(R)
return Affine2D([s, theta, t[0], t[1]])
def as_matrix(self):
"""
Return 3X3 matrix for a 2D point in homogeneous coordinate
"""
return np.array([
[self.s*cos(self.theta), -self.s*sin(self.theta), self.tx],
[self.s*sin(self.theta), self.s*cos(self.theta), self.ty],
[0.0, 0.0, 1.0],
])
def as_array(self):
return np.array([self.s, self.theta, self.tx, self.ty])
def apply(self, pts):
"""
pts input 2D points (N x 2)
Returns)
Transformed points (N x 2)
"""
M = self.as_matrix()
R = M[:2, :2]
return (R @ pts.T).T + M[:2, 2]
def __repr__(self):
return 'Affine2D: (s={}, theta={} rad, tx={}, ty={})'.format(self.s, self.theta, self.tx, self.ty)
class Affine3D:
"""
2D affine transformation
(similarity)
"""
LENGTH=1+3+3
def __init__(self, arr):
# scale
self.s = arr[0]
# rotation
self.angles = arr[1:4]
# translation
self.t = arr[4:7]
@staticmethod
def identity():
return Affine3D([
1.0,
0.0, 0.0, 0.0,
0.0, 0.0, 0.0])
@staticmethod
def optimize(X, Y):
"""
X (N x 3) source points
Y (N x 3) target points
Returns)
A transformation applied to X.
"""
R, s, t = ralign(X, Y)
angles = Rotation.from_matrix(R).as_euler('XYZ')
return Affine3D(np.concatenate([[s], angles, t]))
@staticmethod
def optimize_2d(X, Y):
"""
X (N x 3) source points
Y (N x 2) target points
Returns)
A transformation applied to X.
"""
assert X.shape[1] == 3 and Y.shape[1] == 2
R, s, t = align_3d_to_2d(X, Y)
angles = Rotation.from_matrix(R).as_euler('XYZ')
return Affine3D(np.concatenate([[s], angles, t]))
def as_matrix(self):
"""
Return 4X4 matrix for a 3D point in homogeneous coordinate
"""
R = self.s * Rotation.from_euler('XYZ', self.angles).as_matrix()
result = np.eye(4)
result[:3,:3] = R
result[:3,3] = self.t
return result
def as_array(self):
return np.concatenate([[self.s], self.angles, self.t])
def apply(self, pts):
"""
pts input 3D points (N x 3)
Returns)
Transformed points (N x 3)
"""
M = self.as_matrix()
R = M[:3, :3]
return (R @ pts.T).T + M[:3, 3]
def __repr__(self):
return 'Affine3D: (s={}, angles={} rad, t={})'.format(self.s, self.angles, self.t)