前置条件:
1、openni2
2、opencv 要混合openni2 进行编译
注:一定要下载源码自己编译,默认的python-opencv没有openni
可以用 print(cv2.getBuildInformation())进行查看是否有openni模块 应该在videoIO下面
平台:
windows10
import numpy as np
import cv2
from openni import openni2
from openni import _openni2 as c_api
openni2.initialize() # can also accept the path of the OpenNI redistribution
dev = openni2.Device.open_any()
# 帧同步
dev.set_depth_color_sync_enabled(True)
# 设置注册(registration)格式
dev.is_image_registration_mode_supported(openni2.IMAGE_REGISTRATION_DEPTH_TO_COLOR)
dev.set_image_registration_mode(openni2.IMAGE_REGISTRATION_DEPTH_TO_COLOR)
# 视频流的传输
#深度流不用显示的话,可以不用这两句
depth_stream = dev.create_depth_stream()
depth_stream.start()
#设置完注册模式后,color_stream就是合并后的信息
color_stream = dev.create_color_stream()
color_stream.start()
while(True):
colorframe = color_stream.read_frame()
color_frame_data = colorframe.get_buffer_as_uint8()
colorimg = np.frombuffer(color_frame_data, dtype=np.uint8)
colorimg.shape = (424,512,3)
# img 1080*1920*3
colorimg = cv2.cvtColor(colorimg,cv2.COLOR_BGR2RGB)
depth_frame = depth_stream.read_frame()
depth_frame_data = depth_frame.get_buffer_as_uint16()
colorimg = cv2.resize(colorimg,(1920,1080))
cv2.imshow("color", colorimg)
cv2.waitKey(34)
depth_stream.stop()
openni2.unload()



