3
$\begingroup$

In ROS1 you can convert a pointCloud2 message to an xyz array with sensor_msgs.point_cloud2.read_points().

Unfortunately, this option has not yet been adapted for ROS2.

What other options are there to obtain this.

$\endgroup$
3
  • $\begingroup$ What do you mean it hasn't been adapted for ROS2? Here's a link to the read_points method in the ROS2 Github project. Is there something not working about this? $\endgroup$ Commented Aug 31, 2022 at 17:42
  • $\begingroup$ Thank you Chuck. You made my day. Sorry I'm maybe not experienced enough to find the right links. I included in my installation and it works perfect. I use ROS via the official docker. It looks like these things are not pre-installed. Do you know why? $\endgroup$ Commented Sep 1, 2022 at 10:25
  • $\begingroup$ Happy to help, not sure what the official docker is though, or why it wouldn't have been included. $\endgroup$ Commented Sep 1, 2022 at 11:42

1 Answer 1

0
$\begingroup$

Always fun to Google a problem and find my own answers. The comment I left a few years ago (before I personally was using ROS2) just links to the documentation.

Now I'm using ROS2 and I need to read points in Python lol. If you want to use the method, then you need a slightly different import statement. Using from sensor_msgs.msg import PointCloud2 gets you the message definition, but doesn't get you the utility methods. For that, you want:

from sensor_msgs_py import point_cloud2

Now that you've got point_cloud2 you can access the point cloud helper methods. For example, if you want to read the x/y/z and reflectivity values from a pointcloud, then you can:

pc2_data = point_cloud2.read_points_list(pc2_msg, field_names=["x", "y", "z", "reflectivity"], skip_nans=True)

If you want to convert that output from a list of named tuple to a numpy array for further processing, then it's just

import numpy as np
data_array = np.array(pc2_data)

and each column corresponds to the fields you named in the read_points_list method.

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.