i want to track only one person through the kinect and i wanna track its skeletal data and at the same time i want to show the depth from containing only that player but not the other players.
Attached here is the code responsible for that, CAN anyone HELP ?!!
void mySensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
if (closing)
return;
using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
{
if (depthFrame == null)
{
return;
}
byte[] pixels = GenerateDepthImage(depthFrame);
int stride = depthFrame.Width * 4;
depthImage.Source =
BitmapSource.Create(depthFrame.Width, depthFrame.Height,
96, 96, PixelFormats.Bgra32, null, pixels, stride);
}
//Get a skeleton
Skeleton first = GetFirstSkeleton(e);
ProcessSkeletalData(first, e);
}
and here is the method of generating depth image:
private byte[] GenerateDepthImage(DepthImageFrame depthFrame)
{
//get the raw data from kinect with the depth for every pixel
short[] rawDepthData = new short[depthFrame.PixelDataLength];
depthFrame.CopyPixelDataTo(rawDepthData);
//use depthFrame to create the image to display on-screen
//depthFrame contains color information for all pixels in image
//Height x Width x 4 (Red, Green, Blue, empty byte)
Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4];
//Bgr32 - Blue, Green, Red, empty byte
//Bgra32 - Blue, Green, Red, transparency
//You must set transparency for Bgra as .NET defaults a byte to 0 = fully transparent
//hardcoded locations to Blue, Green, Red (BGR) index positions
const int BlueIndex = 0;
const int GreenIndex = 1;
const int RedIndex = 2;
const int AlphaIndex = 3;
//loop through all distances
//pick a RGB color based on distance
for (int depthIndex = 0, colorIndex = 0;
depthIndex < rawDepthData.Length && colorIndex < pixels.Length;
depthIndex++, colorIndex += 4)
{
//get the player (requires skeleton tracking enabled for values)
int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;
//gets the depth value
int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;
pixels[colorIndex + BlueIndex] = 255;
pixels[colorIndex + GreenIndex] = 255;
pixels[colorIndex + RedIndex] = 255;
pixels[colorIndex + AlphaIndex] = 0;
//Color all players
//Debug.WriteLine(player);
if (player > 0 )
{
pixels[colorIndex + BlueIndex] = 0;
pixels[colorIndex + GreenIndex] = 0;
pixels[colorIndex + RedIndex] = 0;
pixels[colorIndex + AlphaIndex] = 40;
}
}
return pixels;
}
The code of GetFirstSkeleton MEthod
Skeleton GetFirstSkeleton(AllFramesReadyEventArgs e)
{
using (SkeletonFrame skeletonFrameData = e.OpenSkeletonFrame())
{
if (skeletonFrameData == null)
return null;
skeletonFrameData.CopySkeletonDataTo(allSkeletons);
//get the first tracked skeleton
Skeleton first = (from s in allSkeletons
where s.TrackingState == SkeletonTrackingState.Tracked
select s).FirstOrDefault();
return first;
}
}
The problem is here i have all the players UP TO 6 players detected by the depth image while only one by the skeleton tracking and i want to have only one in both, the same player.
When i changed from player > 0 to player ==1 it didn't work because the player is not always with id 1.
Any idea how to solve the matter ?!
Thanks a lot, Michael