-
Notifications
You must be signed in to change notification settings - Fork 1
/
camera_model.py
137 lines (113 loc) · 5.15 KB
/
camera_model.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
import numpy as np
import torch
import sys
sys.path.append("core")
from utils_point import rotate_forward, rotate_back
class CameraModel:
def __init__(self, focal_length=None, principal_point=None):
self.focal_length = focal_length
self.principal_point = principal_point
def project_pytorch(self, xyz: torch.Tensor, image_size, reflectance=None):
if xyz.shape[0] == 3:
xyz = torch.cat([xyz, torch.ones(1, xyz.shape[1], device=xyz.device)])
else:
if not torch.all(xyz[3, :] == 1.):
xyz[3, :] = 1.
raise TypeError("Wrong Coordinates")
order = [1, 2, 0, 3]
xyzw = xyz[order, :]
indexes = xyzw[2, :] >= 0
if reflectance is not None:
reflectance = reflectance[:, indexes]
xyzw = xyzw[:, indexes]
uv = torch.zeros((2, xyzw.shape[1]), device=xyzw.device)
uv[0, :] = self.focal_length[0] * xyzw[0, :] / xyzw[2, :] + self.principal_point[0]
uv[1, :] = self.focal_length[1] * xyzw[1, :] / xyzw[2, :] + self.principal_point[1]
indexes = uv[0, :] >= 0.1
indexes = indexes & (uv[1, :] >= 0.1)
indexes = indexes & (uv[0,:] < image_size[1])
indexes = indexes & (uv[1,:] < image_size[0])
if reflectance is None:
uv = uv[:, indexes], xyzw[2, indexes], xyzw[:3, indexes], None
else:
uv = uv[:, indexes], xyzw[2, indexes], xyzw[:3, indexes], reflectance[:, indexes]
return uv
# for pc_RT
def project_withindex_pytorch(self, xyz: torch.Tensor, image_size, reflectance=None):
if xyz.shape[0] == 3:
xyz = torch.cat([xyz, torch.ones(1, xyz.shape[1], device=xyz.device)])
else:
if not torch.all(xyz[3, :] == 1.):
xyz[3, :] = 1.
# raise TypeError("Wrong Coordinates")
order = [1, 2, 0, 3]
xyzw = xyz[order, :]
indexes = xyzw[2, :] >= 0
if reflectance is not None:
reflectance = reflectance[:, indexes]
xyzw = xyzw[:, indexes]
VI_indexes = indexes
uv = torch.zeros((2, xyzw.shape[1]), device=xyzw.device)
uv[0, :] = self.focal_length[0] * xyzw[0, :] / xyzw[2, :] + self.principal_point[0]
uv[1, :] = self.focal_length[1] * xyzw[1, :] / xyzw[2, :] + self.principal_point[1]
indexes = uv[0, :] >= 0.1
indexes = indexes & (uv[1, :] >= 0.1)
indexes = indexes & (uv[0, :] < image_size[1])
indexes = indexes & (uv[1, :] < image_size[0])
# generate complete indexes
ind = torch.where(VI_indexes == True)[0]
VI_indexes[ind] = VI_indexes[ind] & indexes
if reflectance is None:
uv = uv[:, indexes], xyzw[2, indexes], xyzw[:3, indexes], None, VI_indexes
else:
uv = uv[:, indexes], xyzw[2, indexes], xyzw[:3, indexes], reflectance[:, indexes], VI_indexes
return uv
def project_withoutindex_pytorch(self, xyz: torch.Tensor):
"""not consider potential outer point"""
if xyz.shape[0] == 3:
xyz = torch.cat([xyz, torch.ones(1, xyz.shape[1], device=xyz.device)])
else:
if not torch.all(xyz[3, :] == 1.):
xyz[3, :] = 1.
raise TypeError("Wrong Coordinates")
order = [1, 2, 0, 3]
xyzw = xyz[order, :]
uv = torch.zeros((2, xyzw.shape[1]), device=xyzw.device)
uv[0, :] = self.focal_length[0] * xyzw[0, :] / xyzw[2, :] + self.principal_point[0]
uv[1, :] = self.focal_length[1] * xyzw[1, :] / xyzw[2, :] + self.principal_point[1]
return uv
def get_matrix(self):
matrix = np.zeros([3, 3])
matrix[0, 0] = self.focal_length[0]
matrix[1, 1] = self.focal_length[1]
matrix[0, 2] = self.principal_point[0]
matrix[1, 2] = self.principal_point[1]
matrix[2, 2] = 1.0
return matrix
def deproject_pytorch(self, uv, pc_project_uv):
index = np.argwhere(uv > 0)
mask = uv > 0
z = uv[mask]
x = (index[:, 1] - self.principal_point[0]) * z / self.focal_length[0]
y = (index[:, 0] - self.principal_point[1]) * z / self.focal_length[1]
zxy = np.array([z, x, y])
zxy = torch.tensor(zxy, dtype=torch.float32)
zxyw = torch.cat([zxy, torch.ones(1, zxy.shape[1], device=zxy.device)])
zxy = zxyw[:3, :]
zxy = zxy.cpu().numpy()
xyz = zxy[[1, 2, 0], :]
pc_project_u = pc_project_uv[:, :, 0][mask]
pc_project_v = pc_project_uv[:, :, 1][mask]
pc_project = np.array([pc_project_v, pc_project_u])
match_index = np.array([index[:, 0], index[:, 1]])
return xyz.transpose(), pc_project.transpose(), match_index.transpose()
def depth2pc(self, uv):
index = np.argwhere(uv > 0)
mask = uv > 0
z = uv[mask]
x = (index[:, 1] - self.principal_point[0]) * z / self.focal_length[0]
y = (index[:, 0] - self.principal_point[1]) * z / self.focal_length[1]
zxy = np.array([z, x, y], dtype=np.float32)
zxy = torch.tensor(zxy, dtype=torch.float32)
zxyw = torch.cat([zxy, torch.ones(1, zxy.shape[1], device=zxy.device)])
return zxyw