Numpy tobytes方法非常慢



我正在尝试将PointCloud2消息发布到ROS2网络。但是从保存我的点的numpy数组到我插入到消息中的字节数据的转换每一帧几乎需要2秒。

这是我的代码,罪魁祸首是points.tobytes()方法。对于一个形状为(1024,768,6)的数组,它需要大约2秒的时间,这似乎有点多了。
def publish_points(self, points):
msg = PointCloud2()
msg.header.frame_id = 'lidar_link'
msg.width = WIDTH
msg.height = HEIGHT
ros_dtype = PointField.FLOAT32
dtype = np.float32
itemsize = np.dtype(dtype).itemsize
msg.fields = [PointField(
name=n, offset=i * itemsize, datatype=ros_dtype, count=1)
for i, n in enumerate('xyzrgb')]
msg.data = points.tobytes()
msg.is_dense = False
msg.is_bigendian = False
msg.point_step = 6 * itemsize
msg.row_step = 6 * itemsize * WIDTH
msg.header.stamp = self.get_clock().now().to_msg()
self.publisher_.publish(msg)

有没有其他方法可以替代这个方法?

当我认为是.tobytes()调用是缓慢的时候,我错了。它实际上是对PointCloud2消息的data属性的赋值。我设法通过分配私有属性_data来绕过它。

相关内容

最新更新