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

git clone https://github.com/IntelRealSense/librealsense.git
cd librealsense
mkdir build && cd build
cmake .. -DBUILD_EXAMPLES=ON
make -j$(nproc)
sudo make install

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

cmake .. -DBUILD_EXAMPLES=OFF -DBUILD_GRAPHICAL_EXAMPLES=OFF

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

import pyrealsense2 as rs
import numpy as np
import cv2

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:
    while True:
        frames = pipeline.wait_for_frames()
        color_frame = frames.get_color_frame()
        depth_frame = frames.get_depth_frame()
        if not color_frame or not depth_frame:
            continue
        color_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:
        #    break
finally:
    pipeline.stop()

获取点云数据的python代码:

import pyrealsense2 as rs

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 _ in range(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()
    if not depth_frame or not color_frame:
        raise RuntimeError("Depth or Color frame is missing!")
    # ✅ 关键:显式转换为 video_frame
    color_vframe = color_frame.as_video_frame()
    pc = rs.pointcloud()
    pc.map_to(color_vframe)               # ✅ 传 video_frame,不再报类型错
    points = pc.calculate(depth_frame)
    # 保存 PLY
    points.export_to_ply("output.ply", color_vframe)

except Exception as e:
    print(e)
finally:
    pipeline.stop()
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐