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

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

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

mattbell gravatar image

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

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

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

2 Answers

Sort by ยป oldest newest most voted
1

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

David Lu gravatar image

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]
edit flag offensive delete link more
0

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

duststorm gravatar image

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

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;
edit flag offensive delete link more

Question Tools

1 follower

Stats

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

Seen: 2,373 times

Last updated: Mar 17 '11