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

3

10

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]] 
Halfblood answered 28/6, 2019 at 16:0 Comment(0)
Q
10

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

Quire answered 28/6, 2019 at 17:55 Comment(2)
What if my points have 5 dimensions, like [x, y, z, intensity, time_stamp], open3d only provides format with 3,4,6 dimensions...Hephzipa
>>> import open3d as o3d Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/jhuai/.local/lib/python3.8/site-packages/open3d/__init__.py", line 9, in <module> from open3d.linux import * File "/home/jhuai/.local/lib/python3.8/site-packages/open3d/linux/__init__.py", line 7, in <module> globals().update(importlib.import_module('open3d.linux.open3d').__dict__) File "/usr/lib/python3.8/importlib/__init__.py", line 127 ImportError: /home/jhuai/.local/lib/python3.8/site-packages/open3d/linux/open3d.so: undefined symbol: _Py_ZeroStructPogy
A
1

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

Adversative answered 22/11, 2021 at 7:57 Comment(1)
>>> from pypcd import pypcd Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/home/jhuai/.local/lib/python3.8/site-packages/pypcd/pypcd.py", line 15, in <module> import cStringIO as sio ModuleNotFoundError: No module named 'cStringIO'Pogy
E
0

As an addition to the answer above I found pypcd4 (https://github.com/MapIV/pypcd4) to be generally a lot more robust and capable in terms of dealing with point clouds with multiple colorings (like rgba and an additional label color scalar feild).

The pypcd4 code that answers this question would be

from pypcd4 import PointCloud

pc = PointCloud.from_path("Path")
data = pc.numpy(("x", "y", "z"))
Eyepiece answered 2/5 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.