宇树Unitree G1机器人摄像机获取

📅 2026/7/22 14:38:52 👁️ 阅读次数 📝 编程学习
宇树Unitree G1机器人摄像机获取

选择librealscense获取摄像头数据
编译命令:

gitclone https://github.com/IntelRealSense/librealsense.gitcdlibrealsensemkdirbuild&&cdbuild cmake..-DBUILD_EXAMPLES=ONmake-j$(nproc)sudomakeinstall

如果不需要可视化界面可以

cmake..-DBUILD_EXAMPLES=OFF-DBUILD_GRAPHICAL_EXAMPLES=OFF

(可视化界面的编译非常慢)
获取RGB和深度图像的python代码:

importpyrealsense2asrsimportnumpyasnpimportcv2 pipeline=rs.pipeline()config=rs.config()config.enable_stream(rs.stream.color,640,480,rs.format.bgr8,30)config.enable_stream(rs.stream.depth,640,480,rs.format.z16,30)profile=pipeline.start()try:whileTrue:frames=pipeline.wait_for_frames()color_frame=frames.get_color_frame()depth_frame=frames.get_depth_frame()ifnotcolor_frameornotdepth_frame:continuecolor_image=np.asanyarray(color_frame.get_data())depth_image=np.asanyarray(depth_frame.get_data())save_path1='rgb.png'save_path2='depth.png'cv2.imwrite(save_path1,color_image)cv2.imwrite(save_path2,depth_image)break# cv2.imshow('G1 RGB', color_image)#if cv2.waitKey(1) == 27:# breakfinally:pipeline.stop()

获取点云数据的python代码:

importpyrealsense2asrs pipeline=rs.pipeline()config=rs.config()config.enable_stream(rs.stream.depth,640,480,rs.format.z16,30)config.enable_stream(rs.stream.color,640,480,rs.format.bgr8,30)profile=pipeline.start()align=rs.align(rs.stream.color)pc=rs.pointcloud()try:for_inrange(30):# 等自动曝光稳定pipeline.wait_for_frames()frames=pipeline.wait_for_frames()# 如果用了对齐,先 align 再取帧align=rs.align(rs.stream.color)aligned_frames=align.process(frames)depth_frame=aligned_frames.get_depth_frame()color_frame=aligned_frames.get_color_frame()ifnotdepth_frameornotcolor_frame:raiseRuntimeError("Depth or Color frame is missing!")# ✅ 关键:显式转换为 video_framecolor_vframe=color_frame.as_video_frame()pc=rs.pointcloud()pc.map_to(color_vframe)# ✅ 传 video_frame,不再报类型错points=pc.calculate(depth_frame)# 保存 PLYpoints.export_to_ply("output.ply",color_vframe)exceptExceptionase:print(e)finally:pipeline.stop()