Page 3 of 3
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
and the for loop now becomes:
for (int i = 0; i < depth.Length; i++)
player[i] = pixelData[i] &
depth[i] = ((ushort) pixelData[i])>>
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.
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 * 4);
The only other change needed is to use Image controls rather than PictureBox controls and:
image1.Source = IntToBitmap(depth,
image2.Source = IntToBitmap(playercoded,
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#
- Introduction to Kinect
- Getting started with Microsoft Kinect SDK 1
- Using the Depth Sensor
- The Player Index
- Depth and Video Space
- The Full Skeleton
- 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..