def pointcloud2_to_array(cloud_msg, squeeze=True):
''' Converts a rospy PointCloud2 message to a numpy recordarray
Reshapes the returned array to have shape (height, width), even if the height is 1.
The reason for using np.fromstring rather than struct.unpack is speed... especially
for large point clouds, this will be <much> faster.
'''
# construct a numpy record type equivalent to the point type of this cloud
dtype_list = fields_to_dtype(cloud_msg.fields, cloud_msg.point_step)
# parse the cloud into an array
cloud_arr = np.fromstring(cloud_msg.data, dtype_list)
# remove the dummy fields that were added
cloud_arr = cloud_arr[
[fname for fname, _type in dtype_list if not (fname[:len(DUMMY_FIELD_PREFIX)] == DUMMY_FIELD_PREFIX)]]
if squeeze and cloud_msg.height == 1:
return np.reshape(cloud_arr, (cloud_msg.width,))
else:
return np.reshape(cloud_arr, (cloud_msg.height, cloud_msg.width))
评论列表
文章目录