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

Function for unpacking RGB point field from point in point cloud?

asked 2011-02-18 06:32:27 -0600

mattbell gravatar image

updated 2011-03-18 04:54:49 -0600

kwc gravatar image

I found an example in the codebase that uses some messy bit-twiddling to get the individual r,g,b fields out of a packed rgb float. Is there a nice utility function for doing this?

(I'm trying to avoid unnecessary copy-pasting)

edit retag flag offensive close merge delete


Here's what I ended up doing: uint32_t rgb_val_; memcpy(&rgb_val_, &(cloud_rgb->points[i].rgb), sizeof(float)); uint8_t r_ = (uint8_t)((rgb_val_ >> 16) & 0x000000ff); uint8_t g_ = (uint8_t)((rgb_val_ >> 8) & 0x000000ff); uint8_t b_ = (uint8_t)((rgb_val_) & 0x000000ff);
mattbell gravatar image mattbell  ( 2011-02-27 17:24:13 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2011-02-19 10:19:17 -0600

David Lu gravatar image

The sample code on provides two ways to do the reverse. Shouldn't be too hard to flip it around.

In C++,

int rgb = 0xff0000; 
float float_rgb = *reinterpret_cast<float*>(&rgb);

In Python,

float_rgb = struct.unpack('f', struct.pack('i', 0xff0000))[0]
edit flag offensive delete link more

answered 2011-03-17 10:42:54 -0600

duststorm gravatar image

updated 2011-03-17 10:48:56 -0600

I don't know of anything like that in the ros pcl code itself, but maybe you can use the openni_camera::RGBValue union. (you can find it here at line 55:

As an example of how to use it:

pcl::PointXYZRGB pt = ...

openni_camera::RGBValue color = (openni_camera::RGBValue) pt.rgb;

// Fill in color 
color.Red   = (const uint8_t) 0xFF;
color.Green = (const uint8_t) 0x00;
color.Blue  = (const uint8_t) 0x00;
color.Alpha = 0; 
float float_rgb = color.float_value;

// Or the other way around
color.float_value = 0xFF0000;
unsigned char red = color.Red;
unsigned char green = color.Green;
unsigned char blue = color.Blue;
edit flag offensive delete link more

Question Tools

1 follower


Asked: 2011-02-18 06:32:27 -0600

Seen: 2,350 times

Last updated: Mar 17 '11