Skip to main content
 首页 » 编程设计

python之我如何将 float32 解释为 numpy 中的四个 uint8

2024年08月21日29bluestorm

我尝试用python读取点云数据文件,扩展名为“.pcd”。它们只用一个 numpy float32 存储 RGBA 颜色信息。 如何在 python 中将 float32 数据类型转换为 4 个 uint8 数字? 例如,数字 4.51073351e-39 中隐藏的 rgb 值是多少?

In [13]: pc.pc_data["rgb"] 
Out[13]:  
array([  4.51073351e-39,   4.41853788e-39,   4.87845245e-39, ..., 
         3.31220574e-39,   2.94594696e-39,   3.12997949e-39], dtype=float32) 

这是下载数据集的链接:http://rgbd-dataset.cs.washington.edu/dataset/rgbd-dataset_pcd_ascii/apple_1.tar

我使用 pypcd 读取文件:这里是 pypcd github 链接:https://github.com/dimatura/pypcd

请您参考如下方法:

我想你想要 pc.pc_data["rgb"].view((np.uint8, 4)),对于你的例子给出:

array([[23, 30, 49,  0], 
       [22, 29, 48,  0], 
       [36, 31, 53,  0], 
       ... 
       [21, 17, 36,  0], 
       [26, 20, 32,  0], 
       [28, 21, 34,  0]], dtype=uint8)