PCL:python pcl解碼RGB- point_cloud2.read_points rgb

參考: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

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章