深度摄像头D415在python中的使用

本文参考

【OpenCV 4】伪色彩 applyColorMap() 函数使用_kingkee的博客-CSDN博客_applycolormap函数

利用D415读取 需要标记的人脸face_recognition的距离 Python + wind10_weixin_44576543的博客-CSDN博客

目录

1.环境安装

2.获取双目图像

3.深度图像的获取


D415是深度图像的摄像机

我们使用python进行驱动

1.环境安装

python版本3.7,下面这些包使用pip都可以安装上,我是使用conda创建的环境,可能有的与使用别的环境不太一样

2.获取双目图像

我们使用普通彩色图像与红外线图像

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.infrared,  640, 480, rs.format.y8, 30)
pipeline.start(config)

try:
    while True:
        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()

        color_frame = frames.get_color_frame()
        depth_frame = frames.get_infrared_frame()

        # Convert images to numpy arrays 把图像转换为numpy data
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())
        color_image = cv2.cvtColor(color_image,cv2.COLOR_BGR2GRAY)

        # Stack both images horizontally 把两个图片水平拼在一起
        images = np.hstack((color_image, depth_image))

        # Show images 展示一下图片
        cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense', images)

        key = cv2.waitKey(1)
        if key == ord(' '):
            break

finally:
    # Stop streaming
    pipeline.stop()

运行后的效果是这样的,左侧本来是彩色的图像,我将其转换为灰度图像 

红外线图像中会有纹路一样的东西

 当我用手挡住其中彩色的摄像头,红外线摄像头不受影响

3.深度图像的获取

我使用了彩色原图像与深度图像进行对比,要不然效果不明显

import pyrealsense2 as rs
import numpy as np
import cv2

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)

# Start streaming
pipeline.start(config)

try:
    while True:
        # Wait for a coherent pair of frames: depth and color
        frames = pipeline.wait_for_frames()

        depth_frame = frames.get_depth_frame()
        color_frame = frames.get_color_frame()

        if not depth_frame or not color_frame:
            continue

        # Convert images to numpy arrays 把图像转换为numpy data
        depth_image = np.asanyarray(depth_frame.get_data())
        color_image = np.asanyarray(color_frame.get_data())

        # Apply colormap on depth image (image must be converted to 8-bit per pixel first) 在深度图上用颜色渲染
        depth_colormap = cv2.applyColorMap(cv2.convertScaleAbs(depth_image, alpha=0.03), cv2.COLORMAP_JET)
        depth_colormap = cv2.blur(depth_colormap,(3,3))

        # Stack both images horizontally 把两个图片水平拼在一起
        images = np.hstack((color_image, depth_colormap))

        # Show images 展示一下图片
        cv2.namedWindow('RealSense', cv2.WINDOW_AUTOSIZE)
        cv2.imshow('RealSense', images)

        key = cv2.waitKey(1)
        if key == ord(' '):
            break
finally:
    # Stop streaming
    pipeline.stop()
  • 代码中的blur滤波不是官方的demo,是我后来加的,加入后感觉上可以识别到更短的距离 

这里使用到了伪彩色,如果不加入伪彩色的话,图像是这样的

伪彩色的用法可以参考 【OpenCV 4】伪色彩 applyColorMap() 函数使用_风语留痕-CSDN博客

深度图像是两个摄像头的综合结果,挡上两个中的一个摄像头,深度图像就会消失

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Suyuoa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值