3

I'm currently working on a project where I need to access and process depth data using the PyKinect library.

What I want to do is to define a depth threshold where I'll do some image segmentation, but since I'm new to PyKinect and I still don't know quite well where to look for resources, I don't know how to access that data and get the values.

I've tried to use the freenect library also, but I cannot get it to work.

Can anyone tell me how to do that or redirect me to some kind of documentation?

4

1 回答 1

5

我刚刚在我的 BitBucket 帐户上创建了一个片段,以使用 PyKinect 和 Pygame 可视化深度图像。这是代码:

import thread
import pygame
from pykinect import nui

DEPTH_WINSIZE = 320,240

screen_lock = thread.allocate()
screen = None

tmp_s = pygame.Surface(DEPTH_WINSIZE, 0, 16)


def depth_frame_ready(frame):
    with screen_lock:
        frame.image.copy_bits(tmp_s._pixels_address)
        arr2d = (pygame.surfarray.pixels2d(tmp_s) >> 7) & 255
        pygame.surfarray.blit_array(screen, arr2d)

        pygame.display.update()


def main():
    """Initialize and run the game."""
    pygame.init()

    # Initialize PyGame
    global screen
    screen = pygame.display.set_mode(DEPTH_WINSIZE, 0, 8)
    screen.set_palette(tuple([(i, i, i) for i in range(256)]))
    pygame.display.set_caption('PyKinect Depth Map Example')

    with nui.Runtime() as kinect:
        kinect.depth_frame_ready += depth_frame_ready   
        kinect.depth_stream.open(nui.ImageStreamType.Depth, 2, nui.ImageResolution.Resolution320x240, nui.ImageType.Depth)

        # Main game loop
        while True:
            event = pygame.event.wait()

            if event.type == pygame.QUIT:
                break

if __name__ == '__main__':
    main()

编辑:上面的代码显示了如何将深度数据转换为 8 位表示(以便可以轻松地将它们绘制为灰度图像)。但是如果你想使用实际的深度数据,你需要知道它们的结构。

使用 Microsoft Kinect SDK(PyKinect 所基于),单个深度像素由 16 位组成。3个不太重要的代表玩家索引,而我不太了解最重要的一个的含义......但是假设我们需要删除最后3位和第一个。例如,这是您需要为每个像素执行的操作的示例(取自此问题):

0 1 1 0 0 0 1 0 0 0 1 1 1 0 0 0 - 16 bits number
0 1 1 0 0 0 1 0 0 0 1 1 1       - 13 bits number
  1 1 0 0 0 1 0 0 0 1 1 1       - 12 bits number

上述操作(删除最后 3 位和第一个)可以通过对arr2d数组进行两次按位操作来实现。因为它是一个 NumPy 数组,所以可以按如下方式进行:

def depth_frame_ready(frame):
    frame.image.copy_bits(tmp_s._pixels_address)

    arr2d = (pygame.surfarray.pixels2d(tmp_s) >> 3) & 4095
    # arr2d[x,y] is the actual depth measured in mm at (x,y)

然后,您可能需要显示此数据,因此您可能需要 8 位表示。为拿到它,为实现它:

arr2d >>= 4
于 2015-05-18T12:04:35.713 回答