andreeas26
andreeas26

Reputation: 71

pykinect2 depth frame truncated

I am trying to use PyKinect2 module to get the depth frames from a Kinect v2 device. I've followed the examples provided here. I am able to see the depth frames, but they appear in the PyGame window truncated, although the size of the frame is 512x424 as it should be.

depth_frame

I use the get_last_depth_frame() method provided by PyKinect2 and draw it on the surface with the following code.

def draw_depth_frame(self, frame, target_surface):
    target_surface.lock()
    address = self._kinect.surface_as_array(target_surface.get_buffer())
    ctypes.memmove(address, frame.ctypes.data, frame.size)
    del address
    target_surface.unlock()

Upvotes: 1

Views: 752

Answers (2)

Alaa M.
Alaa M.

Reputation: 5273

First you have to change the Surface size to 24bit:

In __init__(self) function:

self._depth_frame_surface = pygame.Surface((self._kinect.depth_frame_desc.Width,
self._kinect.depth_frame_desc.Height),
0, 24)

Then you should change self._screen to take the depth frame width and height:

self._screen = pygame.display.set_mode((self._kinect.depth_frame_desc.Width,
self._kinect.depth_frame_desc.Height), pygame.HWSURFACE | pygame.DOUBLEBUF | pygame.RESIZABLE,
32)

Finally, draw the depth frame like @Shuangjun Suggested:

def draw_depth_frame(self, frame, target_surface):
    target_surface.lock()
    f8 = np.uint8(frame.clip(1, 4000) / 16.)
    frame8bit = np.dstack((f8, f8, f8))
    address = self._kinect.surface_as_array(target_surface.get_buffer())
    ctypes.memmove(address, frame8bit.ctypes.data, frame8bit.size)
    del address
    target_surface.unlock()

Upvotes: 0

Shuangjun Liu
Shuangjun Liu

Reputation: 93

I don't have reputation to comment. So I write it here. I think you need 3 channels to stuff the memory to required length. Try this

f8=np.uint8(frame.clip(1,4000)/16.)    
frame8bit=np.dstack((f8,f8,f8))

Then memmove from frame8bit instead of original frame.

Upvotes: 1

Related Questions