【发布时间】:2015-06-21 17:39:31
【问题描述】:
我目前正在开展一个项目,我需要使用 PyKinect 库访问和处理深度数据。
我想做的是定义一个深度阈值,我将在其中进行一些图像分割,但由于我是 PyKinect 的新手,我仍然不太清楚在哪里寻找资源,所以我不知道知道如何访问该数据并获取值。
我也尝试使用 freenect 库,但无法使用。
谁能告诉我该怎么做或将我重定向到某种文档?
【问题讨论】:
我目前正在开展一个项目,我需要使用 PyKinect 库访问和处理深度数据。
我想做的是定义一个深度阈值,我将在其中进行一些图像分割,但由于我是 PyKinect 的新手,我仍然不太清楚在哪里寻找资源,所以我不知道知道如何访问该数据并获取值。
我也尝试使用 freenect 库,但无法使用。
谁能告诉我该怎么做或将我重定向到某种文档?
【问题讨论】:
我刚刚创建了一个snippet on my BitBucket account 来使用 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位和第一个。例如,这是您需要对每个像素执行的操作的示例(取自this question):
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
【讨论】:
arr2d >>= 4):最后一个操作会将处理后的深度图转换为 8 位灰度图像。