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

In-Place Modification of PointCloud2 message?

asked 2019-06-04 09:22:25 -0500

moooeeeep gravatar image

I am subscribing to a sensor_msgs/PointCloud2 message (points with: x,y,z,intensity) and I would like to change the intensity value based on some heuristic in my code.

In principle the code looks like this:

import sensor_msgs.point_cloud2

def handle_cloud(self, msg):
    field_names = ("x", "y", "z", "intensity")
    reader = sensor_msgs.point_cloud2.read_points(msg, field_names)
    for p in reader:
        new_intensity = some_code(p)
        # assign new intensity to point
        p[3] = new_intensity # TODO: is this possible somehow?
    # republish with new intensity values

Can I do this in-place, similarly to the code above? Or do I need to build a new message, using sensor_msgs.point_cloud2.create_cloud() ?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2019-06-04 12:10:34 -0500

This is possible, although the the PointCloud2 messages are not designed to be used this way. It may well take less code to create a new PointCloud2 message, however if you need to work on the message in-place for performance reasons then there is a way. Just to clarify if you're not doing this for performance reasons then I would recommend extracting the cloud from the message, modifying it and creating a new message.

The PointCloud2 message type stores the actual point data in it's data member as a byte array. it is possible to work out exactly where within this array each value lies and its type and then overwrite them in place.

The data structure of the PointCloud2 message is defined by an array of PointField Messages and the point_step and row_step byte spacing. If you're only going to work with a single type of point cloud you could ignore this an hard code the solution, but you'll need to look at it at least once to know what the structure is in the first place.

If you still want to try and do this and need some more pointers, let me know and I can give you some more detailed info.

edit flag offensive delete link more


To be clear, this would involve adapting the code from here: and do the struct unpacking and packing in my own code? I think I don't need it this badly. Thanks for the clarification!

moooeeeep gravatar image moooeeeep  ( 2019-06-05 01:59:18 -0500 )edit

Yes that's what you'd need to do. Possible but far from easy! No problem

PeteBlackerThe3rd gravatar image PeteBlackerThe3rd  ( 2019-06-05 06:29:23 -0500 )edit

Question Tools



Asked: 2019-06-04 09:22:25 -0500

Seen: 657 times

Last updated: Jun 04 '19