-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.py
30 lines (26 loc) · 853 Bytes
/
util.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
import numpy as np
import scipy.optimize
def _singularize(F):
U, S, V = np.linalg.svd(F)
S[-1] = 0
F = U.dot(np.diag(S).dot(V))
return F
def _objective_F(f, pts1, pts2):
F = _singularize(f.reshape([3, 3]))
num_points = pts1.shape[0]
hpts1 = np.concatenate([pts1, np.ones([num_points, 1])], axis=1)
hpts2 = np.concatenate([pts2, np.ones([num_points, 1])], axis=1)
Fp1 = F.dot(hpts1.T)
FTp2 = F.T.dot(hpts2.T)
r = 0
for fp1, fp2, hp2 in zip(Fp1.T, FTp2.T, hpts2):
r += (hp2.dot(fp1))**2 * (1/(fp1[0]**2 + fp1[1]**2) + \
1/(fp2[0]**2 + fp2[1]**2))
return r
def refineF(F, pts1, pts2):
f = scipy.optimize.fmin_powell(
lambda x: _objective_F(x, pts1, pts2), F.reshape([-1]),
maxiter=100000,
maxfun=10000
)
return _singularize(f.reshape([3, 3]))