PRBonn/lidar-bonnetal

Converting ROS PointCloud2 object to KITTI .bin file

jonathanslee4 opened this issue · 13 comments

This is more of a ROS/KITTI question, but does anyone know how to convert a ROS PointCloud2 object to a KITTI-style .bin file? I'm collecting PointCloud2 scan objects, but don't know how to convert them to the KITTI .bin format in order to use this lidar-bonnetal code for segmentation.

A .bin file contains 4*N values, where N is the number of points and 4 because it contains x, y, z, reflection in that order. So you have to extract these values from your pointcloud, create an array from them and save it in a binary file using a float32 format.
Sample python code:

  pc = pypcd.PointCloud.from_msg(msg)
  x = pc.pc_data['x']
  y = pc.pc_data['y']
  z = pc.pc_data['z']
  intensity = pc.pc_data['intensity']
  arr = np.zeros(x.shape[0] + y.shape[0] + z.shape[0] + intensity.shape[0], dtype=np.float32)
  arr[::4] = x
  arr[1::4] = y
  arr[2::4] = z
  arr[3::4] = intensity
   arr.astype('float32').tofile('filename.bin')

Thanks for resolving this issue, @kosmastsk.

I would also say, read the point cloud with ROS, write it as a binary file, 4 floats each point. But @kosmastsk provides actually nice code that should basically solve your problem.

Thank you so much for the help!

Hello @jonathanslee4 , @kosmastsk I have the error "no module named pypcd" when I run python script. I have install pypcd with pip (python 3). Can you help me please ?

@SofianeB-03 you have to try re-install pypcd in your system, in a different environment or something. But there are also other ways to "read" a pointcloud, apart from pypcd, this was just an example

@kosmastsk first thank you for your reply. Okay I will probably try to re-install pypcd in a different environnement. Also I found pcl_ros package to convert a point-cloud2 message to PCD format but I have intensity and also reflectivity values in the output PCD file. What is the 4th component for the .bin KITTI: Intensity or reflectivity ?

@SofianeB-03 intensity and reflectivity is exactly the same thing in LiDAR data. In KITTI data as far as I remember these values are in the scale [0,1]

Okay I note this information. Thank you very much to take the time to answer me.

@kosmastsk @jonathanslee4 i have rosbag collected using ros2 galactic, can you tell me a detailed way to create .bin files from rosbag.
i saw above code but its giving
NotImplementedError('ROS sensor_msgs not found')

i am new to ros and pointcloud. any help is appreciated. Thank you.

The above response is for ROS1, I'm sorry but I have no such thing for ROS2 right not.

@kosmastsk i really appreciate the response.
thank you very much

if i use ros neotic can you share simple steps to convert rosbag into kitti format .bin file
apologies for such basic question, but im quiet confused.

Thank you in advance