-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcolorized_point_cloud.py
156 lines (139 loc) · 6.04 KB
/
colorized_point_cloud.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
# Copyright (c) 2023, Amarskiy Artem and Yaroslav Muravev
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import open3d as o3d
import numpy as np
def get_list_of_point_colors_from_image(points, image):
"""
Extracts colors from an image for each 2D point.
Parameters:
points (numpy.ndarray): Array of 2D points (shape: (2, N)).
image (PIL.Image): Image object from which colors will be extracted.
Returns:
list: List of RGB colors (values in range [0, 1]) for each point.
"""
pixels = list(image.getdata())
colors = []
for i in range(points.shape[1]):
colors.append(pixels[(int(points[1, i]) * image.width + int(points[0, i]))])
for i in range(len(colors)):
colors[i] = (colors[i][0] / 255, colors[i][1] / 255, colors[i][2] / 255)
return colors
def __delete_point_which_not_visible_on_image(points, image):
points = points[:, points[0] >= 0]
points = points[:, points[1] >= 0]
points = points[:, points[2] >= 0]
points_depth = points[2]
points = points[:2, :] / points[2, :]
points = np.vstack((points, points_depth))
points = points[:, points[0] <= image.width]
points = points[:, points[1] <= image.height]
return points
def get_colorized_2d_point_cloud(dataset, index_of_image, cam_number):
"""
Generates a colorized 2D point cloud based on the velodyne data and the given image.
Parameters:
dataset (pykitti.odometry): The dataset containing the Velodyne and camera data.
index_of_image (int): Index of the image in the dataset.
cam_number (int): Camera number (2 for camera 2, 3 for camera 3).
Returns:
open3d.geometry.PointCloud: Colorized 2D point cloud.
"""
velo_data = dataset.get_velo(index_of_image)
if cam_number == 2:
image = dataset.get_cam2(index_of_image)
else:
image = dataset.get_cam3(index_of_image)
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(velo_data[:, :3])
if cam_number == 2:
point_cloud.transform(dataset.calib.T_cam2_velo)
points3d_coordinates_in_2d = (
dataset.calib.K_cam2 @ np.asarray(point_cloud.points).transpose()
)
else:
point_cloud.transform(dataset.calib.T_cam3_velo)
points3d_coordinates_in_2d = (
dataset.calib.K_cam3 @ np.asarray(point_cloud.points).transpose()
)
points3d_coordinates_in_2d = __delete_point_which_not_visible_on_image(
points3d_coordinates_in_2d, image
)
point_cloud.points = o3d.utility.Vector3dVector(
points3d_coordinates_in_2d.transpose()
)
point_cloud.colors = o3d.utility.Vector3dVector(
get_list_of_point_colors_from_image(points3d_coordinates_in_2d, image)
)
return point_cloud
def get_colorized_3d_point_cloud(dataset, index_of_image, cam_number):
"""
Generates a colorized 3D point cloud based on the velodyne data and the given image.
Parameters:
dataset (pykitti.odometry): The dataset containing the Velodyne and camera data.
index_of_image (int): Index of the image in the dataset.
cam_number (int): Camera number (2 for camera 2, 3 for camera 3).
Returns:
open3d.geometry.PointCloud: Colorized 3D point cloud.
"""
velo_data = dataset.get_velo(index_of_image)
point_cloud = o3d.geometry.PointCloud()
point_cloud.points = o3d.utility.Vector3dVector(velo_data[:, :3])
if cam_number == 2:
image = dataset.get_cam2(index_of_image)
point_cloud.transform(dataset.calib.T_cam2_velo)
points3d_coordinates_in_2d = (
dataset.calib.K_cam2 @ np.asarray(point_cloud.points).transpose()
)
else:
image = dataset.get_cam3(index_of_image)
point_cloud.transform(dataset.calib.T_cam3_velo)
points3d_coordinates_in_2d = (
dataset.calib.K_cam3 @ np.asarray(point_cloud.points).transpose()
)
points3d_coordinates_in_2d = __delete_point_which_not_visible_on_image(
points3d_coordinates_in_2d, image
)
points_depth = points3d_coordinates_in_2d[2]
point_cloud = o3d.geometry.PointCloud()
point_cloud.colors = o3d.utility.Vector3dVector(
get_list_of_point_colors_from_image(points3d_coordinates_in_2d, image)
)
points3d_coordinates_in_2d = (
points3d_coordinates_in_2d[:2, :] * points3d_coordinates_in_2d[2, :]
)
points3d_coordinates_in_2d = np.vstack((points3d_coordinates_in_2d, points_depth))
if cam_number == 2:
new_points = np.linalg.inv(dataset.calib.K_cam2) @ points3d_coordinates_in_2d
else:
new_points = np.linalg.inv(dataset.calib.K_cam3) @ points3d_coordinates_in_2d
point_cloud.points = o3d.utility.Vector3dVector(new_points.transpose())
return point_cloud
def get_cloud_union_in_world_coords(dataset, cloud_2, cloud_3):
"""
Transforms and merges two camera-based point clouds into the world coordinate system.
Parameters:
dataset (pykitti.odometry): The dataset containing calib and poses data.
cloud_2 (open3d.geometry.PointCloud): Camera 2 point cloud.
cloud_3 (open3d.geometry.PointCloud): Camera 3 point cloud.
Returns:
open3d.geometry.PointCloud: Merged point cloud in world coordinates.
"""
cloud_2.transform(np.linalg.inv(dataset.calib.T_cam2_velo)).transform(
dataset.calib.T_cam0_velo
).transform(dataset.poses[0])
cloud_3.transform(np.linalg.inv(dataset.calib.T_cam3_velo)).transform(
dataset.calib.T_cam0_velo
).transform(dataset.poses[0])
cloud_3 += cloud_2
return cloud_3