I didn't see any good Python function for converting point cloud datatype between Open3D and ROS, so I made this repo.
The script lib_cloud_conversion_between_Open3D_and_ROS.py contains 2 functions:
where the ROS cloud format is indicating this: "sensor_msgs/PointCloud2.msg".
The script also contains a test case, which does such a thing:
(1) Read a open3d_cloud from .pcd file by Open3D.
(2) Convert it to ros_cloud.
(3) Publish ros_cloud to topic.
(4) Subscribe the ros_cloud from the same topic.
(5) Convert ros_cloud back to open3d_cloud.
(6) Display it and save it to .pcd file.
You can test it by:
$ rosrun open3d_ros_pointcloud_conversion lib_cloud_conversion_between_Open3D_and_ROS.py
A very useful but bad documented github repo:
https://github.com/karaage0703/open3d_ros
It (only) contains codes for converting XYZ point cloud from open3d to ros. I copied pieces of codes from it.
However, when converting cloud from ros to open3d, it writes the cloud to file and then use open3d to read file, which is slower.
In my view, its scripts and function/variable namings are not well organized. So I decided to rewrite it.
PointCloud2 message type:
http://docs.ros.org/melodic/api/sensor_msgs/html/msg/PointCloud2.html
Function of sensor_msgs.point_cloud2.create_cloud()
http://docs.ros.org/jade/api/sensor_msgs/html/namespacesensor__msgs_1_1point__cloud2.html#ad456fcf9391ad2ed2279df69572ca71d
Open3d: from numpy to open3d pointcloud
http://www.open3d.org/docs/tutorial/Basic/working_with_numpy.html#from-numpy-to-open3d-pointcloud
pcl::PointCloud<PointXYZRGB>::Ptr pcl_cloud = (some function to read in a cloud);
sensor_msgs::PointCloud2 ros_cloud;
pcl::toROSMsg(*pcl_cloud, ros_cloud);
----- Contents of PointCloud2 converted from pcl::toROSMsg and viewed by "rostopic echo" -----
header:
seq: 16
stamp:
secs: 0
nsecs: 0
frame_id: ""
height: 1
width: 1706
fields:
-
name: "x"
offset: 0
datatype: 7
count: 1
-
name: "y"
offset: 4
datatype: 7
count: 1
-
name: "z"
offset: 8
datatype: 7
count: 1
-
name: "rgb"
offset: 16
datatype: 7
count: 1
is_bigendian: False
point_step: 32
row_step: 54592
data: ...