Article directory
Open3d, python-pcl, numpy point cloud data format conversion
NumPy to open3d.PointCloud
Reference: https://www.codenong.com/cs106756630/
Converting numpy to open3D requires the use of the Vector3dVector function, which can be directly assigned to open3d.PointCloud.points. The specific operation is as follows, assuming (x, y, z), (n_x, n_y , n_z), (r, g, b) are an n*3numpy array (these three are not necessarily all required), then for the conversion of points, normal vectors and colors, the Vector3dVector function can be used, and the specific operations are as follows:
import numpy as np
import open3D as o3d
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(xyz)
pcd.normals = o3d.utility.Vector3dVector(nxnynz)
pcd.colors = o3d.utility.Vector3dVector(rgb)
open3d.PointCloud to NumPy
import numpy as np
import open3d as o3d
# Load saved point cloud and visualize it
pcd_load = o3d.io.read_point_cloud("../xxx.ply")#或 xxx.pcd 等常见格式
# convert Open3D.o3d.geometry.PointCloud to numpy array
xyz_load = np.asarray(pcd_load.points)
o3d.visualization.draw_geometries([pcd_load])
numpy array saved as pcd file
import numpy as np
import open3d as o3d
# pointcloud_interest 为 numpy数组
# pointcloud_interest =rays_end_all.numpy()
pcd = o3d.geometry.PointCloud()
pcd.points = o3d.utility.Vector3dVector(pointcloud_interest)
# print("len(pcd.points):",len(pcd.points))#add hxz
o3d.io.write_point_cloud("/home/xx/pointcloud_interest.pcd", pcd)
python pcl point cloud to numpy
import pcl
import numpy as np
# 读取pcl 格式点云 并转换为 numpy数组
pointcloud_source = pcl.load('xx.pcd')
pointcloud_source_numpy = pointcloud_source.to_array()
# pointcloud_source_numpy = pointcloud_source.to_array()[:, :4]
numpy to python pcl point cloud
import pcl
import numpy as np
# 转换为 pcl 格式点云,输入点云 points_,形状(N,3)
if(0): # 将 double 转换为 float32_t, 可能需要
points=np.ones((points_.shape[0],3),np.float32)
for i in range(points_.shape[0]):
points[i][0]=points_[i][0]
points[i][1]=points_[i][1]
points[i][2]=points_[i][2]
pointcloud_trans = pcl.PointCloud(points)# numpy 数组转 pcl
if(1):
pointcloud_trans = pcl.PointCloud(points_)# numpy 数组转 pcl