Open3D对接Intel RealSense L515相机与PLY点云保存技术咨询
解决Intel RealSense L515与Open3D的在线点云采集及保存问题
1. 相机与Python的连接
Intel RealSense L515的Python交互依赖官方的pyrealsense2库,搭配Open3D即可实现实时点云采集。先安装必要依赖:
pip install pyrealsense2 open3d
初始化相机时,需配置深度流与彩色流的参数(L515支持1280x720分辨率、30fps的主流配置),通过rs.pipeline启动数据流,逐帧获取深度与彩色图像。
2. 相机输入转换为.ply格式
Open3D提供了直接从深度图生成点云的API,只需将RealSense获取的帧数据转换为Open3D兼容的格式,即可调用write_point_cloud方法保存为PLY文件。核心步骤包括:
- 对齐深度帧与彩色帧,确保点云颜色与空间位置匹配
- 利用相机内参将深度图转换为三维点云
- 为点云赋予彩色信息后保存
完整在线采集与保存代码
import pyrealsense2 as rs import open3d as o3d import numpy as np # 初始化RealSense相机流水线 pipeline = rs.pipeline() config = rs.config() # 配置深度与彩色流参数(适配L515) config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30) config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30) # 启动相机流 profile = pipeline.start(config) # 获取深度传感器缩放比例(用于将深度值转换为真实距离) depth_sensor = profile.get_device().first_depth_sensor() depth_scale = depth_sensor.get_depth_scale() # 对齐深度帧到彩色帧,保证点云颜色与位置对应 align_to = rs.stream.color align = rs.align(align_to) try: # 初始化Open3D可视化窗口 vis = o3d.visualization.Visualizer() vis.create_window(window_name="L515 Real-time Point Cloud") point_cloud = o3d.geometry.PointCloud() is_first_frame = True while True: # 等待获取相机帧 frames = pipeline.wait_for_frames() aligned_frames = align.process(frames) aligned_depth_frame = aligned_frames.get_depth_frame() color_frame = aligned_frames.get_color_frame() if not aligned_depth_frame or not color_frame: continue # 将帧数据转换为numpy数组 depth_image = np.asanyarray(aligned_depth_frame.get_data()) color_image = np.asanyarray(color_frame.get_data()) # 获取彩色相机内参,用于深度图转点云 intrinsics = profile.get_stream(rs.stream.color).as_video_stream_profile().get_intrinsics() o3d_intrinsics = o3d.camera.PinholeCameraIntrinsic( intrinsics.width, intrinsics.height, intrinsics.fx, intrinsics.fy, intrinsics.ppx, intrinsics.ppy ) # 从深度图生成点云 pcd = o3d.geometry.PointCloud.create_from_depth_image( o3d.geometry.Image(depth_image), o3d_intrinsics, depth_scale=depth_scale, convert_rgb_to_intensity=False ) # 为点云添加彩色信息 pcd.colors = o3d.utility.Vector3dVector(color_image.astype(np.float32) / 255.0) # 更新可视化窗口 if is_first_frame: vis.add_geometry(pcd) is_first_frame = False else: vis.update_geometry(pcd) vis.poll_events() vis.update_renderer() # 按键控制:按's'保存点云为PLY,按'q'退出 key_events = vis.get_window_key_events() if ord('s') in key_events: save_path = "realtime_pointcloud.ply" o3d.io.write_point_cloud(save_path, pcd) print(f"点云已保存至 {save_path}") if ord('q') in key_events: break finally: # 停止相机流并关闭可视化窗口 pipeline.stop() vis.destroy_window()
代码说明
- 相机对齐:通过
rs.align将深度帧与彩色帧对齐,避免点云颜色错位 - 内参转换:将RealSense的相机内参转换为Open3D兼容格式,保证点云坐标精度
- 交互控制:实时可视化过程中,按
s键保存当前点云为PLY文件,按q键退出程序
内容的提问来源于stack exchange,提问作者SAGAR TYAGI
相关产品推荐
相关产品推荐

