retrieve rgb from kinect pointcloud

Hello! The pointcloud published at camera/rgb/points is sensed to have the rgb at the fourth float

rostopic echo /camera/rgb/points/fields
-
name: x
offset: 0
datatype: 7
count: 1
-
name: y
offset: 4
datatype: 7
count: 1
-
name: z
offset: 8
datatype: 7
count: 1
-
name: rgb
offset: 16
datatype: 7
count: 1
---

However, when storing the float with

float *pstep2 = (float*)&msg->data[(kk) * msg->point_step];
xi[kk] = pstep2; //x
yi[kk] = pstep2; //y
zi[kk] = pstep2; //z
ci[kk] = pstep2; //rgb

and printing out the float shows how all of them are set to zero.

How can we retrieve the associated rgb values for each 3d point of the pointcloud output by the kinect?

edit retag close merge delete

Sort by » oldest newest most voted

Yes, we knew that, but the float should be non-zero anyway. Actually using the union

typedef union
{
struct /*anonymous*/
{
unsigned char Blue;
unsigned char Green;
unsigned char Red;
unsigned char Alpha;
};
float float_value;
long long_value;
} RGBValue;

returned binary invalid types. But we've solved it converting it first to PointXYZRGB

pcl::PointCloud<pcl::PointXYZRGB>::iterator pt_iter = this->pcl_xyzrgb.begin();
for (int rr = 0; rr < (int)pcl_xyzrgb.height; ++rr) {
for (int cc = 0; cc < (int)pcl_xyzrgb.width; ++cc, ++pt_iter) {
pcl::PointXYZRGB &pt = *pt_iter;
RGBValue color;
color.float_value= pt.rgb;
matlab_xyzrgb << rr << " " << cc << " " << pt.x*1000 << " " << pt.y*1000 << " " << pt.z*1000 << " " << (int)color.Red << " " << (int)color.Green << " " << (int)color.Blue << std::endl;
}
}

The code above works.

more The rgb value is stored in just one single float. You can, for instance, use a function like this to decode it:

void getRGB(float rgb_encoded, unsigned char &r, unsigned char &g, unsigned char &b)
{
const unsigned char *color = reinterpret_cast<const unsigned char *>(&rgb_encoded);
r = color;
g = color;
b = color;
}

When passing *pstep2 of your example as first parameter, you should get correct rgb values.

more