奥比中光相机的python采集代码

奥比中光相机的python采集代码


前言


在Windows系统下,使用python语言,采集奥比中光相机的拍摄的图像。

一、驱动安装

在奥比中光官网安装驱动。
下载地址:https://developer.orbbec.com.cn/download.html?id=32
进入这个页面点击“安装”------“下一页”------“完成"。
在这里插入图片描述

在这里插入图片描述
进入设备管理器查看,奥比中光相机正常运转。
在这里插入图片描述

二、配置openni

使用python驱动奥比中光相机需要借助openni2,安装步骤如下。
从官网下载openni2安装包,地址:https://structure.io/openni
在这里插入图片描述
安装完成后,系统会自动配置环境变量。
在这里插入图片描述
如果环境变量不存在,需要自己手动补齐。
在官网下载的openni2不包含orbbec.dll,orbbec.ini。手动将这两个文件复制到刚才安装的OpenNI2\Redist\OpenNI2\Drivers文件夹中。

三、采集代码

from openni import openni2
import numpy as np
import cv2
import datetime
import matplotlib.pyplot as plt
import time


def loop_func(func, second):
    while True:
        func()
        time.sleep(second)
        key = cv2.waitKey(1)
        if int(key) == ord('Q'):
            break


def picture_capture():
    frame = depth_stream.read_frame()
    dframe_data = np.array(frame.get_buffer_as_triplet()).reshape([480, 640, 2])
    dpt1 = np.asarray(dframe_data[:, :, 0], dtype='float32')
    dpt2 = np.asarray(dframe_data[:, :, 1], dtype='float32')

    dpt2 *= 255
    dpt = dpt1 + dpt2

    now = datetime.datetime.now()
    now = now.strftime('%Y%m%d%H%M%S')
    list1 = ['E:/testpic/', now, 'dth', '.png']
    list2 = ['E:/testpic/', now, 'rgb', '.png']
    address1 = ''.join(list1)
    address2 = ''.join(list2)
    ret, frame = cap.read()
    cv2.imwrite(address2, frame)
    print(dpt.shape)
    plt.figure(640)
    plt.imshow(dpt)
    plt.axis('off')  
    fig = plt.gcf()
    fig.set_size_inches(6.4 / 3, 4.81 / 3)  # dpi = 300, output = 700*700 pixels
    plt.gca().xaxis.set_major_locator(plt.NullLocator())
    plt.gca().yaxis.set_major_locator(plt.NullLocator())
    plt.subplots_adjust(top = 1, bottom = 0, right = 1, left = 0, hspace = 0, wspace = 0)
    plt.margins(0,0)
    fig.savefig(address1, format='png', transparent=True, dpi=300, pad_inches = 0)


if __name__ == "__main__":

    openni2.initialize()

    dev = openni2.Device.open_any()
    print(dev.get_device_info())

    depth_stream = dev.create_depth_stream()
    depth_stream.start()

    cap = cv2.VideoCapture(0)

    loop_func(picture_capture, 1)
    depth_stream.stop()
    dev.close()

使用以上代码就可以实时采集图片。

  • 5
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是使用奥比中光深度相机获取点云的Python代码示例: ```python import openni2 import numpy as np # 初始化奥比中光深度相机 openni2.initialize() # 打开设备 dev = openni2.Device.open_any() # 创建深度流 depth_stream = dev.create_depth_stream() # 启动深度流 depth_stream.start() # 获取深度图像 depth_frame = depth_stream.read_frame() # 将深度图像转换为numpy数组 depth_array = np.array(depth_frame.get_buffer_as_uint16()) # 获取相机参数 depth_stream_profile = depth_stream.get_video_stream().get_sensor_info().get_stream_profile() intrinsics = depth_stream_profile.get_intrinsics() # 计算点云 fx = intrinsics.fx fy = intrinsics.fy cx = intrinsics.ppx cy = intrinsics.ppy depth_scale = depth_stream.get_depth_scale() depth_array = depth_array.astype(float) * depth_scale rows, cols = depth_array.shape c, r = np.meshgrid(np.arange(cols), np.arange(rows), sparse=True) valid = depth_array > 0 z = np.where(valid, depth_array, np.nan) x = np.where(valid, z * (c - cx) / fx, 0) y = np.where(valid, z * (r - cy) / fy, 0) point_cloud = np.dstack((x, y, z)) # 关闭深度流 depth_stream.stop() depth_stream.destroy() # 关闭设备 dev.close() # 反初始化奥比中光深度相机 openni2.unload() ``` 以上代码中,首先使用`openni2.initialize()`初始化奥比中光深度相机,然后使用`dev.create_depth_stream()`创建深度流,并使用`depth_stream.start()`启动深度流。接着,使用`depth_stream.read_frame()`获取深度图像,并使用`depth_frame.get_buffer_as_uint16()`将深度图像转换为numpy数组。然后,使用`depth_stream.get_video_stream().get_sensor_info().get_stream_profile()`获取相机参数,并计算点云。最后,使用`depth_stream.stop()`关闭深度流,使用`dev.close()`关闭设备,最后使用`openni2.unload()`反初始化奥比中光深度相机

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值