Module my_ros_utils.cloud_conversion
ROS/Open3D pointcloud converter
This script can be imported as a module and contains the following public methods:
Functions
def pointcloud_numpy_to_open3d(xyz, rgb=None)-
Convert two
np.ndarrays(one with cartesian data and one with RGB values) into an Open3D pointcloud object.Args
xyz:np.ndarray- An array of shape
(…, 3)with the cartesian coordinates of the points in the cloud. The order of the point values is assumed to match the one inrgb. rgb:np.ndarray, optional- An array of shape
(…, 3)with the RGB values of the points in the cloud. The order of the point values is assumed to match the one inxyz.
Returns
open3d.geometry.PointCloud- An Open3D PointCloud object with the same geometry data as
xyzand, if any, the same color information asrgb.
def pointcloud_numpy_to_ros(xyz, rgb=None, merge_rgb=True)-
Convert an Open3D PointCloud object into a ROS PointCloud2 message.
Args
xyz:np.ndarray- An array of shape (…, 3) with the cartesian coordinates of the points in the cloud. The order of the point values is assumed to match the one in
rgb. rgb:np.ndarray, optional- An array of shape (…, 3) with the RGB values of the points in the cloud. The order of the point values is assumed to match the one in
xyz. merge_rgb:bool, optional- If
True, ther,gandb(typenp.uint8) fields are merged into a singlergb(typenp.float32) fields. Defaults toTrue.
Returns
sensor_msgs.PointCloud2- A ROS message with the same data of the input Open3D cloud.
def pointcloud_open3d_to_numpy(cloud, shape)-
Convert an Open3D PointCloud object into two
np.ndarrays(one with cartesian data and one with RGB values) into an Open3D pointcloud object.Args
cloud:open3d.geometry.PointCloud- An Open3D PointCloud object.
shape:tuple, optional- A tuple with two
intelements, specifiying the 2D structure of the point cloud. Defaults to None.
Raises
ValueError- If the height and width value given with
shapedo not match the number of points in the cloud.
Returns
A tuple containing a first
np.ndarray(XYZ coordinates of the points in the cloud), and a secondnd.array(RGB values). If the input cloud has no objects, the second item will be an array of zeros of proper shape. def pointcloud_open3d_to_ros(cloud, shape=None, merge_rgb=True)-
Convert an Open3D PointCloud object into a ROS PointCloud2 message.
Args
cloud:open3d.geometry.PointCloud- An Open3D PointCloud object.
- shape ((int, int), optional): A tuple with two elements, specifiying the 2D structure of the point cloud. Defaults to None.
merge_rgb:bool, optional- If True, the
np.uint8R, G and B values are merged into a singlenp.float32value. Defaults to True.
Returns
sensor_msgs.PointCloud2- A ROS PointCloud2 message, with the same information of the input cloud.
def pointcloud_ros_to_numpy(msg)-
Convert a ROS PointCloud2 message into two np.ndarrays (one for XYZ coordinates, and one for RGB values). From: https://answers.ros.org/question/321829/color-problems-extracting-rgb-from-pointcloud2/
Args
msg:sensor_msgs.PointCloud2- A ROS PointCloud2 message.
Returns
A tuple containing a first
np.ndarray(XYZ coordinates of the points in the cloud), a secondnp.ndarray(RGB values), and a tuple ((int, int)shape of the input cloud). The second item (RGB values) can be None if the input PointCloud2 has no color information. def pointcloud_ros_to_open3d(msg)-
Convert a ROS PointCloud2 message into a Open3D PointCloud object.
Args
msg:sensor_msgs.PointCloud2- A ROS PointCloud2 message.
Returns
open3d.geometry.PointCloud- An Open3D PointCloud object with the same data of the input message.