forked from luigifreda/pyslam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera.py
234 lines (186 loc) · 8.44 KB
/
camera.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
"""
* This file is part of PYSLAM
*
* Copyright (C) 2016-present Luigi Freda <luigi dot freda at gmail dot com>
*
* PYSLAM is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PYSLAM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PYSLAM. If not, see <http://www.gnu.org/licenses/>.
"""
import numpy as np
import cv2
#import g2o
from utils_geom import add_ones
class Camera:
def __init__(self, width, height, fx, fy, cx, cy, D, fps = 1): # D = [k1, k2, p1, p2, k3]
self.width = width
self.height = height
self.fx = fx
self.fy = fy
self.cx = cx
self.cy = cy
self.D = np.array(D,dtype=np.float32) # np.array([k1, k2, p1, p2, k3]) distortion coefficients
self.fps = fps
self.is_distorted = np.linalg.norm(self.D) > 1e-10
self.initialized = False
class PinholeCamera(Camera):
def __init__(self, width, height, fx, fy, cx, cy, D, fps = 1):
super().__init__(width, height, fx, fy, cx, cy, D, fps)
self.K = np.array([[fx, 0,cx],
[ 0,fy,cy],
[ 0, 0, 1]])
self.Kinv = np.array([[1/fx, 0,-cx/fx],
[ 0, 1/fy,-cy/fy],
[ 0, 0, 1]])
self.u_min, self.u_max = 0, self.width
self.v_min, self.v_max = 0, self.height
self.init()
def init(self):
if not self.initialized:
self.initialized = True
self.undistort_image_bounds()
# project a 3D point or an array of 3D points (w.r.t. camera frame), of shape [Nx3]
# out: Nx2 image points, [Nx1] array of map point depths
def project(self, xcs):
#u = self.fx * xc[0]/xc[2] + self.cx
#v = self.fy * xc[1]/xc[2] + self.cy
#print("XCS:", xcs)
projs = self.K @ xcs.T
zs = projs[-1]
projs = projs[:2] / zs
#print("AQUI2:", np.shape(projs.T), projs.T, np.shape(zs), zs)
return projs.T, zs
# unproject 2D point uv (pixels on image plane) on
def unproject(self, uv):
x = (uv[0] - self.cx)/self.fx
y = (uv[1] - self.cy)/self.fy
return x,y
# in: uvs [Nx2]
# out: xcs array [Nx3] of normalized coordinates
def unproject_points(self, uvs):
return np.dot(self.Kinv, add_ones(uvs).T).T[:, 0:2]
# in: uvs [Nx2]
# out: uvs_undistorted array [Nx2] of undistorted coordinates
def undistort_points(self, uvs):
if self.is_distorted:
#uvs_undistorted = cv2.undistortPoints(np.expand_dims(uvs, axis=1), self.K, self.D, None, self.K) # => Error: while undistorting the points error: (-215:Assertion failed) src.isContinuous()
uvs_contiguous = np.ascontiguousarray(uvs[:, :2]).reshape((uvs.shape[0], 1, 2))
uvs_undistorted = cv2.undistortPoints(uvs_contiguous, self.K, self.D, None, self.K)
return uvs_undistorted.ravel().reshape(uvs_undistorted.shape[0], 2)
else:
return uvs
# update image bounds
def undistort_image_bounds(self):
uv_bounds = np.array([[self.u_min, self.v_min],
[self.u_min, self.v_max],
[self.u_max, self.v_min],
[self.u_max, self.v_max]], dtype=np.float32).reshape(4,2)
#print('uv_bounds: ', uv_bounds)
if self.is_distorted:
uv_bounds_undistorted = cv2.undistortPoints(np.expand_dims(uv_bounds, axis=1), self.K, self.D, None, self.K)
uv_bounds_undistorted = uv_bounds_undistorted.ravel().reshape(uv_bounds_undistorted.shape[0], 2)
else:
uv_bounds_undistorted = uv_bounds
#print('uv_bounds_undistorted: ', uv_bounds_undistorted)
self.u_min = min(uv_bounds_undistorted[0][0],uv_bounds_undistorted[1][0])
self.u_max = max(uv_bounds_undistorted[2][0],uv_bounds_undistorted[3][0])
self.v_min = min(uv_bounds_undistorted[0][1],uv_bounds_undistorted[2][1])
self.v_max = max(uv_bounds_undistorted[1][1],uv_bounds_undistorted[3][1])
# print('camera u_min: ', self.u_min)
# print('camera u_max: ', self.u_max)
# print('camera v_min: ', self.v_min)
# print('camera v_max: ', self.v_max)
def is_in_image(self, uv, z):
return (uv[0] > self.u_min) & (uv[0] < self.u_max) & \
(uv[1] > self.v_min) & (uv[1] < self.v_max) & \
(z > 0)
# input: [Nx2] array of uvs, [Nx1] of zs
# output: [Nx1] array of visibility flags
def are_in_image(self, uvs, zs):
return (uvs[:, 0] > self.u_min) & (uvs[:, 0] < self.u_max) & \
(uvs[:, 1] > self.v_min) & (uvs[:, 1] < self.v_max) & \
(zs > 0 )
class EquirecCamera(Camera):
def __init__(self, width, height, fx, fy, cx, cy, D, fps = 1):
super().__init__(width, height, fx, fy, cx, cy, D, fps)
self.u_min, self.u_max = 0, width
self.v_min, self.v_max = 0, height
self.K = np.array([[fx, 0,cx],
[ 0,fy,cy],
[ 0, 0, 1]])
self.Kinv = np.array([[1/fx, 0,-cx/fx],
[ 0, 1/fy,-cy/fy],
[ 0, 0, 1]])
self.xi = 1
self.Mc = np.array([-self.xi, 0 , 0,
0 , self.xi, 0,
0 , 0 , 1])
self.init()
def init(self):
if not self.initialized:
self.initialized = True
self.undistort_image_bounds()
def to_deg(rad):
"""From radians to degrees"""
return rad*180/np.pi
def to_rad(deg):
"""From degrees to radians"""
return deg*np.pi/180
# 3D to 2D
def project(self, xcs):
"""(lat, lon) in a sphere to (x, y) in a image"""
x = xcs[:, 0]
y = xcs[:, 1]
z = xcs[:, 2]
r = np.sqrt(x**2 + y**2 + z**2)
theta = np.arctan2(y, x)
phi = np.arccos(z/r)
for i in range(len(theta)):
if theta[i] < 0:
theta[i] += (2 * np.pi)
u = self.width * theta / (2 * np.pi)
v = self.height * phi / np.pi
uvs = np.array([i for i in zip(u, v)])
return uvs, z
# 2D to 3D
def unproject_points(self, uvs):
u = uvs[:, 0] / self.width
v = uvs[:, 1] / self.height
theta = u * 2 * np.pi
phi = v * np.pi
x = np.cos(theta) * np.sin(phi)
y = np.sin(theta) * np.sin(phi)
xcs = np.array([i for i in zip(x, y)])
return xcs
# in: uvs [Nx2]
# out: uvs_undistorted array [Nx2] of undistorted coordinates
def undistort_points(self, uvs):
return uvs
# update image bounds
def undistort_image_bounds(self):
uv_bounds = np.array([[self.u_min, self.v_min],
[self.u_min, self.v_max],
[self.u_max, self.v_min],
[self.u_max, self.v_max]], dtype=np.float32).reshape(4,2)
uv_bounds_undistorted = uv_bounds
self.u_min = min(uv_bounds_undistorted[0][0],uv_bounds_undistorted[1][0])
self.u_max = max(uv_bounds_undistorted[2][0],uv_bounds_undistorted[3][0])
self.v_min = min(uv_bounds_undistorted[0][1],uv_bounds_undistorted[2][1])
self.v_max = max(uv_bounds_undistorted[1][1],uv_bounds_undistorted[3][1])
def is_in_image(self, uv, z):
return (uv[0] > self.u_min) & (uv[0] < self.u_max) & \
(uv[1] > self.v_min) & (uv[1] < self.v_max) & \
(zs > 0 )
def are_in_image(self, uvs, zs):
return (uvs[:, 0] > self.u_min) & (uvs[:, 0] < self.u_max) & \
(uvs[:, 1] > self.v_min) & (uvs[:, 1] < self.v_max) & \
(zs > 0 )