Kinect SDK 1 - The Player Index
Article Index
Kinect SDK 1 - The Player Index
Displaying the data
Color mapping

Color mapping

Clearly if you want to visualize the user index data it has to be processed to a more distinct set of colors corresponding to 0 to 7,

The sample program that comes with the SDK uses a hand coded color mapping for each of the 7 colors. A much simpler solution is to map each of the three bits to the three colours R G and B. That is if bit 1 is set then set R to 0xFF i.e. full Red. if bit 2 is set then set G to 0xFF i.e. full green and finally if bit 3 is set then set B to 0xFF i.e. full blue. If any combination of the bits is set then you get a mixture of colors i.e. user 7 gives white.

To implement this we need a new array:

int[] playercoded = new 
int[imageFrame.PixelDataLength];

and the for loop now becomes:

for (int i = 0; i < depth.Length; i++)
{
player[i] = pixelData[i] &
DepthImageFrame.PlayerIndexBitmask;
depth[i] = ((ushort) pixelData[i])>>
DepthImageFrame.PlayerIndexBitmaskWidth;
playercoded[i] = 0;
if ((player[i] & 0x01) == 0x01)
playercoded[i] |= 0xFF0000;
if ((player[i] & 0x02) == 0x02)
playercoded[i] |= 0x00FF00;
if ((player[i] & 0x04) == 0x04)
playercoded[i] |= 0x0000FF;
}

And the line to display the user index is changed to:

pictureBox2.Image = IntToBitmap(
playercoded, Image.Width, Image.Height);

and now you should see player 1 appear as a red silhouette.

index

 

WPF

If you are working with WPF then the only change is that the IntToBitmap conversion routine which now has to use a BitmapSource object instead:

BitmapSource IntToBitmap(int[] array, 
int w, int h)
{
BitmapSource bmap = BitmapSource.Create(
w,
h,
96, 96,
PixelFormats.Bgr32,
null,
array,
w * 4);
return bmap;
}

The only other change needed is to use Image controls rather than PictureBox controls and:

  image1.Source = IntToBitmap(depth, 
imageFrame.Width, imageFrame.Height);
image2.Source = IntToBitmap(playercoded,
imageFrame.Width, imageFrame.Height);
}
}

The program now works in exactly the same way as the Windows Forms version.

An alignment problem

Now we are ready to use the player index as a mask to pick out the player in the video stream. However there is an interesting problem that we have to solve first.

If you simply try to use the player index or the depth field as a mask on the video stream you will discover that they are slightly out of alignment. If you explore this you will also discover that that the amount that they are out of alignment varies according to the distance the user is from the Kinect.

The reason for this is that the video and depth cameras are in slightly different locations and therefore have a different view point. The pixels in the depth and video image don't match up in a simple way.

There is a solution but this is explained in the next chapter.

 

You can download the code for both the Windows Forms version and the WPF version in a single ZIP file from the CodeBin (note you have to register first).

Practical Windows Kinect in C#
Chapter List

  1. Introduction to Kinect
  2. Getting started with Microsoft Kinect SDK 1
  3. Using the Depth Sensor
  4. The Player Index
  5. Depth and Video Space
  6. Skeletons
  7. The Full Skeleton
  8. A 3D Point Cloud

To be informed about new articles on I Programmer, subscribe to the RSS feed, follow us on Google+, Twitter, Linkedin or Facebook or sign up for our weekly newsletter..