eric-wieser/ros_numpy

PointCloud2 -> np.array with shape=(n_points, point_dimensions)?

v4hn opened this issue · 3 comments

v4hn commented

Hi there, this is quite a handy package you provide here, thank you!

Currently, I need to convert PointCloud2 data to numpy generating an ndarray with shape=(nr_of_points, 3), that is to say an ndarray with the second dimension having one entry for each geometrical dimension. In general of course the pointcloud could contain more fields.

It looks like the package here converts the PointCloud2 only into a structured ndarray with shape (nr_of_points,) and tuples as entries. Is my requested use-case also supported by the library?

If not, how do you propose to do this conversion efficiently.
Currently, I'm working with

cloud_tmp = ros_numpy.numpify(recognized_object.point_cloud)
cloud_np = np.zeros((cloud_tmp.shape[0], 3), dtype= np.float32)
cloud_np[:,0] = map(lambda x: x[0], cloud_tmp[:])
cloud_np[:,1] = map(lambda x: x[1], cloud_tmp[:])
cloud_np[:,2] = map(lambda x: x[2], cloud_tmp[:])

but that looks rather suboptimal...

tpet commented

Something like this should work: np.stack(cloud_tmp[f] for f in ('x', 'y', 'z')).T
You may need to call cloud_tmp = cloud_tmp.ravel() first if the cloud is two-dimensional.

v4hn commented

Thank you, that works. I wasn't aware structured arrays could be indexed like that. 👍

@v4hn could you give instructions on how to run it?