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?

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 close merge delete

## Comments

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);
( 2011-02-27 17:24:13 -0500 )edit

## 2 Answers

Sort by ยป oldest newest most voted

The sample code on http://www.ros.org/wiki/rviz/DisplayTypes/PointCloud 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]
more

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: https://kforge.ros.org/openni/openni_ros/file/5414d278b8a5/openni_camera/src/openni_nodelet.cpp

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;
more

## Stats

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

Seen: 2,403 times

Last updated: Mar 17 '11