Kinect v2 SDK C++ - 2. Kinect Depth Data

Goals: Learn how to get depth data from a kinect and what the format of the data is.
Source: View Source      Download: 2_Depth.zip


Overview

This is a very short tutorial - there are only two real changes to the RGB code.

I have also refactored the SDL and Glut components into separate files to focus more on the Kinect specifics.

Contents

  1. Kinect Code
  2. Display Frameworks

Kinect Code

Kinect Initialization

To get the depth data from the kinect, simply change the types of the framesource, framereader, and frame. Note also that the resolution of the depth camera is different from that of the color camera: 512*424 instead of 1920*1080.
IDepthFrameReader* reader;     // Kinect depth data source

bool initKinect() {
    if (FAILED(GetDefaultKinectSensor(&sensor))) {
        return false;
    }
    if (sensor) {
        sensor->Open();
        IDepthFrameSource* framesource = NULL;
        sensor->get_DepthFrameSource(&framesource);
        framesource->OpenReader(&reader);
        if (framesource) {
            framesource->Release();
            framesource = NULL;
        }
        return true;
    } else {
        return false;
    }
}
        

Getting a depth frame from the Kinect

We'll display the depth image from the kinect in grayscale. Each pixel will just be the pixel's distance from the kinect (in millimeters) mod 256.

void getKinectData(GLubyte* dest) {
    IDepthFrame* frame = NULL;
    if (SUCCEEDED(reader->AcquireLatestFrame(&frame))) {
        unsigned int sz;
        unsigned short* buf;
        frame->AccessUnderlyingBuffer(&sz, &buf);

        const unsigned short* curr = (const unsigned short*)buf;
        const unsigned short* dataEnd = curr + (width*height);

        while (curr < dataEnd) {
            // Get depth in millimeters
            unsigned short depth = (*curr++);

            // Draw a grayscale image of the depth:
            // B,G,R are all set to depth%256, alpha set to 1.
            for (int i = 0; i < 3; ++i)
                *dest++ = (BYTE)depth % 256;
            *dest++ = 0xff;
        }
    }
    if (frame) frame->Release();
}
        
Note that, unlike with the color frame, we just want to access the raw data from the frame. To do this, we use frame->AccessUnderlyingBuffer to get a pointer to the data.
Then we convert the depth data and write it to our image array.
That's all the Kinect code! The rest is just how to get it onscreen.

Display Frameworks

I refactored the code so that the glut and SDL specifics are bundled up into an init() function, a draw() function, and an execute() function.

To use one or the other, simply change the include at the top of main.cpp to use either sdl.h or glut.h. Make sure you have the appropriate includes and links in the Project Properties!


The End! Build and run, making sure that your Kinect is plugged in. You should see a window containing a video stream of what your Kinect sees, in grayscale.
Previous: Basics

Next: Kinect Point Cloud