'how to import all the points from a .pcd file into a 2d python array

How do you import all the 3d points from a file named edge_cloud.pcd and put them into an array? I want the array to be in the format

array=[[xvalue1,yvalue1,zvalue1],[xvalue2,yvalue2,zvalue2]] 


Solution 1:[1]

Tested with Python 3.7.3, numpy 1.16.2, and open3d 0.7.0.0:

import numpy as np 
import open3d as o3d

pcd = o3d.io.read_point_cloud("C:\\Users\\Username\\Source\\pointcloud\\bunny.pcd")
out_arr = np.asarray(pcd.points)  
print("output array from input list : ", out_arr)  

Output:

output array from input list :  
[[ 0.0054216  0.11349    0.040749 ]
 [-0.0017447  0.11425    0.041273 ]
 [-0.010661   0.11338    0.040916 ]
 ...
 [-0.064992   0.17802   -0.054645 ]
 [-0.069935   0.17983   -0.051988 ]
 [-0.07793    0.17516   -0.0444   ]]

Input PCD file:

https://github.com/PointCloudLibrary/pcl/blob/master/test/bunny.pcd

Solution 2:[2]

Tested with python 3.8, access pcd files using pypcd.

Install using below command

    pip install pypcd

If this doesnt install, try below command

    python -m pip install --user git+https://github.com/DanielPollithy/pypcd.git

After the installation you can load pcd to numpy arrays using below code.

    from pypcd import pypcd
    pc = pypcd.PointCloud.from_path("demo.pcd")
    pc_data = pc.pc_data
    pc_array = np.array([pc_data["x"], pc_data["y"], pc_data["z"]], dtype=np.float32)

https://github.com/dimatura/pypcd

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 phoenix
Solution 2 Gayathri Devi