奥比中光大白 RGBD相机 python opencv代码实现

奥比中光 "大白",彩色图像UVC协议,深度用openni。(很烦)

我们的项目需要在python中使用,问了奥比中光的技术支持,说没有python版本的demo,就自己写了一个。

彩色:

import cv2
import sys

capture_width  = 800
capture_height = 1280


cap = cv2.VideoCapture(0)
cap.set(3, capture_width)
cap.set(4, capture_height)

for i in range(10):
  cap.read()

ret, img = cap.read()
cv2.imwrite("uvc.png", img)
cap.release()

深度:

import cv2
import numpy as np
from openni import openni2
from openni import _openni2 as c_api
import time
import imutils
import ctypes
from openni import openni2
redistPath = "../lib/Redist/" 
#redist就是sdk文件夹下,Windows下的Redist文件夹,里面是openni.ini等
openni2.initialize(redistPath) 


fps = 30      # frames per second
width = 640       # Width of image
height = 400      # height of image


dev = openni2.Device.open_any()
print(dev.get_device_info())
depth_stream = dev.create_depth_stream()
dev.set_image_registration_mode(True) #彩色和深度图像对齐
dev.set_depth_color_sync_enabled(True)


depth_stream.set_video_mode(c_api.OniVideoMode(pixelFormat=c_api.OniPixelFormat.ONI_PIXEL_FORMAT_DEPTH_1_MM,
                                                   resolutionX=width,
                                                   resolutionY=height,
                                                   fps=fps))
depth_stream.start()
while True:
    frame = depth_stream.read_frame()
    frame_data = frame.get_buffer_as_uint16()
    img = np.ndarray((frame.height, frame.width), dtype=np.uint16,buffer=frame_data)

    cv2.imshow("Depth", img)
    if (cv2.waitKey(1) & 0xFF == ord('q')):
        break

openni2.unload()
dev.close()

 

你可能感兴趣的:(python)