pykinect2 extract depth data from individule pixel KinectV2

1.1k Views Asked by At

I am trying to extract the depth data from a pixel that is clicked on with the mouse. Its works when I print the X,Y coordinates. It dosen't work when I try to print pixel depth data .The code Im attempting to use is a modified from how to extract RGB values for a clicked on pixel. The issue Im having is how to parse out the depth data to print. When I run this I get TypeError: 'NoneType' object is not subscriptable Any ideas?

Here is code:

kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Depth)

while True:
    # --- Getting frames and drawing
    if kinect.has_new_depth_frame():
        frame = kinect.get_last_depth_frame()
        frameD = kinect.get_last_depth_frame()
        frameD = kinect._depth_frame_data
        #frameD = frame.astype(np.uint8)
        frame = frame.astype(np.uint8)
        frame = np.reshape(frame, (424, 512))
        output = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
        def click_event(event, x, y, flags, param):
            if event == cv2.EVENT_LBUTTONDOWN:
                print(x, y)
            if event == cv2.EVENT_RBUTTONDOWN:
                Pixel_Depth = output[x, y , 1]
                print(Pixel_Depth)
        #output = cv2.bilateralFilter(output, 1, 150, 75)
        cv2.imshow('KINECT Video Stream', output)
        cv2.setMouseCallback('KINECT Video Stream', click_event)
        output = None

    key = cv2.waitKey(1)
1

There are 1 best solutions below

0
On

To get it to work I had to specify the pixel number not the x,y coordinates of "frameD = kinect._depth_frame_data"

Here is code:

rom pykinect2 import PyKinectV2
from pykinect2.PyKinectV2 import *
from pykinect2 import PyKinectRuntime
import numpy as np
import cv2

kinect = PyKinectRuntime.PyKinectRuntime(PyKinectV2.FrameSourceTypes_Depth)

while True:
    # --- Getting frames and drawing
    if kinect.has_new_depth_frame():
        frame = kinect.get_last_depth_frame()
        frameD = kinect._depth_frame_data
        frame = frame.astype(np.uint8)
        frame = np.reshape(frame, (424, 512))
        frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
        def click_event(event, x, y, flags, param):
            if event == cv2.EVENT_LBUTTONDOWN:
                print(x, y)
            if event == cv2.EVENT_RBUTTONDOWN:
                Pixel_Depth = frameD[((y * 512) + x)]
                print(Pixel_Depth)
        ##output = cv2.bilateralFilter(output, 1, 150, 75)
        cv2.imshow('KINECT Video Stream', frame)
        cv2.setMouseCallback('KINECT Video Stream', click_event)
        output = None

    key = cv2.waitKey(1)
    if key == 27: break