python调用Kinect-V2获取rgb与深度图

环境配置

  • PyKinect2 官方仓库
  • PyKinect2 安装/使用问题汇总
pip install pykinect2 comtypes numpy pygame

Code

Kinect必须使用USB 3.0接口

from pykinect2 import PyKinectV2
from pykinect2 import PyKinectRuntime
import cv2
import numpy as np

# 获取深度图, 默认尺寸 424x512
def get_last_depth():
    frame = kinect.get_last_depth_frame()
    frame = frame.astype(np.uint8)
    dep_frame = np.reshape(frame, [424, 512])
    return cv2.cvtColor(dep_frame, cv2.COLOR_GRAY2RGB)

#获取rgb图, 1080x1920x4
def get_last_rbg():
    frame = kinect.get_last_color_frame()
    return np.reshape(frame, [1080, 1920, 4])[:, :, 0:3]

# 运行模式选择读取深度和rgb
kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Depth | PyKinectV2.FrameSourceTypes_Color)
# 二选一使用opencv显示
frame_type = 'rgb'
while True:
    if frame_type == 'rgb':
        # if kinect.has_new_color_frame():
        last_frame = get_last_rbg()
    else:
        # if kinect.has_new_depth_frame():
        last_frame = get_last_depth()
    # 使用opencv显示图片
    cv2.imshow('test', last_frame)
    cv2.waitKey(1)


更多推荐

python调用Kinect-V2 rgb与深度图