python :open 3d 读取rosbag的激光雷达的包

import rospy
import cv2
import numpy as np
import math

from sensor_msgs.msg import PointCloud2
from sensor_msgs.msg import PointCloud
from sensor_msgs import point_cloud2
import sensor_msgs.point_cloud2 as pc2
import message_filters
import open3d as o3d
import numpy as np
import torch
import geometry_msgs

# from open3d import *

t1=[]
t2=[]
def callback(data_hf,data_fl,data_lf):
    # 设置画布为600*600像素
    # pointcloud1 = PointCloud()
    # pointcloud1.header.stamp = rospy.Time().now()
    # pointcloud1.header.frame_id = "pointcloud1"
    # t1.append(data_hf.header.stamp)
    # for p in pc2.read_points(data_hf, field_names=("x", "y", "z"), skip_nans=True):
    #     val_point = geometry_msgs.msg.Point32()#相当于sensor_msg.msg::PointCloud.points
    #     (val_point.x, val_point.y, val_point.z) = (p[0], p[1], p[2])#p[0~3]分别为x,y,z坐标
    #     pointcloud1.points.append(val_point)
    # intensity_array = data_process_intensity(filepath + file_ply)

    gen = point_cloud2.read_points(data_hf, field_names=("x", "y", "z"), skip_nans=True)
    print(type(gen)) #   
    points=[]
    for p in gen:
        points.append(p)
        #print(list(p))
    # for p in gen:
    #   print (" x : %.3f  y: %.3f  z: %.3f" %(p[0],p[1],p[2]))

    #points = np.random.rand(10000, 3)
    #print(len(points),points.ndim)
    point_cloud = o3d.geometry.PointCloud()
    point_cloud.points = o3d.utility.Vector3dVector(points)
    o3d.visualization.draw_geometries([point_cloud])
    
# 话题监听函数,其实主要是一个初始化过程
# 初始化节点,'laser_listener' 挺重要的,是节点(node)名称,一个py文件只能有一个。
# 代表开启一个进程!匿名参数也默认是这个
# 订阅的语句。先是你需要订阅的话题要对,然后是数据类型,然后是回调函数名字
# 最后的队列很重要,不管发布还是订阅,都有queue_size参数;
# 如果默认的话,发布的频率远高于你处理图片的速度,因此根本无法实时的显示
# 所以需要换为1,只接收最新的一个消息,其他的都丢了不管~

if __name__ == "__main__":
    
    rospy.init_node('listener', anonymous=True)
    sub_hf = message_filters.Subscriber("/hesai/front_high", PointCloud2)
    # sub = message_filters.Subscriber("/uv_array", Float32MultiArray, queue_size=700000)   # 获取c++输出的数组
    sub_fl = message_filters.Subscriber("/hesai/front_low", PointCloud2)
    sub_lf = message_filters.Subscriber("/hesai/left_front", PointCloud2)

    # print(1)  一般情况下还是使用header.stamp,默认allow_headerless=False
    ts = message_filters.ApproximateTimeSynchronizer([sub_hf, sub_fl,sub_lf], 1, 1, allow_headerless=True) #False
    ts.registerCallback(callback)
    # rospy.Subscriber("/hesai/front_high", PointCloud2,callback,queue_size = 1)
    # rospy.Subscriber("/hesai/front_low", PointCloud2,callback1,queue_size = 1)
    rospy.spin()

你可能感兴趣的:(ubuntu,ubuntu)