-
Notifications
You must be signed in to change notification settings - Fork 2
/
open3d_numpy.py
executable file
·38 lines (31 loc) · 1.29 KB
/
open3d_numpy.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
# examples/Python/Basic/working_with_numpy.py
import copy
import numpy as np
import open3d as o3d
if __name__ == "__main__":
# generate some neat n times 3 matrix using a variant of sync function
x = np.linspace(-3, 3, 401)
mesh_x, mesh_y = np.meshgrid(x, x)
z = np.sinc((np.power(mesh_x, 2) + np.power(mesh_y, 2)))
z_norm = (z - z.min()) / (z.max() - z.min())
xyz = np.zeros((np.size(mesh_x), 3))
xyz[:, 0] = np.reshape(mesh_x, -1)
xyz[:, 1] = np.reshape(mesh_y, -1)
xyz[:, 2] = np.reshape(z_norm, -1)
print('xyz')
print(xyz)
# Pass xyz to Open3D.o3d.geometry.PointCloud and visualize
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
o3d.io.write_point_cloud("../sync.ply", pcd)
# Load saved point cloud and visualize it
pcd_load = o3d.io.read_point_cloud("../sync.ply")
o3d.visualization.draw_geometries([pcd_load])
# convert Open3D.o3d.geometry.PointCloud to numpy array
xyz_load = np.asarray(pcd_load.feature_points)
print('xyz_load')
print(xyz_load)
# save z_norm as an image (change [0,1] range to [0,255] range with uint8 type)
img = o3d.geometry.Image((z_norm * 255).astype(np.uint8))
# o3d.io.write_image("../../TestData/sync.png", img)
o3d.visualization.draw_geometries([img])