How to convert PointCloud2 fields from RGBA to RGB

asked 2023-05-22 20:30:34 -0500

iHany gravatar image

Hi, I have pcd files and when I publish PointCloud2 using pcl_ros as

rosrun pcl_ros pcd_to_pointcloud my_file.pcd 0.1

It has the field of rgba, not rgb.

How can I convert this field to rgb?

I'd like to re-publish this as PointCloudXYZRGB.

edit retag flag offensive close merge delete

Comments

In my case, this works in my case.

My code snippet looks like (not a working example though):

def point_rgba_to_rgb(self, p):
    x, y, z, rgba = p
    r = (rgba >> 16) & 0x0000ff
    g = (rgba >> 8) & 0x0000ff
    b = (rgba) & 0x0000ff
    a = 255
    rgb = struct.unpack('I', struct.pack('BBBB', b, g, r, a))[0]
    return x, y, z, rgb

def callback(self, pcd_msg):
    print("Preprocessing color image and point cloud...")
    _fields = ("x", "y", "z", "rgba")
    _pcd = [self.point_rgba_to_rgb(p) for p in read_points(pcd_msg, field_names=_fields)]
    fields = [
        PointField('x', 0, PointField.FLOAT32, 1),
        PointField('y', 4, PointField.FLOAT32, 1),
        PointField('z', 8, PointField.FLOAT32, 1),
        PointField('rgb', 12, PointField.UINT32, 1),
    ]
    pcd_new = create_cloud(pcd_msg.he

ader, fields, _pcd)

iHany gravatar image iHany  ( 2023-05-23 02:43:29 -0500 )edit