Wpf 检测并显示使用Kinect检测到的玩家数量

Wpf 检测并显示使用Kinect检测到的玩家数量,wpf,kinect,kinect-sdk,kinect.toolbox,Wpf,Kinect,Kinect Sdk,Kinect.toolbox,我正在尝试使用WPF应用程序显示Kinect传感器检测到的玩家数量。除了显示播放器的数量外,我还根据像素与Kinect的距离为其着色。最初的目标是测量像素的距离并显示距离,但我也想显示帧中有多少人。下面是我现在使用的代码片段 PS我从教程中借用了这个想法,我正在使用SDK 1.8和XBOX 360 Kinect(1414) private void\u sensor\u AllFramesReady(对象发送器,AllFramesReadyEventArgs e) { 使用(DepthImage

我正在尝试使用WPF应用程序显示Kinect传感器检测到的玩家数量。除了显示播放器的数量外,我还根据像素与Kinect的距离为其着色。最初的目标是测量像素的距离并显示距离,但我也想显示帧中有多少人。下面是我现在使用的代码片段

PS我从教程中借用了这个想法,我正在使用SDK 1.8和XBOX 360 Kinect(1414)

private void\u sensor\u AllFramesReady(对象发送器,AllFramesReadyEventArgs e)
{
使用(DepthImageFrame depthFrame=e.OpenDepthImageFrame())
{
if(depthFrame==null)
{
返回;
}
字节[]像素=生成彩色字节(深度帧);
int stride=深度帧。宽度*4;
image.Source=BitmapSource.Create(depthFrame.Width、depthFrame.Height、,
96,96,PixelFormats.Bgr32,null,像素,步幅);
}
}
专用字节[]生成彩色字节(DepthImageFrame depthFrame)
{   
//从kinect获取每个像素深度的原始数据
short[]rawDepthData=新的short[depthFrame.PixelDataLength];
CopyPixelDataTo(rawDepthData);
/*
使用depthFrame创建要在屏幕上显示的图像
depthFrame包含图像中所有像素的颜色信息
*/
//高度*宽度*4(红、绿、蓝、空字节)
字节[]像素=新字节[depthFrame.Height*depthFrame.Width*4];
//蓝、绿、红(BGR)指数头寸的硬编码波动
const int BlueIndex=0;
常数int GreenIndex=1;
常数int RedIndex=2;
//循环所有距离,并根据距离选择RGB颜色
对于(int-depthIndex=0,colorIndex=0;
深度索引DepthImageFrame.PlayerIndexBitmaskWidth;
//.9M或2.95'
如果(深度900和深度2000)
{
//遥远的
像素[颜色索引+蓝色索引]=0;
像素[颜色索引+绿色索引]=255;
像素[colorIndex+RedIndex]=0;
}
//给所有人涂上金色
如果(玩家>0)
{
像素[colorIndex+BlueIndex]=Colors.Gold.B;
像素[colorIndex+GreenIndex]=Colors.Gold.G;
像素[colorIndex+RedIndex]=Colors.Gold.R;
playerValue.Text=player.ToString();
}
}
返回像素;
}
目前的目标是--

  • 检测检测到的播放机总数,并将其显示在
    文本框中

  • 根据距离逻辑给它们上色,即
    深度在表单代码中获取一个静态变量

    然后使用视频帧例程设置此变量(不要在此处定义)

    然后更新文本框视图,可能在您的_sensor _allframes中更新 当新帧到达时,运行在不同的线程中 我没有看到更新call textbox.show的所有代码

    主循环看起来有点奇怪,太复杂了。 基本上你可以用它来给图像中的每个像素上色。 由于kinect360具有320x240像素,因此形成了尺寸为76800的深度阵列 您可以简单地为X和Y的下一个循环创建2个循环,然后在此循环中增加一个变量来选择适当的深度值

    private void _sensor_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
            {
                if (depthFrame==null)
                {
                    return;
                }
                byte[] pixels = GenerateColoredBytes(depthFrame);
                int stride = depthFrame.Width * 4;
                image.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 
                96, 96, PixelFormats.Bgr32, null, pixels, stride);
            }
        }
    
    private byte[] GenerateColoredBytes(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 * Width *4 (Red, Green, Blue, Empty byte)
            Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4];
            //Hardcoded loactions for Blue, Green, Red (BGR) index positions
            const int BlueIndex = 0;
            const int GreenIndex = 1;
            const int RedIndex = 2;
            //Looping through all distances and picking a RGB colour based on distance
    
            for (int depthIndex = 0, colorIndex = 0; 
                depthIndex < rawDepthData.Length && 
                colorIndex<pixels.Length; depthIndex++, colorIndex+=4)
            {
                //Getting player
                int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;
    
                //Getting depth value
                int depth =rawDepthData[depthIndex]>>DepthImageFrame.PlayerIndexBitmaskWidth;
    
                //.9M or 2.95'
                if (depth <=900 )
                {
                    //Close distance
                    pixels[colorIndex + BlueIndex] = 0;
                    pixels[colorIndex + GreenIndex] = 0;
                    pixels[colorIndex + RedIndex] = 255;
                    //textBox.Text = "Close Object";
                }
    
                //.9M - 2M OR 2.95' - 6.56'
                else if (depth >900 && depth<2000)
                {
                    //Bit further away
                    pixels[colorIndex + BlueIndex] = 255;
                    pixels[colorIndex + GreenIndex] = 0;
                    pixels[colorIndex + RedIndex] = 0;
                }
    
                else if (depth > 2000)
                {
                    //Far away
                    pixels[colorIndex + BlueIndex] = 0;
                    pixels[colorIndex + GreenIndex] = 255;
                    pixels[colorIndex + RedIndex] = 0;
                }
    
                //Coloring all people in Gold
                if (player > 0)
                {
                    pixels[colorIndex + BlueIndex] = Colors.Gold.B;
                    pixels[colorIndex + GreenIndex] = Colors.Gold.G;
                    pixels[colorIndex + RedIndex] = Colors.Gold.R;
                    playersValue.Text = player.ToString();
                }
            }
            return pixels;
        }