-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathall_tools.py
83 lines (69 loc) · 2.78 KB
/
all_tools.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
import os
import numpy as np
import open3d as o3d
import plyfile
from plyfile import PlyElement, PlyData
import pyransac3d as pyrsc
def read_ply2np(path):
"""
:rtype: numpy
"""
ply_read = PlyData.read(path)
name = [ply_read["vertex"].properties[i].name for i in range(len(ply_read["vertex"].properties))]
data = np.array(ply_read["vertex"][name[0]]).reshape(-1, 1)
for i, name in enumerate(name[1:]):
temp_i = np.array(ply_read["vertex"][name]).reshape(-1, 1)
data = np.concatenate([data, temp_i], axis=1)
return data
def read_pcd2np(path,type="XYZRGB"):
"""
:rtype: numpy
"""
pcd = o3d.io.read_point_cloud(path)
# 将点云数据转换为NumPy数组
points = np.asarray(pcd.points)
if type=="XYZRGB":
colors = np.asarray(pcd.colors)
colors.astype(int)
np_pcd = np.concatenate([points, colors],axis=1)
return np_pcd
else:
return points
def save_ply_from_np(np_input, ply_path):
"""
Args:
np_input: numpy point cloud
ply_path: save path
Returns:
"""
dtype_list = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'int16'), ('green', 'int16'),
('blue', 'int16')]
points = [tuple(x) for x in np_input.tolist()]
if np_input.shape[1] == 6:
vertex = np.array(points, dtype=dtype_list)
el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
PlyData([el]).write(ply_path)
elif np_input.shape[1] == 7:
dtype_list = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'int16'), ('green', 'int16'),
('blue', 'int16'), ('scalar_sf', 'f4')]
vertex = np.array(points, dtype=dtype_list)
el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
PlyData([el]).write(ply_path)
elif np_input.shape[1] > 7:
dtype_list = [('x', 'f4'), ('y', 'f4'), ('z', 'f4'), ('red', 'int16'), ('green', 'int16'),
('blue', 'int16'), ('scalar_sf', 'f4')]
for i in range(np_input.shape[1] - 7):
dtype_list.append((f'scalar_sf{i + 1}', 'f4'))
vertex = np.array(points, dtype=dtype_list)
el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
PlyData([el]).write(ply_path)
elif np_input.shape[1] < 6:
dtype_list = [('x', 'f4'), ('y', 'f4'), ('z', 'f4')]
if np_input.shape[1] >= 4:
for i in range(np_input.shape[1] - 3):
dtype_list.append((f'scalar_sf{i}', 'f4'))
vertex = np.array(points, dtype=dtype_list)
el = PlyElement.describe(vertex, 'vertex', comments=['vertices'])
PlyData([el]).write(ply_path)
if __name__ == '__main__':
print("hello world")