-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathestimator.py
160 lines (135 loc) · 6.07 KB
/
estimator.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
import numpy as np
import cv2
from utils.base_utils import pts_to_hpts, hpts_to_pts,process_image
def normalize_coordinates_camera(pts1, pts2, K1, K2):
pts1, pts2 = np.ascontiguousarray(pts1, np.float32), np.ascontiguousarray(pts2, np.float32)
pts_l_norm = hpts_to_pts(pts_to_hpts(pts1) @ np.linalg.inv(K1).T)
pts_r_norm = hpts_to_pts(pts_to_hpts(pts2) @ np.linalg.inv(K2).T)
return pts_l_norm, pts_r_norm
class RANSACEstimator:
def __init__(self,cfg):
self.configs=cfg
def estimate_pose(self,pts1, pts2, K1, K2):
if len(pts1) < 5:
return None
pts_l_norm, pts_r_norm = normalize_coordinates_camera(pts1, pts2, K1, K2)
E, mask = cv2.findEssentialMat(pts_l_norm, pts_r_norm, focal=1.0, pp=(0., 0.),
method=cv2.RANSAC, prob=self.configs['confidence'],
threshold=self.configs['thresh'])
# E, mask = cv2.findEssentialMat(pts_l_norm, pts_r_norm,
# method=cv2.RANSAC,
# threshold=self.configs['thresh'])
if E is None:
import ipdb; ipdb.set_trace()
assert E is not None
best_num_inliers = 0
ret = None
for _E in np.split(E, len(E) / 3):
n, R, t, _ = cv2.recoverPose(
_E, pts1, pts2, np.eye(3), 1e9, mask=mask)
if n > best_num_inliers:
best_num_inliers = n
ret = (R, t[:, 0], mask.ravel() > 0)
return ret
def pose_estimate(self, pts1, pts2, K1, K2, *args, **kwargs):
# f_avg = (K1[0, 0] + K2[0, 0]) / 2
results=self.estimate_pose(pts1, pts2, K1, K2)
if results is not None:
R, t, mask = results
else:
R, t, mask=np.eye(3), np.zeros(3), np.zeros(pts1.shape[0],np.bool)
return mask, R, t[:,None]
def estimate_pose_pts(self,pts1, pts2, K1, K2):
if len(pts1) < 5:
return None
pts_l_norm, pts_r_norm = normalize_coordinates_camera(pts1, pts2, K1, K2)
E, mask = cv2.findEssentialMat(pts_l_norm, pts_r_norm, focal=1.0, pp=(0., 0.),
method=cv2.RANSAC, prob=self.configs['confidence'],
threshold=self.configs['thresh'])
# E, mask = cv2.findEssentialMat(pts_l_norm, pts_r_norm,
# method=cv2.RANSAC,
# threshold=self.configs['thresh'])
if E is None:
import ipdb; ipdb.set_trace()
assert E is not None
best_num_inliers = 0
ret = None
for _E in np.split(E, len(E) / 3):
n, R, t, _ = cv2.recoverPose(
_E, pts1, pts2, np.eye(3), 1e9, mask=mask)
if n > best_num_inliers:
best_num_inliers = n
ret = (R, t[:, 0], mask.ravel() > 0)
return ret
def pose_estimate_pts(self, pts1, pts2):
results=self.estimate_pose(pts1, pts2)
if results is not None:
R, t, mask = results
else:
R, t, mask=np.eye(3), np.zeros(3), np.zeros(pts1.shape[0],np.bool)
return R, t[:,None]
def fundamental_matrix_estimate(self, pts1, pts2):
F, mask = cv2.findFundamentalMat(pts1, pts2, cv2.FM_RANSAC, ransacReprojThreshold=self.configs['thresh'],
confidence=self.configs['confidence'])
return mask[:,0].astype(np.bool), F
def homography_estimation(self,pts1,pts2):
H, mask = cv2.findHomography(pts1[:,None,:2],pts2[:,None,:2],method=cv2.RANSAC,ransacReprojThreshold=self.configs['thresh'])
return H
class RescaleRANSACEstimator:
@staticmethod
def estimate_pose(kpts0, kpts1, K0, K1, thresh, conf=0.99999):
if len(kpts0) < 5:
return None
f_mean = np.mean([K0[0, 0], K1[1, 1], K0[0, 0], K1[1, 1]])
norm_thresh = thresh / f_mean
kpts0 = (kpts0 - K0[[0, 1], [2, 2]][None]) / K0[[0, 1], [0, 1]][None]
kpts1 = (kpts1 - K1[[0, 1], [2, 2]][None]) / K1[[0, 1], [0, 1]][None]
E, mask = cv2.findEssentialMat(
kpts0, kpts1, np.eye(3), threshold=norm_thresh, prob=conf,
method=cv2.RANSAC)
if E is None:
import ipdb; ipdb.set_trace()
assert E is not None
best_num_inliers = 0
ret = None
for _E in np.split(E, len(E) / 3):
n, R, t, _ = cv2.recoverPose(
_E, kpts0, kpts1, np.eye(3), 1e9, mask=mask)
if n > best_num_inliers:
best_num_inliers = n
ret = (R, t[:, 0], mask.ravel() > 0)
return ret
def __init__(self,cfg):
self.resize=cfg['resize']
self.resize_float=False if 'resize_float' in cfg and not cfg['resize_float'] else True
self.round=True if 'round' in cfg and cfg['round'] else False
def pose_estimate(self, pts1, pts2, K1, K2, image1, image2):
pts1, pts2, K1, K2 = np.copy(pts1), np.copy(pts2), np.copy(K1), np.copy(K2)
resize = (self.resize,) if isinstance(self.resize, int) else self.resize
_,_,scales=process_image(image1,resize=resize,resize_float=self.resize_float)
scales=np.ascontiguousarray(scales)
pts1=pts1/scales[None,:]
K1[0,0]/=scales[0]
K1[0,2]/=scales[0]
K1[1,1]/=scales[1]
K1[1,2]/=scales[1]
_,_,scales=process_image(image2,resize=resize,resize_float=self.resize_float)
scales = np.ascontiguousarray(scales)
pts2=pts2/scales[None,:]
K2[0,0]/=scales[0]
K2[0,2]/=scales[0]
K2[1,1]/=scales[1]
K2[1,2]/=scales[1]
if self.round:
pts1=np.round(pts1)
pts2=np.round(pts2)
results=self.estimate_pose(pts1,pts2,K1,K2,1.0)
if results is not None:
R, t, mask = results
else:
R, t, mask=np.eye(3), np.zeros(3), np.zeros(pts1.shape[0],np.bool)
return mask, R, t[:,None]
name2estimator={
'ransac': RANSACEstimator,
'rescale_ransac': RescaleRANSACEstimator,
}