Kinect SDK 1 - The Player Index
Written by Harry Fairhead   
Monday, 05 March 2012
Article Index
Kinect SDK 1 - The Player Index
Displaying the data
Color mapping

Extracting the player index

Separating the two pieces of information is fairly easy. You can use a logical mask to extract the lower three bits and then use right shifts to normalize the depth data and to get rid of the player index bits.

That is if s is a short value we can extract the player index using:

user index = s & 0x07;

and the depth using

depth = ((ushort) s )>>3;

The x07 is just 111 in binary and it masks out the bottom three bits. You can use

DepthImageFrame.PlayerIndexBitmask

in place of x07 if you want to and

DepthImageFrame.PlayerIndexBitmaskWidth;

in place of the three in the shift.

If you don't follow the shift expression you need to remember that  a short can be a positive or a negative value and you need to shift the bits down using a logical and not an arithmetic shift. To do this you need to cast the value to a (ushort) i.e. unsigned short.

Putting this together we can now start to process the data into something more usable. As a first step let's separate the data into two arrays:

int[] depth = new int[
imageFrame.PixelDataLength];
int[] player = new int[
imageFrame.PixelDataLength];

we need to scan through the short array and extract the data:

for (int i = 0; i < depth.Length; i++)
{
player[i] = pixelData[i] &
DepthImageFrame.PlayerIndexBitmask;
depth[i] = ((ushort) pixelData[i])>>
DepthImageFrame.PlayerIndexBitmaskWidth;
}

Displaying the data - Windows Forms

Now we have the player data and the depth data in int arrays.

These can be converted into Bitmap objects in the usual way. The depth data can be used as described in the previous article and the only change is that now it is in an int array. To convert an int array to a Bitmap the basic scheme is the same only now you use a different overload of the Copy method

Bitmap IntToBitmap(int[] array,
int w,int h)
{
Bitmap bmap = new Bitmap(
w,
h,
PixelFormat.Format32bppRgb);
BitmapData bmapdata = bmap.LockBits(
new Rectangle(0, 0, w,h),
ImageLockMode.WriteOnly,
bmap.PixelFormat);
IntPtr ptr = bmapdata.Scan0;
Marshal.Copy(array,
0,
ptr,
array.Length);
bmap.UnlockBits(bmapdata);
return bmap;
}

If you place a PictureBox on the form you can view the depth data using

pictureBox1.Image = IntToBitmap(depth, 
imageFrame.Width,
  imageFrame.Height);

depth

Notice that this isn't a good mapping of depth number to color because only the lower 13 bits are used by the depth measurement and these are mapped to green and blue - red is hardly used.

The same method can be use to convert the user index and if you place a second PictureBox on the form you can view this data using:

pictureBox2.Image = IntToBitmap(player,
 imageFrame.Width,
imageFrame.Height);

But if you do try to view this you will most likely see just a black screen. The reason is that the player index runs from 0 - black to 7, which is a very dark blue in this color mapping.



Last Updated ( Friday, 27 April 2012 )
 
 

   
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.