ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

1) If you're not too bothered about performance you can use the read_points_list method instead this returns all the points in the point cloud as a list which you can index the one you want. This won't be very efficient though so you may need to write your own code to access individual values directly from the raw data of the point cloud.

points = point_cloud2.read_points_list(ptcloud_data)
print points[50]

However this function internally will loop through all points to make the list. You can access the point directly but you'll have to use the point_step, row_step and data members directly then decode the type of point used in your point cloud. I'm not sure if any easier method than this exists unfortunately.

2) The points in structured point cloud like will be ordered using Row-Major ordering, so you can calculate the index of a particular pixel using the formula below:

PointIndex = PixelX + ( PixelY * CloudWidth)

You can also find the width and height of this structured cloud from the PointCloud2 message itself by accessing the width and height members. I recommend you use this approach so your code will be compatible with all structured point cloud messages.

Hope this helps.

1) If you're not too bothered about performance you can use the read_points_list method instead this returns all the points in the point cloud as a list which you can index the one you want. This won't be very efficient though so you may need to write your own code to access individual values directly from the raw data of the point cloud.

points = point_cloud2.read_points_list(ptcloud_data)
print points[50]

However this function internally will loop through all points to make the list. You can access the point directly but you'll have to use the point_step, row_step and data members directly then decode the type of point used in your point cloud. I'm not sure if any easier method than this exists unfortunately.

2) The points in structured point cloud like will be ordered using Row-Major ordering, so you can calculate the index of a particular pixel using the formula below:

PointIndex = PixelX + ( PixelY * CloudWidth)

You can also find the width and height of this structured cloud from the PointCloud2 message itself by accessing the width and height members. I recommend you use this approach so your code will be compatible with all structured point cloud messages.

Hope this helps.

Update:

In order to extract the point data from the data buffer you'll want to use the python struct.unpack_from function, this with convert the byte buffer into the actual point data. If you just want your code to work with float 32 XYZ type point clouds then its fairly simple to hard code the structure in. The full PointCloud2 decoder can detect and work with all the different point types, but that is probably more complex than necessary in your case.

The code will look something like this:

index = (row*ptcloud_data.row_step) + (col*ptcloud_data.point_step)
(X, Y, Z) = struct.unpack_from('fff', ptcloud_data.data, offset=index)

Hope this helps.