跳到主要内容
极客日志极客日志面向AI+效率的开发者社区
首页博客我的书AI学习GitHub 精选镜像AI 生图工具UI配色美学关于
搜索内容 / 工具 / 仓库 / 镜像...⌘K搜索
注册
博客列表
PythonAI算法

Windows 11 配置 Intel RealSense D435i 深度相机及 Python 开发

Intel RealSense D435i 是一款集成 RGB 与深度信息的传感器,支持主动立体红外成像。详细记录了在 Windows 11 环境下完成 SDK 安装、Python 虚拟环境配置及驱动调试的全过程。内容涵盖基础流数据可视化、RGB 图像采集保存,以及结合 YOLOv8 进行目标检测与三维坐标计算的高级应用。通过 pyrealsense2 库实现多模态数据同步,解决了分辨率差异与时间同步问题,为 SLAM 及机器人视觉项目提供硬件接入参考。

清酒独酌发布于 2026/3/24更新于 2026/10/15114 浏览
Windows 11 配置 Intel RealSense D435i 深度相机及 Python 开发

产品简介

Intel RealSense D435i 是一款消费级深度相机,集成了 RGB 相机、两个红外相机、红外发射器以及 IMU 单元。与标准 D435 相比,D435i 增加了惯性测量单元(IMU),这也是型号后缀"i"的含义。

其核心成像原理是主动立体红外测距,而非传统的双目 RGB 视觉。这意味着它能直接输出深度图(3D 点云)和对应的 RGB 影像,构成典型的 RGB-D 传感器。在 SLAM 应用中,它支持多种模式:单目 RGB、单目 +IMU、双目红外、RGB-D 等,唯独不支持双目 RGB 模式(因红外通道为单通道)。

主要参数概览:

  • 深度技术:主动立体 IR
  • 深度分辨率 & 帧率:1280×720 @ 90FPS
  • RGB 分辨率 & 帧率:1920×1080 @ 30FPS
  • 最小测距:约 0.1m,最大范围约 10m
  • 尺寸:90mm × 25mm × 25mm

值得注意的是,深度图与 RGB 图的分辨率不同步,且 RGB 采用卷帘快门,高速运动下可能出现果冻效应。此外,由于依赖主动红外信号,该设备更适合室内或光照可控环境,室外强光下性能会受限。

SDK 安装与环境准备

1. 下载与安装

前往 Intel RealSense GitHub 发布页下载 SDK 2.0。Windows 11 与 Windows 10 通用,选择对应的 exe 安装包即可。安装过程会自动附带 Viewer 可视化工具、示例代码库及 Depth Quality Tool。

2. 基础测试

连接相机后,运行 realsense-viewer 启动可视化界面。确认 Stereo Module(深度)、RGB Module(彩色)及 Motion Module(IMU)数据均能正常显示。若画面正常,说明驱动已就绪。

在此阶段建议调整 Stereo Module 和 RGB Camera 的分辨率与 FPS,确保后续程序中的流配置与实际硬件设置一致。

Python 环境搭建与基础调用

1. 虚拟环境配置

推荐使用 Anaconda 管理环境。以 Python 3.8 为例:

conda create -n realsense_env python=3.8
conda activate realsense_env

安装必要的依赖包。OpenCV 版本需匹配,pyrealsense2 负责驱动交互,open3d 用于点云处理:

pip install opencv-python opencv-contrib-python
pip install pyrealsense2 open3d

2. 基础流可视化

初始化管道并开启深度与颜色流。注意时间同步问题,使用 wait_for_frames() 获取成对帧。

import pyrealsense2 as rs
import numpy as np
import cv2

if __name__ == "__main__":
    pipeline = rs.pipeline()
    config = rs.config()
    
    # 配置深度和颜色流:640x480, 30FPS
    config.enable_stream(rs.stream.depth, 640, 480, rs.format.z16, 30)
    config.enable_stream(rs.stream.color, 640, , rs..bgr8, )
    
    pipeline.start(config)
    
    :
         :
            frames = pipeline.wait_for_frames()
            depth_frame = frames.get_depth_frame()
            color_frame = frames.get_color_frame()
            
              depth_frame   color_frame:
                
            
            
            depth_image = np.asanyarray(depth_frame.get_data())
            color_image = np.asanyarray(color_frame.get_data())
            
            
            depth_colormap = cv2.applyColorMap(
                cv2.convertScaleAbs(depth_image, alpha=), 
                cv2.COLORMAP_JET
            )
            
            
            images = np.hstack((color_image, depth_colormap))
            
            cv2.namedWindow(, cv2.WINDOW_AUTOSIZE)
            cv2.imshow(, images)
            
            key = cv2.waitKey()
             key &  == ()  key == :
                
    :
        pipeline.stop()
480
format
30
try
while
True
if
not
or
not
continue
# 转换为 numpy 数组
# 深度图伪彩色处理
0.03
# 左右拼接显示
'RealSense'
'RealSense'
1
if
0xFF
ord
'q'
or
27
break
finally

数据采集与保存

在实际项目中,往往需要离线存储图像数据。下面是一个简单的 RGB 采集脚本,支持按快捷键保存。

import os
import cv2
import numpy as np
import pyrealsense2 as rs
import time

# 初始化 Pipeline
pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.color, 1920, 1080, rs.format.bgr8, 15)
profile = pipeline.start(config)

cv2.namedWindow('RGB Image', cv2.WINDOW_AUTOSIZE)
save_dir = 'E:/my_images/'
os.makedirs(save_dir, exist_ok=True)
counter = 0

try:
    while True:
        frames = pipeline.wait_for_frames()
        color_frame = frames.get_color_frame()
        if not color_frame:
            continue
        
        color_image = np.asanyarray(color_frame.get_data())
        cv2.imshow('RGB Image', color_image)
        
        key = cv2.waitKey(1)
        if key == ord('t'):  # 按 T 键保存
            rgb_file = os.path.join(save_dir, f'rgb_{counter:04d}.png')
            cv2.imwrite(rgb_file, color_image)
            print(f'Saved: {rgb_file}')
            counter += 1
        elif key in [27, ord('q')]:  # ESC 或 Q 退出
            break
finally:
    pipeline.stop()

进阶应用:YOLOv8 与关键点识别

1. YOLOv8 目标检测与三维坐标

将深度信息映射到检测框内,可获取目标的精确距离和空间位置。关键在于使用 rs.align 将深度流对齐到彩色流,解决视差问题。

import cv2
import pyrealsense2 as rs
import numpy as np
from ultralytics import YOLO

model = YOLO("runs/detect/train/weights/best.pt")

pipeline = rs.pipeline()
config = rs.config()
config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 848, 480, rs.format.bgr8, 30)
pipeline.start(config)

align_to = rs.stream.color
align = rs.align(align_to)

def get_aligned_images():
    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()
    
    intr = color_frame.profile.as_video_stream_profile().intrinsics
    depth_intrin = aligned_depth_frame.profile.as_video_stream_profile().intrinsics
    
    depth_image = np.asanyarray(aligned_depth_frame.get_data())
    depth_image_8bit = cv2.convertScaleAbs(depth_image, alpha=0.03)
    depth_image_3d = np.dstack((depth_image_8bit, depth_image_8bit, depth_image_8bit))
    color_image = np.asanyarray(color_frame.get_data())
    
    return intr, depth_intrin, color_image, depth_image, aligned_depth_frame

def get_3d_camera_coordinate(depth_pixel, aligned_depth_frame, depth_intrin):
    x, y = depth_pixel
    dis = aligned_depth_frame.get_distance(x, y)
    camera_coordinate = rs.rs2_deproject_pixel_to_point(depth_intrin, depth_pixel, dis)
    return dis, camera_coordinate

try:
    while True:
        intr, depth_intrin, color_image, depth_image, aligned_depth_frame = get_aligned_images()
        if not depth_image.any() or not color_image.any():
            continue
        
        results = model.predict(color_image, conf=0.5)
        annotated_frame = results[0].plot()
        detected_boxes = results[0].boxes.xyxy
        
        for i, box in enumerate(detected_boxes):
            x1, y1, x2, y2 = map(int, box)
            xrange = max(1, int(abs((x1 - x2) / 30)))
            yrange = max(1, int(abs((y1 - y2) / 30)))
            
            point_cloud_data = []
            for xp in range(x1, x2, xrange):
                for yp in range(y1, y2, yrange):
                    dis, coord = get_3d_camera_coordinate([xp, yp], aligned_depth_frame, depth_intrin)
                    point_cloud_data.append(f"{coord}")
            
            with open("point_cloud_data.txt", "a") as file:
                file.write(f"\nTime: {time.time()}\n")
                file.write(" ".join(point_cloud_data))
            
            ux, uy = int((x1 + x2) / 2), int((y1 + y2) / 2)
            dis, coord = get_3d_camera_coordinate([ux, uy], aligned_depth_frame, depth_intrin)
            formatted_coord = f"({coord[0]:.2f}, {coord[1]:.2f}, {coord[2]:.2f})"
            
            cv2.circle(annotated_frame, (ux, uy), 4, (255, 255, 255), 5)
            cv2.putText(annotated_frame, formatted_coord, (ux + 20, uy + 10), 
                        0, 1, [225, 255, 255], thickness=1, lineType=cv2.LINE_AA)
        
        cv2.imshow('YOLOv8 RealSense', annotated_frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
finally:
    pipeline.stop()

2. 关键点识别逻辑

对于姿态估计任务,流程类似:先获取对齐后的深度与彩色帧,提取相机内参,再遍历关键点坐标计算深度值。

  1. 获取对齐帧:使用 align.process(frames) 确保深度与颜色像素一一对应。
  2. 提取内参:通过 profile.as_video_stream_profile().intrinsics 获取焦距和主点。
  3. 坐标转换:利用 get_distance 获取像素深度,结合内参反投影至相机坐标系。

这种方案能有效融合 2D 视觉感知与 3D 空间信息,适用于机器人抓取、人体动作分析等场景。

目录

  1. 产品简介
  2. SDK 安装与环境准备
  3. 1. 下载与安装
  4. 2. 基础测试
  5. Python 环境搭建与基础调用
  6. 1. 虚拟环境配置
  7. 2. 基础流可视化
  8. 数据采集与保存
  9. 初始化 Pipeline
  10. 进阶应用:YOLOv8 与关键点识别
  11. 1. YOLOv8 目标检测与三维坐标
  12. 2. 关键点识别逻辑

更多推荐文章

查看全部
  • Obsidian + Claudian 搭建 AI 知识管理系统
  • Windsurf AI IDE 深度使用指南
  • 无人机航拍图像标注:从采集到训练全流程
  • Parasoft C/C++test 安装部署与静态分析配置教程
  • 无人机光伏双模态缺陷检测数据集(红外 + 可见光)及 YOLO 融合方案
  • 动态规划详解:核心概念与经典案例
  • YOLOv8 目标检测 PySide6 可视化 GUI 界面实现
  • MCP 插件实战:browser-tools-mcp 配置与使用指南
  • Termux+Ubuntu 本地部署 OpenClaw 与 Llama 大模型教程
  • 前缀和算法详解与经典例题
  • Apache IoTDB 实战:FILL 空值填充与 LIMIT/SLIMIT 分页查询
  • URDF 入门指南:创建你的第一个机器人模型
  • C++ 实现 2048 游戏核心逻辑解析
  • Python 制作大麦网抢票程序
  • 机器人 SLAM 技术核心原理解析
  • JavaScript 函数柯里化:原理与通用实现
  • 基于 CTCM 算法的复杂城市地形无人机避障三维航迹规划
  • 一个人就是一支影视团队:实测国内最强影视级 AI 视频创作平台 TapNow——告别抽卡,导演级精准控制
  • OpenAI Codex 本地部署、模型切换及 MCP 使用指南
  • 生物细胞学在 AI 时代的最新进展

相关免费在线工具

  • 加密/解密文本

    使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online

  • RSA密钥对生成器

    生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online

  • Mermaid 预览与可视化编辑

    基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online

  • 随机西班牙地址生成器

    随机生成西班牙地址(支持马德里、加泰罗尼亚、安达卢西亚、瓦伦西亚筛选),支持数量快捷选择、显示全部与下载。 在线工具,随机西班牙地址生成器在线工具,online

  • Gemini 图片去水印

    基于开源反向 Alpha 混合算法去除 Gemini/Nano Banana 图片水印,支持批量处理与下载。 在线工具,Gemini 图片去水印在线工具,online

  • curl 转代码

    解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online