pyrealsense2 初步使用教程

2023-05-16

这是我的推广信息,以激励自己更好的分享自己的知识和经验!也希望看到的你能够多多支持,谢谢!

1. 滴滴云AI大师:

目前滴滴云正在大力推广自己的云计算服务,需要购买的朋友们用我的AI大师码 「2049」在滴滴云上购买 GPU / vGPU / 机器学习产品可额外享受 9 折优惠,点击这里前往滴滴云官网。

参考信息

  • pyrealsense2 documentation
  • 官方示例

示例代码

import numpy as np
import cv2
import matplotlib.pyplot as plt
import random

import pyrealsense2 as rs
bagfile = 'realsense/record/20200901.bag'

初始化工作

注意要预先知道彩色图像和深度图像的分辨率,否则报错。

pipeline = rs.pipeline()

config = rs.config()
rs.config.enable_device_from_file(config, bagfile, repeat_playback=False)

config.enable_stream(rs.stream.color, 1280, 720, rs.format.rgb8, 30)
config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30)

pf = pipeline.start(config) 
<ipython-input-3-291a003cf405>:6: DeprecationWarning: an integer is required (got type pyrealsense2.format).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
  config.enable_stream(rs.stream.color, 1280, 720, rs.format.rgb8, 30)
<ipython-input-3-291a003cf405>:7: DeprecationWarning: an integer is required (got type pyrealsense2.format).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
  config.enable_stream(rs.stream.depth, 848, 480, rs.format.z16, 30)
print(f"device: {pf.get_device()}")
print(f"depth_sensor: {pf.get_device().first_depth_sensor()}")
print(f"depth_scale: {pf.get_device().first_depth_sensor().get_depth_scale()}")
print(f"streams: {pf.get_streams()}")
device: <pyrealsense2.device: Intel RealSense D435I (S/N: 939622075496)>
depth_sensor: <pyrealsense2.depth_sensor object at 0x7fd10032ac70>
depth_scale: 0.0010000000474974513
streams: [<pyrealsense2.video_stream_profile: 1(0) 848x480 @ 30fps 1>, <pyrealsense2.video_stream_profile: 2(0) 1280x720 @ 30fps 5>]

读取一帧数据

# 将深度图对齐到RGB
align_to = rs.stream.color
align = rs.align(align_to)
print(f"align:{align}")

# 获取Realsence一帧的数据
frame = pipeline.wait_for_frames()

# frame的基本元素
print(f"data: {frame.data}")
print(f"frame_number: {frame.frame_number}")
print(f"frame_timestamp_domain: {frame.frame_timestamp_domain}")
print(f"profile: {frame.profile}")
print(f"timestamp: {frame.timestamp}")
align:<pyrealsense2.align object at 0x7fd0f9fd6870>
data: <pyrealsense2.BufData object at 0x7fd0fa2820f0>
frame_number: 3744
frame_timestamp_domain: timestamp_domain.system_time
profile: <pyrealsense2.video_stream_profile: 1(0) 848x480 @ 30fps 1>
timestamp: 1598934180418.8987

获取RGB图像

# 获取图像数据
data_sz = frame.get_data_size()
print(data_sz, f"1280*720={1280* 720 * 3}")

color_rs = frame.get_color_frame()
img = np.asanyarray(color_rs.get_data())
plt.imshow(img)
plt.axis('off')
plt.show()
814080 1280*720=2764800

在这里插入图片描述

获取深度图

一般深度图的单位是mm,用np.uint16来表示.

depth_rs = frame.get_depth_frame()
depth = np.asanyarray(depth_rs.get_data())
print(depth.shape, depth.dtype)
print(depth[0, 0])
print(f"max(depth):{np.max(depth)}; min(depth:{np.min(depth)})")
(480, 848) uint16
0
max(depth):7302; min(depth:0)
# 显示深度图,最大作用距离为4m
dimg_gray = cv2.convertScaleAbs(depth, alpha=255/4000)
dimg = cv2.applyColorMap(dimg_gray, cv2.COLORMAP_JET)

plt.imshow(dimg)
plt.axis('off')
plt.show()
plt.imshow(dimg_gray, cmap='gray')
plt.axis('off')
plt.show()

在这里插入图片描述
在这里插入图片描述

获取点云

点云数据就是一系列的点的合集,如下:

pc = rs.pointcloud()
pc.map_to(color_rs)
points_rs = pc.calculate(depth_rs)

points = np.asanyarray(points_rs.get_vertices())
print(points.shape, f"848*480 = {848*480}")
print(points[0], type(points[0]))
(407040,) 848*480 = 407040
(-0., -0., 0.) <class 'numpy.void'>
def calc_dist(xyz):
    x, y, z = xyz
    return (x**2 + y**2 + z**2)**0.5
dists = list(map(calc_dist, points))
dists = np.array(dists).reshape(dimg_gray.shape)

fig = plt.figure()

ax = fig.add_subplot(111)
ax.set_title('colorMap')
plt.imshow(dists)
ax.set_aspect('equal')

cax = fig.add_axes([0.12, 0.1, 0.78, 0.8])
cax.get_xaxis().set_visible(False)
cax.get_yaxis().set_visible(False)
cax.patch.set_alpha(0)
cax.set_frame_on(False)
plt.colorbar(orientation='vertical')
plt.show()

在这里插入图片描述

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

pyrealsense2 初步使用教程 的相关文章

随机推荐

  • Intel汇编语言程序设计学习-第三章 汇编语言基础-上

    汇编语言基础 3 1 汇编语言的基本元素 有人说汇编难 xff0c 有人说汇编简单 xff0c 我个人不做评价 xff0c 下面是一个简单的实例 xff08 部分代码 xff09 xff1a main PROC mov eax 5 5送 E
  • 技术面试需要掌握的基础知识整理

    算法 pencil2 操作系统 computer 网络 cloud 面向对象 couple 数据库 floppy disk Java coffee 分布式 sweat drops 工具 hammer 编码实践 speak no evil 后
  • 【笔记78】同步访问共享的可变数据

    关键字 synchronized 可以保证在同一时刻 xff0c 只有一个线程可以执行某一个方法 xff0c 或者某一个代码块 许多程序员把同步的概念仅仅理解为一种互斥 xff08 mutual exclusion xff09 的方式 xf
  • GitHub如何配置SSH Key

    文章目录 步骤一 设置git的user name和email二 检查是否存在SSH Key三 获取SSH Key四 GitHub添加SSH Key五 验证和修改 https github com xiangshuo1992 preload
  • 无人机相关资料整理-备忘

    近期一直在学习无人机相关基础知识以及平台搭建方面的知识 xff0c 搜集了很多网站的相关信息 xff0c 有一些比较有用的github 中英文资料 谈论区网址 还收藏了一些免费的课程 xff0c 以及自己已购买的课程的链接 有些普及的教程和
  • ov_eval说明

    目录 ov evalpose to fileformat convertererror comparisonerror dataseterror singlerunerror simulationtiming singleruntiming
  • arm开发板de1-soc配置opencv3.2环境

    0 温馨提示 不要尝试vnc虚拟桌面 xff0c 不要尝试高版本ubuntu镜像 xff0c 更不要尝试交叉编译 当然做以上这些尝试你确实可以学到更多东西 xff0c 不止学会怎么用这个板子 1 准备所需材料 1 1 opencv3 2 h
  • 2020-11-10

    https pan baidu com s 1uvuB6ahrfijMiWy9AqFCig
  • 杰理之AT协议之协议说明【篇】

    xff08 1 xff09 MCU发给芯片的数据包称为CMD xff08 命令 xff09 xff0c MCU通过发送CMD来完成配置蓝牙 xff0c 控制蓝牙连接 xff0c 发送数据等操作 xff08 2 xff09 芯片发给MCU的数
  • 杰理之创建静态任务和创建动态任务有什么区别,该如何选择?【篇】

    答 xff1a 静态任务创建时采用的是静态内存 xff0c 而动态任务创建时采用的动态内存 采用静态任务创建可以减小内存碎片 xff1b 动态任务创建由于会频繁分配和释放内存 xff0c 容易产生内存碎片 因此系统运行期间都必须存在的任务可
  • 杰理之AT协议说明【篇】

    xff08 1 xff09 MCU 发给芯片的数据包称为 CMD xff08 命令 xff09 xff0c MCU 通过发送 CMD 来完成配置蓝牙 xff0c 控制蓝牙 连接 xff0c 发送数据等操作 xff08 2 xff09 芯片发
  • 最新详细版Ubuntu20.04安装教程

    文章目录 浏览设置默认迅雷下载下载UbuntuVMware中添加虚拟机 浏览设置默认迅雷下载 很多资源使用迅雷下载会比使用浏览器下载快很多 这里给大家说如何设置浏览器默认使用迅雷下载 打开迅雷 xff0c 打开迅雷的设置中心 xff0c 找
  • ubuntu 14.04 opencv2 和opencv3 多个版本切换使用

    最近在学视觉SLAM过程中要使用opencv2 和opencv3 xff0c 虽然说在程序里能修改opencv 代码 xff0c 但是自从用上linux后不怕折腾的精神已经深入人心 安装双opencv切换使用多方便 在折腾了若干次之后终于成
  • Jetson TX2刷rtso-9003并使用Jetpack安装软件包

    Jetson TX2刷rtso 9003并使用Jetpack安装软件包 为Jetson TX2刷rtso 9003系统并使用Jetpack安装其他软件包 xff08 CUDA TensorRT cuDNN等 xff09 1 在主机上下载并安
  • FLIR Thermal Starter 数据集详解

    简介 下载地址 xff1a https pan baidu com s 11GJe4MdM NH6fuENCQ2MtQ 提取码 019b官方网站 xff1a https www flir com oem adas adas dataset
  • 关于qt.qpa.plugin: Could not load the Qt platform plugin “xcb“的问题

    今天在运行代码的时候出现了以下错误 xff0c 经查找资料 xff0c 找到了解决方法 错误如下 xff1a QFactoryLoader span class token punctuation span span class token
  • 【工具推荐】Windows下读取Linux系统的文件,Linux Reader4.5 By DiskInternals

    前言 相信做机器视觉相关的很多人都会安装 Windows 和 Linux 双系统 在 Linux 下 xff0c 我们可以很方便的访问Windows的磁盘 xff0c 反过来却不行 但是这又是必须的 通过亲身体验 xff0c 向大家推荐这么
  • Windows/Linux下创建文件夹的软连接,让一个大数据在电脑上只存在一份

    前言 相信我们在使用计算机的过程中存在这样的情况 xff0c 不同的地方都需要用到同一个大的视频或者其他数据 常常需要把这些数据复制过来复制过去 xff0c 浪费时间和空间 这时候 xff0c 创建数据或者文件夹的软连接就显得很方便了 让连
  • 2D目标检测模型表现总览

    前言 2D检测领域各种模型层出不穷 xff0c 为了对他们有一个直观的印象 xff0c 想要总结这么一张表 强烈欢迎大家补充 修正 更改以及完善 xff01 可以在本文留言 xff0c 或者去github上修改 xff0c 谢谢 xff01
  • pyrealsense2 初步使用教程

    这是我的推广信息 xff0c 以激励自己更好的分享自己的知识和经验 xff01 也希望看到的你能够多多支持 xff0c 谢谢 xff01 1 滴滴云AI大师 目前滴滴云正在大力推广自己的云计算服务 xff0c 需要购买的朋友们用我的AI大师