-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcamera_model_localization.py
58 lines (47 loc) · 2.23 KB
/
camera_model_localization.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
# -------------------------------------------------------------------
# Copyright (C) 2020 Università degli studi di Milano-Bicocca, iralab
# Author: Daniele Cattaneo ([email protected])
# Released under Creative Commons
# Attribution-NonCommercial-ShareAlike 4.0 International License.
# http://creativecommons.org/licenses/by-nc-sa/4.0/
# -------------------------------------------------------------------
import numpy as np
import torch
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")
# [0, 2, 1, 3] [2, 1, 0, 3]/ [1, 2, 0, 3] [0, 1, 2, 3]/ [2, 0, 1, 3]
order = [1, 2, 0, 3]
xyzw = xyz[order, :] # lidar_coor -> camera_coor
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[:2, indexes], xyzw[2, indexes], xyzw[0, indexes], xyzw[1, indexes], None
else:
uv = uv[:2, indexes], xyzw[2, indexes], xyzw[0, indexes], xyzw[1, indexes], reflectance[:, indexes]
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