Intel RealSense D435i深度相机通过点云获取图片中任意点三维信息(python实现)

2023-05-16

  • 引用基础包

import pyrealsense2 as rs
import numpy as np
import cv2
import os
import time
  • 声明了个类,以后也许会添加重置旋转等操作,目前只用了暂停

class State:
    def __init__(self, *args, **kwargs):
        self.WIN_NAME = 'RealSense'
        self.paused = False

state = State()
saved_count = 0
  • 一些相机的基础设置

# 设置
pipeline = rs.pipeline()
config = rs.config()

pipeline_wrapper = rs.pipeline_wrapper(pipeline)
pipeline_profile = config.resolve(pipeline_wrapper)
device = pipeline_profile.get_device()

found_rgb = False
for s in device.sensors:
    if s.get_info(rs.camera_info.name) == 'RGB Camera':
        found_rgb = True
        break
if not found_rgb:
    print("The demo requires Depth camera with Color sensor")
    exit(0)

config.enable_stream(rs.stream.depth, 1280, 720, rs.format.z16, 30)
config.enable_stream(rs.stream.color, 1280, 720, rs.format.bgr8, 30)

# 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)

# 声明点云对象
pc = rs.pointcloud()
points = rs.points()

# 创建对齐对象与color流对齐
align_to = rs.stream.color 
align = rs.align(align_to)

cv2.namedWindow("live", cv2.WINDOW_AUTOSIZE)
  • 定义保存路径

# 保存路径
f_path = r".\test\test3"
if not os.path.exists(f_path):
    os.mkdir(f_path)
    os.mkdir(os.path.join(f_path, "images"))
    os.mkdir(os.path.join(f_path, "information"))
    os.mkdir(os.path.join(f_path, "live_record"))

save_path = os.path.join(os.getcwd(), f_path + "\live_record", time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime()))
os.mkdir(save_path)
os.mkdir(os.path.join(save_path, "color"))
os.mkdir(os.path.join(save_path, "depth"))
  • 获取对齐的图像帧和相机内参 

def get_aligned_images():
    frames = pipeline.wait_for_frames()  # 等待获取图像帧,获取颜色和深度的框架集
    aligned_frames = align.process(frames)  # 获取对齐帧,将深度框与颜色框对齐

    aligned_depth_frame = aligned_frames.get_depth_frame()  # 获取对齐帧中的的depth帧
    aligned_color_frame = aligned_frames.get_color_frame()  # 获取对齐帧中的的color帧

    # 将images转为numpy arrays
    img_color = np.asanyarray(aligned_color_frame.get_data())  # RGB图
    img_depth = np.asanyarray(aligned_depth_frame.get_data())  # 深度图

    # 获取相机参数
    depth_intrin = aligned_depth_frame.profile.as_video_stream_profile().intrinsics  # 获取深度参数(像素坐标系转相机坐标系会用到)
    color_intrin = aligned_color_frame.profile.as_video_stream_profile().intrinsics  # 获取相机内参

    depth_mapped_image = cv2.applyColorMap(cv2.convertScaleAbs(img_depth, alpha=0.03), cv2.COLORMAP_JET)

    return color_intrin, depth_intrin, img_color, img_depth, depth_mapped_image, aligned_color_frame, aligned_depth_frame
  •  获取三维坐标

def get_3d_camera_coordinate(depth_pixel, aligned_color_frame, aligned_depth_frame):
    x = int(depth_pixel[0])
    y = int(depth_pixel[1])
    # 计算点云
    pc.map_to(aligned_color_frame)
    points = pc.calculate(aligned_depth_frame)
    vtx = np.asanyarray(points.get_vertices())
    # print('vtx_before_reshape: ', vtx.shape)  # 921600
    vtx = np.reshape(vtx, (720, 1280, -1))
    # print('vtx_after_reshape: ', vtx.shape)  # (720, 1280, 1)

    camera_coordinate = vtx[y][x][0]
    # print ('camera_coordinate: ',camera_coordinate)
    dis = camera_coordinate[2]
    return dis, camera_coordinate
  • 主函数

if __name__ == "__main__":
    while True:
        if not state.paused:
            # 获取对齐图像帧与相机参数
            color_intrin, depth_intrin, img_color, img_depth, depth_mapped_image, aligned_color_frame, aligned_depth_frame = get_aligned_images()  # 获取对齐图像与相机参数

        # 显示画面
        cv2.imshow("live", np.hstack((img_color, depth_mapped_image)))

        key = cv2.waitKey(1)

此时效果(左侧RGB图,右侧深度图)(过近时深度信息几乎显示不出来)

  •  按下p键暂停画面

        if key == ord("p"):
            state.paused ^= True
  • 按下s键保存图片

        if key == ord("s"):
            saved_color_image = img_color
            saved_depth_mapped_image = depth_mapped_image

            cv2.imwrite(os.path.join((save_path), "color", "{}.jpg".format(saved_count)), saved_color_image)
            cv2.imwrite(f_path + r"\images\target.jpg", saved_color_image)
            np.save(os.path.join((save_path), "depth", "{}".format(saved_count)), img_depth)
            saved_count += 1
  • 按下r键读取刚才保存的图片,并通过image_sliced文件将图片裁剪到自己需要的范围

        if key == ord("r"):
            color_image = cv2.imread(f_path + r"\images\target.jpg")
            cv2.imshow("color", color_image)
            img_sliced = image_sliced.img_sliced(f_path)
            cv2.imshow("img_sliced", img_sliced)

image_sliced.py

import cv2
import os

def img_sliced(f_path):
    image_full = cv2.imread(f_path + r"\images\target.jpg")
    size = image_full.shape
    print('原尺寸=', size)
    target_sliced = image_full[80:370, 309:625]
    print('裁剪后=', target_sliced.shape)
    save_path = os.path.join(f_path + r"\images\target_sliced.jpg")
    cv2.imwrite(save_path, target_sliced)
    # cv2.imshow('image_full', image_full)
    # cv2.imshow('target_sliced', target_sliced)
    # cv2.waitKey(0)
    return target_sliced

# img_sliced(r'.\test\test3')
  • 按下g键进行图像处理,判断方向,并将三维信息显示在图片上

        if key == ord("g"):
            image_processing.main(f_path)
            img_rect = cv2.imread(f_path + r".\images\target_rect_1.jpg")

image_processing.py处理过程参考文章:用python和opencv实现物体框选并保存坐标信息及面积信息(附代码)

调用方向判断文件判断方向并获取物体中心点(x,y)坐标

direction_func.py参考:使用opencv判断物体方向

            cenx, ceny = direction_func.main(f_path)
            print("cenx,ceny", cenx, ceny)
            depth_pixel = [ceny, cenx]

调用获取三维坐标函数计算深度信息并标在图片上

            dis, camera_coordinate = get_3d_camera_coordinate(depth_pixel, aligned_color_frame, aligned_depth_frame)
            print('depth: ', dis)  # 深度单位是m
            print('camera_coordinate: ', camera_coordinate)

            img_rect_direction = cv2.imread(f_path + r".\images\target_rect_direction.jpg")
            # 在图中标记物体中心点及其坐标
            cv2.circle(img_rect_direction, (int(cenx), int(ceny)), 8, [255, 0, 255], thickness=-1)
            cv2.putText(img_rect_direction, "Dis:" + str(dis) + " m", (40, 40), cv2.FONT_HERSHEY_SIMPLEX, 1.2, [0, 0, 255])
            cv2.putText(img_rect_direction, "X:" + str(camera_coordinate[0]) + " m", (80, 80), cv2.FONT_HERSHEY_SIMPLEX, 1.2, [255, 0, 0])
            cv2.putText(img_rect_direction, "Y:" + str(camera_coordinate[1]) + " m", (80, 120), cv2.FONT_HERSHEY_SIMPLEX, 1.2, [255, 0, 0])
            cv2.putText(img_rect_direction, "Z:" + str(camera_coordinate[2]) + " m", (80, 160), cv2.FONT_HERSHEY_SIMPLEX, 1.2, [255, 0, 0])
            cv2.imshow('xyz', img_rect_direction)

效果:

  • 按下q键关闭所有窗口

        if key & 0xFF == ord('q') or key == 27:
            cv2.destroyAllWindows()
            break

结束

pipeline.stop()

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

Intel RealSense D435i深度相机通过点云获取图片中任意点三维信息(python实现) 的相关文章

随机推荐

  • 【el-table】设置行变色,以及鼠标移入的时候保持背景色不变

    1 根据条件设置行变色 xff0c 这个官网上也有示例 xff0c 没啥难的 xff0c 直接贴代码了 span class token operator lt span el table data span class token ope
  • 【vxe-table】自定义表头列是否可以排序

    vxe table真是让人又爱又恨 xff0c 官网示例还有文档都很详细 xff0c 但是可能是用的人少 xff0c 每次遇到问题 xff0c 都百度不到啥解决方法 xff0c 不像el table 全是方法 今天给我提了个bug xff0
  • Python中的列表,元组和字典

    一 列表 xff1a 数组 xff1a 存储同一种数据类型的集合 score 61 12 13 14 列表 xff08 被称为打了激素的数组 xff09 xff1a 可以存储任意数据类型的集合 xff08 一个变量中可以存储多个信息 xff
  • 【瀑布流插件】vue-masonry

    最近一直在画静态页面 xff0c 有个图片列表 xff0c 布局很紧凑 xff0c 图片宽度一样 xff0c 高度不一样 xff0c 但是效果图上都是紧挨在一起 xff0c 我用普通的v for循环加css布局 xff0c 它会以这一行最高
  • [svg-icon]引入vue项目后,use标签为0,已解决

    这个bug我之前遇到过一次 xff0c 解决了也就没记录 但是好记性不如烂笔头 xff0c 这次重新遇到 xff0c 又重新排查bug花了1个多小时 svg引入vue项目 xff0c 需要依赖svg sprite loader span c
  • 【css】字体渐变色,边框渐变色等笔记,按钮渐变色等持续更新~~

    一 字体渐变色 span class token operator lt span h2 span class token assign left variable class span span class token operator
  • 【vue】处理数组,无关联父id的情况下,根据特定条件区分父子项,单独给子项加事件

    先说下具体的场景 xff0c 有个列表数组 xff0c 有一列是科目代码 xff0c 这个和财务息息相关 xff0c 财务里还有借方 xff0c 贷方 xff0c 利润表之类的 xff0c 这里不详细阐述了 科目代码的规律是101 xff0
  • 2020-08-22

    广西 河池学院 广西高校重点实验室培训基地 系统控制与信息处理重点实验室 本篇博客来自河池学院 智控无人机小组 写作时间 xff1a 2020 8 22 有关系统节拍定时器的设计思路及方法源于 DataH的msOS学习之路 xff08 1
  • 2020-09-29

    广西 河池学院 广西高校重点实验室培训基地系统控制与信息处理重点实验室 本篇博客来自河池学院 智控无人机小组 写作时间 xff1a 2020 9 29 刚刚接触STM32f103 xff0c 简单了解了基本内容 有48个引脚 xff0c 其
  • DHT11温湿度传感器——学习总结(最详细,最容易适合新手看的资料)

    一 DHT11的简单介绍 DHT11是一款有已校准数字信号输出的温湿度传感器 其精度湿度 5 RH xff0c 温度 2 xff0c 量程湿度20 90 RH xff0c 温度0 50 百度百科 注解 xff1a 相对湿度 xff08 RH
  • 【零基础学STM32】CubeMX+HAL玩转电机控制

    Motor 主要内容前置知识CubeMX配置代码出现的问题参考文献 主要内容 基于被我鸽了的电控作业 主控 STM32F429IGT6 电机TT小黄 模拟小车所以两路编码器 前置知识包括 PID PWM 定时器 LM2596 L298N等
  • 解决ubuntu虚拟机没有公钥问题

    成功解决ubuntu虚拟机升级出现提示 xff1a 没有公钥的问题 把终端提示缺少的公钥复制到代码后面 span class token function sudo span apt key adv keyserver keyserver
  • H3C交换机常用命令

    H3C S5800 显示当前配置 lt H3C gt display current configuration 缩写 xff1a dis cur 恢复出厂设置 lt H3C gt reset saved configuration 按y
  • SVN 增加patch打包管理

    系统上线后必然面临系统的维护 xff0c 目前我们对系统维护和新需求开发 xff0c 是以打patch包形式更新程序 xff0c 但对打包的文件不能很好的搜寻出来 xff0c 为解决这个问题 xff0c 我新开发一插件 xff0c 在svn
  • 1.1 海思3518E视频编解码的一些概念

    目录 1 1 1 前言1 1 2 视频编解码的基本概念了解 1 1 1 前言 这是我第一次写博客 xff0c 我写博客的目的是为了记录我的学习笔记 xff0c 同时也是想把我的学习记录分享出来 xff0c 供参考学习 这个学习笔记是关于海思
  • Git 客户端 - 可视化工具 Fork 使用

    Fork 是什么 当我们在多人协同开发项目的过程中 xff0c Git 是必不可少的代码托管工具 xff0c 但是繁琐的操作命令 抽象的文件状态 xff0c 多个不同分支需要花费大量的时间进行分配管理与维护 xff0c 至此 Fork 拥有
  • STM32串口外设是否需要加上拉电阻?

    STM32F103串口TX一般设置为GPIO Mode AF PP xff08 复用推挽输出 xff09 xff1b RX一般设置为RX一般设置为GPIO Mode IN FLOATING 模拟输入 xff1b 如图所示 xff0c STM
  • Windows11升级踩坑过程与镜像下载地址汇总

    第一天开始写博客 xff0c 之前一直想写但是各种原因没有开始 xff0c 今天折腾了一天升级完了windows11 xff0c 想分享一下过程和踩的坑 xff0c 也算是给自己一个开始的契机 xff0c 有些东西重新配置的时候看自己的博客
  • STM32CubeMx使用教程(六)—— OLED屏使用

    前言 在前面一章中 xff0c 学习了 串口通信以及定时器 xff0c 本章节中将介绍I2C通信 xff0c 使用 I2C 通信方式点亮 OLED 模块 由于 OLED 模块支持多种通信方式 xff0c OLED 模块的 I2C 通信过程主
  • Intel RealSense D435i深度相机通过点云获取图片中任意点三维信息(python实现)

    引用基础包 import pyrealsense2 as rs import numpy as np import cv2 import os import time 声明了个类 xff0c 以后也许会添加重置旋转等操作 xff0c 目前只