Tang Tang - 1 month ago 6
Python Question

How can I interpret a float32 as four uint8s in numpy?

I tried to use python to read points cloud data files, which with extension ".pcd". They store RGBA color information with just one numpy float32.
How can i convert the

float32
data type into 4
uint8
numbers in python ?
For example, what is the rgb value hidden in number 4.51073351e-39 ?

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)


Here is the link to download the dataset: http://rgbd-dataset.cs.washington.edu/dataset/rgbd-dataset_pcd_ascii/apple_1.tar

And i used pypcd to read the file: here is the pypcd github link:https://github.com/dimatura/pypcd

Answer

I think you want pc.pc_data["rgb"].view((np.uint8, 4)), which for your example gives:

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)