Player index with depth data

So far, we have seen how to work with the raw depth data from the sensor and manipulate the data based on the distance. In this section you will learn how Kinect returns the player information and how to deal with the player who is standing in front of the Kinect sensor.

While we were discussing depth data and distance, you have seen that for a 16-bit raw depth data, the first three bits represent the player index and the higher 13 bits represent the distance. You have already learned how the distance calculation works with those higher 13 bits; let's have a look at how those first three bits represent a player.

Note

Player tracking requires the skeleton stream to be enabled. If you have enabled only the depth stream, the sensor won't be able to return the player information. The sensor returns the player index values within the depth pixel bits only if the skeleton stream is enabled. We will discuss skeleton tracking in the upcoming chapters.

How player index works

A Kinect sensor can detect up to six players, numbered one to six. A pixel with a player index value of 0 means there is no player recognized. For calculating the player index value, we do a logical AND operation with the pixel value and PlayerIndexBitmask. PlayerIndexBitmask is a constant defined in the DepthImageStream class, which represents a fixed value 7. So, there is a logical AND operation between these two values.

The following diagram shows the player index calculation with a pixel data value 10001010011110 (pixel value 8862) along with the player index:

How player index works

Well, that was all about the theoretical concepts of the player index calculation. Let's have a look at how to work with the player index using code.

Identifying players

To get the player index and change the color values, you need to perform an operation similar to the one you did for the colorization of depth data. The only difference is the way PlayerIndexBitmask is calculated, and highlighting the player pixels with different colors.

private void TrackPlayer(short[] depthFrame)
{
    for (int depthIndex = 0, colorIndex = 0; depthIndex < depthFrame.Length && colorIndex < this.depth32.Length; depthIndex++, colorIndex += 4)
    {
        int player = depthFrame[depthIndex] & DepthImageFrame.PlayerIndexBitmask;
        if (player > 0)
        {
            depth32[colorIndex + 2] = 169;
            depth32[colorIndex + 1] = 62;
            depth32[colorIndex + 0] = 9;
        }
    }
}

This will change the value of all the pixels associated with a player to brown and will set the background as black as we haven't set any colors for other pixels. The following screenshot shows the output of this code:

Identifying players

As per the previous code, it will set the same color to all the tracked players. You can track different players based on the player index, which will be covered in the next chapter.

Tip

Capturing color and depth data together

You can build an application that can enable both the color and the depth stream at the same time, which means you can capture both the color and the depth data at the same time. If you want to use both the cameras, we just have to enable both streams:

sensor.DepthStream.Enable();
sensor.ColorStream.Enable();

Then you can just attach the event handler for the stream and process the data inside the event handler:

sensor.ColorFrameReady += sensor_ColorFrameReady;
sensor.DepthFrameReady +=sensor _DepthFrameReady;

Though using color and depth data streams is usually very common and most of your applications would require that, here is one interesting scenario where you can use both of them very nicely. Consider you are a building a home security solution and you are using Kinect as an intrusion detector. You can track the intruder by identifying the player index values, and based on the distance, check how far the intruder is; if it's coming near the sensor, capture a color photo and save it. You can also take advantage of the infrared stream, if you want to track the same in low lights.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.154.171