PRBonn / lidar-bonnetal

Semantic and Instance Segmentation of LiDAR point clouds for autonomous driving
http://semantic-kitti.org
MIT License
945 stars 205 forks source link

Converting ROS PointCloud2 object to KITTI .bin file #78

Closed jonathanslee4 closed 3 years ago

jonathanslee4 commented 3 years ago

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.

kosmastsk commented 3 years ago

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')
jbehley commented 3 years ago

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.

jonathanslee4 commented 3 years ago

Thank you so much for the help!

SofianeB-03 commented 3 years ago

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 ?

kosmastsk commented 3 years ago

@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

SofianeB-03 commented 3 years ago

@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 ?

kosmastsk commented 3 years ago

@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]

SofianeB-03 commented 3 years ago

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

SofianeB-03 commented 3 years ago

@kosmastsk thank you

vishaldolase-honda commented 1 year ago

@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.

kosmastsk commented 1 year ago

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

vishaldolase-honda commented 1 year ago

@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

donceykong commented 1 year ago

FOR Noetic

See my StackExchange reply