-
Notifications
You must be signed in to change notification settings - Fork 0
/
MovingLSQ.py
256 lines (208 loc) · 9.03 KB
/
MovingLSQ.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
import numpy as np
import functools
from scipy.sparse import lil_matrix
from scipy.optimize import least_squares
def Error_Affine(x0, src, dst, weight_map, p_star, q_star, buf, buf_map):
label_num = src.shape[0]
a = x0.reshape([-1, 4])
for i in range(label_num):
p = src[i, :]
q = dst[i, :]
p_bar = -p_star + p
q_bar = -q_star + q
buf[:, 0] = p_bar[:, 0] * a[:, 0] + p_bar[:, 1] * a[:, 2]
buf[:, 1] = p_bar[:, 0] * a[:, 1] + p_bar[:, 1] * a[:, 3]
buf -= q_bar
buf_map[:, i] = np.linalg.norm(buf, axis=1) * weight_map[:, i]
error = np.sum(buf_map, axis=1)
return error
def Error_Rigid(x0, src, dst, weight_map, p_star, q_star, buf, buf_map):
label_num = src.shape[0]
a_cos = np.cos(x0)
a_sin = np.sin(x0)
for i in range(label_num):
p = src[i, :]
q = dst[i, :]
p_bar = -p_star + p
q_bar = -q_star + q
buf[:, 0] = p_bar[:, 0] * a_cos + p_bar[:, 1] * a_sin
buf[:, 1] = p_bar[:, 0] * -a_sin + p_bar[:, 1] * a_cos
buf -= q_bar
buf_map[:, i] = np.linalg.norm(buf, axis=1) * weight_map[:, i]
error = np.sum(buf_map, axis=1)
return error
def Error_Similarity(x0, src, dst, weight_map, p_star, q_star, buf, buf_map):
label_num = src.shape[0]
a = x0.reshape([-1, 2])
scale = a[:, 0]
a_cos = np.cos(a[:, 1])
a_sin = np.sin(a[:, 1])
for i in range(label_num):
p = src[i, :]
q = dst[i, :]
p_bar = -p_star + p
q_bar = -q_star + q
buf[:, 0] = (p_bar[:, 0] * a_cos + p_bar[:, 1] * a_sin) * scale
buf[:, 1] = (p_bar[:, 0] * -a_sin + p_bar[:, 1] * a_cos) * scale
buf -= q_bar
buf_map[:, i] = np.linalg.norm(buf, axis=1) * weight_map[:, i]
error = np.sum(buf_map, axis=1)
return error
class MovingLSQ:
def __init__(self, src, dst):
#
# Both src and dst are N x 2 numpy array, they are labeled by user
#
[h, w] = src.shape
self._label_num = h
self._src = src
self._dst = dst
def Run_Affine(self, srcPts, alpha = 1):
npoints = srcPts.shape[0]
label_num = self._label_num
weight = np.zeros([npoints, label_num], np.float)
for i in range(label_num):
label = self._src[i, :]
D = np.linalg.norm( srcPts - label , axis = 1)
D[D == 0] += 0.000001
weight[:, i] = D[:]
#weight += 0.00001 # prevent zero
weight = 1 / (weight**(2*alpha))
# weigth is npoints x label_num
jacobian = lil_matrix((npoints, npoints*4), dtype=int)
idx = np.arange(npoints)
for i in range(4):
jacobian[idx, 4 * idx + i] = 1
x0 = np.zeros(npoints*4)
x0[idx * 4] = 1
x0[idx * 4 + 2] = 1
# init is 1, 0, 1, 0
p_star = np.zeros([npoints, 2], np.float)
q_star = np.zeros([npoints, 2], np.float)
p_tmp = np.zeros([npoints, 2], np.float)
q_tmp = np.zeros([npoints, 2], np.float)
for i in range(label_num):
p = self._src[i, :]
q = self._dst[i, :]
p_tmp[:, 0] += weight[:, i] * p[0]
p_tmp[:, 1] += weight[:, i] * p[1]
q_tmp[:, 0] += weight[:, i] * q[0]
q_tmp[:, 1] += weight[:, i] * q[1]
p_star[:, 0] = p_tmp[:, 0] / np.sum(weight, axis=1)
p_star[:, 1] = p_tmp[:, 1] / np.sum(weight, axis=1)
q_star[:, 0] = q_tmp[:, 0] / np.sum(weight, axis=1)
q_star[:, 1] = q_tmp[:, 1] / np.sum(weight, axis=1)
buf = np.zeros([npoints, 2], np.float)
buf_map = np.zeros([npoints, label_num], np.float)
#print Error_2D_Affine(x0, self._src, self._dst, weight, p_star, q_star, buf, buf_map)
result = least_squares(Error_Affine, x0, jac_sparsity = jacobian, verbose=2,
args=(self._src, self._dst, weight, p_star, q_star, buf, buf_map))
M = result['x'].reshape([-1, 4])
buf[:, 0] = p_star[:, 0] * M[:, 0] + p_star[:, 1] * M[:, 2]
buf[:, 1] = p_star[:, 0] * M[:, 1] + p_star[:, 1] * M[:, 3]
T = q_star - buf
buf[:, 0] = srcPts[:, 0] * M[:, 0] + srcPts[:, 1] * M[:, 2]
buf[:, 1] = srcPts[:, 0] * M[:, 1] + srcPts[:, 1] * M[:, 3]
return buf + T
def Run_Rigid(self, srcPts, alpha = 1):
npoints = srcPts.shape[0]
label_num = self._label_num
weight = np.zeros([npoints, label_num], np.float)
for i in range(label_num):
label = self._src[i, :]
D = np.linalg.norm( srcPts - label , axis = 1)
D[D == 0] += 0.000001
weight[:, i] = D[:]
#weight += 0.00001 # prevent zero
weight = 1 / (weight**(2*alpha))
# weigth is npoints x label_num
jacobian = lil_matrix((npoints, npoints), dtype=int)
idx = np.arange(npoints)
jacobian[idx, idx] = 1
x0 = np.zeros(npoints)
p_star = np.zeros([npoints, 2], np.float)
q_star = np.zeros([npoints, 2], np.float)
p_tmp = np.zeros([npoints, 2], np.float)
q_tmp = np.zeros([npoints, 2], np.float)
for i in range(label_num):
p = self._src[i, :]
q = self._dst[i, :]
p_tmp[:, 0] += weight[:, i] * p[0]
p_tmp[:, 1] += weight[:, i] * p[1]
q_tmp[:, 0] += weight[:, i] * q[0]
q_tmp[:, 1] += weight[:, i] * q[1]
p_star[:, 0] = p_tmp[:, 0] / np.sum(weight, axis=1)
p_star[:, 1] = p_tmp[:, 1] / np.sum(weight, axis=1)
q_star[:, 0] = q_tmp[:, 0] / np.sum(weight, axis=1)
q_star[:, 1] = q_tmp[:, 1] / np.sum(weight, axis=1)
buf = np.zeros([npoints, 2], np.float)
buf_map = np.zeros([npoints, label_num], np.float)
lower_bound = np.zeros(npoints) - np.pi
upper_bound = np.zeros(npoints) + np.pi
#print Error_2D_Rigid(x0, self._src, self._dst, weight, p_star, q_star, buf, buf_map)
#exit()
result = least_squares(Error_Rigid, x0, jac_sparsity = jacobian, verbose=2, bounds=(lower_bound, upper_bound),
args=(self._src, self._dst, weight, p_star, q_star, buf, buf_map))
x_cos = np.cos(result['x'])
x_sin = np.sin(result['x'])
buf[:, 0] = p_star[:, 0] * x_cos + p_star[:, 1] * x_sin
buf[:, 1] = p_star[:, 0] * -x_sin + p_star[:, 1] * x_cos
T = q_star - buf
buf[:, 0] = srcPts[:, 0] * x_cos + srcPts[:, 1] * x_sin
buf[:, 1] = srcPts[:, 0] * -x_sin + srcPts[:, 1] * x_cos
return buf + T
def Run_Similarity(self, srcPts, alpha = 1):
npoints = srcPts.shape[0]
label_num = self._label_num
weight = np.zeros([npoints, label_num], np.float)
for i in range(label_num):
label = self._src[i, :]
D = np.linalg.norm( srcPts - label , axis = 1)
D[D == 0] += 0.000001
weight[:, i] = D[:]
#weight += 0.00001 # prevent zero
weight = 1 / (weight**(2*alpha))
# weigth is npoints x label_num
jacobian = lil_matrix((npoints, npoints*2), dtype=int)
idx = np.arange(npoints)
for i in range(2):
jacobian[idx, 2 * idx + i] = 1
x0 = np.zeros(npoints*2)
x0[idx * 2] = 1
p_star = np.zeros([npoints, 2], np.float)
q_star = np.zeros([npoints, 2], np.float)
p_tmp = np.zeros([npoints, 2], np.float)
q_tmp = np.zeros([npoints, 2], np.float)
for i in range(label_num):
p = self._src[i, :]
q = self._dst[i, :]
p_tmp[:, 0] += weight[:, i] * p[0]
p_tmp[:, 1] += weight[:, i] * p[1]
q_tmp[:, 0] += weight[:, i] * q[0]
q_tmp[:, 1] += weight[:, i] * q[1]
p_star[:, 0] = p_tmp[:, 0] / np.sum(weight, axis=1)
p_star[:, 1] = p_tmp[:, 1] / np.sum(weight, axis=1)
q_star[:, 0] = q_tmp[:, 0] / np.sum(weight, axis=1)
q_star[:, 1] = q_tmp[:, 1] / np.sum(weight, axis=1)
buf = np.zeros([npoints, 2], np.float)
buf_map = np.zeros([npoints, label_num], np.float)
lower_bound = np.zeros(npoints*2)
lower_bound[idx * 2] = 0
lower_bound[idx * 2 + 1] = -np.pi
upper_bound = np.zeros(npoints*2)
upper_bound[idx * 2] = np.inf
upper_bound[idx * 2 + 1] = np.pi
#print Error_2D_Affine(x0, self._src, self._dst, weight, p_star, q_star, buf, buf_map)
result = least_squares(Error_Similarity, x0, jac_sparsity = jacobian, verbose=2, bounds=(lower_bound, upper_bound),
args=(self._src, self._dst, weight, p_star, q_star, buf, buf_map))
#exit()
a = result['x'].reshape([-1, 2])
a_cos = np.cos(a[:, 1])
a_sin = np.sin(a[:, 1])
scale = a[:, 0]
buf[:, 0] = (p_star[:, 0] * a_cos + p_star[:, 1] * a_sin) * scale
buf[:, 1] = (p_star[:, 0] * -a_sin + p_star[:, 1] * a_cos) * scale
T = q_star - buf
buf[:, 0] = (srcPts[:, 0] * a_cos + srcPts[:, 1] * a_sin) * scale
buf[:, 1] = (srcPts[:, 0] * -a_sin + srcPts[:, 1] * a_cos) * scale
return buf + T