在Ubuntu中获取奥比中光的深度值 Python代码

一、官网下载资料

https://developer.orbbec.com.cn/download.html?id=32下载SDK,并解压到Ubuntu中

二、进行后续工作

奥比中光Astra s Pro深度相机(RGBD)+Ubuntu显示深度图像+jeston Xavier NX平台_一头秀发的假程序猿的博客-CSDN博客_奥比中光pro

按照这篇的步骤,完全可以实现。亲测有效哦!!

三、出现的问题

1、在Ubuntu中直接运行会提示OpenNI2.dll: file does not exist

是文件夹中缺少文件,只需要将OpenNI2文件夹、OpenNI.ini、OpenNI.dll和OpenNI.lib全部复制到python文件目录下

2、还有‘‘.so’’文件找不到的情况,这个问题按照上述博客的方法可以解决。

四、代码

from openni import openni2
import numpy as np
import cv2


def depth2mi(depthValue):

    return depthValue * 0.001


def depth2xyz(u, v, depthValue):
    fx = 577.54679
    fy = 578.63325
    cx = 310.24326
    cy = 253.65539

    #depth = depth2mi(depthValue)
    depth = depthValue*0.001

    z = float(depth)
    x = float((u - cx) * z) / fx
    y = float((v - cy) * z) / fy

    result = [x, y, z]
    return result


def mousecallback(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        print(y, x, dpt[y, x])
        arr = np.array(dpt)
        depthValue =float(arr[y, x])
        coordinate = depth2xyz(x, y, depthValue)     
        print("coordinate:", coordinate)


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(2)
    cv2.namedWindow('depth')
    ('depth', mousecallback)

    while True:

        frame_dep = depth_stream.read_frame()

        dframe_data = np.array(frame_dep.get_buffer_as_triplet()).reshape([480, 640, 2])
        dpt1 = np.asarray(dframe_data[:, :, 0], dtype='uint16')
        dpt2 = np.asarray(dframe_data[:, :, 1], dtype='uint16')

        dpt2 *= 255
        dpt = dpt1 + dpt2

        dpt = dpt[:, ::-1]
        im_color = cv2.applyColorMap(cv2.convertScaleAbs(dpt, alpha=0.03), cv2.COLORMAP_JET)
        cv2.imshow('depth', im_color)

        ret, frame = cap.read()
        a = frame
        cv2.imshow('color', frame)

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

    depth_stream.stop()
    dev.close()

代码参考:python通过openni获取奥比中光Astra Pro的深度值和RGB图像_K4762的博客-CSDN博客_python 奥比中光

你可能感兴趣的:(caffe,人工智能,深度学习)