参考:https://answers.ros.org/question/344096/subscribe-pointcloud-and-convert-it-to-numpy-in-python/
rgb在C++中的解码方式为:
unsigned long rgb = *reinterpret_cast<int*>(&cloud->points[i].rgb);
int r = (rgb >> 16) & 0x0000ff;
int g = (rgb >> 8) & 0x0000ff;
int b = (rgb) & 0x0000ff;
当然可以直接使用 data.r 方式获取,编码简单,速度稍慢一些。
python代码段:
test = x[3]
# cast float32 to int so that bitwise operations are possible
s = struct.pack('>f' ,test)
i = struct.unpack('>l',s)[0]
# you can get back the float value by the inverse operations
pack = ctypes.c_uint32(i).value
r = (pack & 0x00FF0000)>> 16
g = (pack & 0x0000FF00)>> 8
b = (pack & 0x000000FF)
# prints r,g,b values in the 0-255 range
# x,y,z can be retrieved from the x[0],x[1],x[2]
xyz = np.append(xyz,[[x[0],x[1],x[2]]], axis = 0)
rgb = np.append(rgb,[[r,g,b]], axis = 0)
可以解析出rgb