Skip to content

Instantly share code, notes, and snippets.

@HViktorTsoi
Created June 19, 2021 05:33
Show Gist options
  • Save HViktorTsoi/c299f3f3d65dde5f02a7c96316c43276 to your computer and use it in GitHub Desktop.
Save HViktorTsoi/c299f3f3d65dde5f02a7c96316c43276 to your computer and use it in GitHub Desktop.
Convert numpy array to PointCloud2 msg
import numpy as np
import ros_numpy
from sensor_msgs.msg import PointCloud2
"""
pc: 2d numpy array of shape (N, 4)
"""
pc_array = np.zeros(len(pc), dtype=[
('x', np.float32),
('y', np.float32),
('z', np.float32),
('intensity', np.float32),
])
pc_array['x'] = pc[:, 0]
pc_array['y'] = pc[:, 1]
pc_array['z'] = pc[:, 2]
pc_array['intensity'] = pc[:, 3]
pc_msg = ros_numpy.msgify(PointCloud2, pc_array, stamp=header.stamp, frame_id=header.frame_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment