彩色图像流、深度数据和骨骼点跟踪的集合处理

来源:互联网 发布:长信基金怎么样 知乎 编辑:程序博客网 时间:2024/06/05 18:10

.cs文件代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
namespace Kinect
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
KinectSensor _kinect;

    const float MaxDepthDistance = 4095;//深度图像最大视距    const float MinDepthDiatance = 850;//深度图像最小视距    const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDiatance;    //BGR32图像,红色、绿色、蓝色分别对应的偏移(32位,4字节中的第几个)    private const int RedIndex = 2;    private const int GreenIndex = 1;    private const int BlueIndex = 0;    //定义骨骼帧数组    private Skeleton[] skeletons;    /// <summary>    ///单色直方图计算公式,返回256色灰阶。颜色越黑则越远,越白则越近    ///将MaxDepthDistanceOffset数值控制在0~255的范围内    /// </summary>    /// <param name="distance">深度值,有效范围MinDepthDistance到MaxDepthDistance</param>    /// <returns>256色灰阶</returns>    private static byte CalculateIntensityFromDepth(int distance)    {        return (byte)(255 - (255 * Math.Max(distance - MinDepthDiatance, 0) / (MaxDepthDistance)));    }    /// <summary>    /// 生成BGR32格式的图片字节数组    /// </summary>    /// <param name="depthFrame"></param>    /// <returns></returns>    private byte[] convertDepthFrameToColorFrame(DepthImageFrame depthFrame)    {        //从深度图像帧中获取原始数据        short[] rawDepthData = new short[depthFrame.PixelDataLength];        depthFrame.CopyPixelDataTo(rawDepthData);        //创建Height X Width X 4 的RGB数组(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        //需要为Bgra32设置透明度(transparency),.NET默认设置该字节为0(全透明)        for(int depthIndex = 0,colorIndex = 0;depthIndex < rawDepthData.Length && colorIndex<pixels.Length;depthIndex++,colorIndex  += 4)        {            //用户分割, 0代表该像素不属于用户身体,低于3位字节表示被追踪的使用者的索引编号            int player = rawDepthData[depthIndex & DepthImageFrame.PlayerIndexBitmask];            //获得深度数值,高13位字节            int depth = rawDepthData[depthIndex]>>DepthImageFrame.PlayerIndexBitmaskWidth;            //0.9米内            if(depth <= 900)            {                //离Kinect很近                pixels[colorIndex + BlueIndex] = 255;                pixels[colorIndex + GreenIndex] = 0;                pixels[colorIndex + RedIndex] = 0;            }            //0.9~2米之间            else if(depth > 900 && depth < 2000)            {                pixels[colorIndex + BlueIndex] = 0;                pixels[colorIndex + GreenIndex] = 255;                pixels[colorIndex + RedIndex] = 0;            }            // 2米+            else if(depth > 2000)            {                //离Kinect超过2米                pixels[colorIndex + BlueIndex] = 0;                pixels[colorIndex + GreenIndex] = 0;                pixels[colorIndex + RedIndex] = 255;            }            //单色直方图着色            byte intensity = CalculateIntensityFromDepth(depth);            pixels[colorIndex + BlueIndex] = intensity;            pixels[colorIndex + GreenIndex] = intensity;            pixels[colorIndex + RedIndex] = intensity;            //如果是人体区域,用“亮绿色标注”            if(player > 0)            {                pixels[colorIndex + BlueIndex] = Colors.LightGreen.B;                pixels[colorIndex + GreenIndex] = Colors.LightGreen.G;                pixels[colorIndex + RedIndex] = Colors.LightGreen.R;            }        }        return pixels;    }    /// <summary>    /// 启动Kinect设备,初始化选项,并注册彩色图像,深度图像和骨骼跟踪事件    /// </summary>    private void startKinect()    {        if(KinectSensor.KinectSensors.Count > 0)        {            //选择第一个Kinect设备            _kinect = KinectSensor.KinectSensors[0];            MessageBox.Show("Kinect目前状态为:" + _kinect.Status);            //初始化设定,启用彩色图像、深度图像和骨骼跟踪            _kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);            _kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);            _kinect.SkeletonStream.Enable();            //注册事件            _kinect.DepthFrameReady += new EventHandler<DepthImageFrameReadyEventArgs>(_kinect_DepthFrameReady);            _kinect.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(_kinect_ColorFrameReady);            _kinect.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(_kinect_SkeletonFrameReady);            //启动Kinect设备            _kinect.Start();        }        else        {            MessageBox.Show("没有发现任何Kinect设备");        }    }    private void stopKinect()    {        if (_kinect != null)        {            if (_kinect.Status == KinectStatus.Connected)            {                _kinect.Stop();            }        }    }    /// <summary>    /// 定义彩色图像事件    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void _kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)    {        //显示彩色摄像头        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())        {            if (colorFrame == null)            {                return;            }            byte[] pixels = new byte[colorFrame.PixelDataLength];            colorFrame.CopyPixelDataTo(pixels);            //BGR32格式图片一个像素为4个字节            int stride = colorFrame.Width * 4;            imageCanera.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height,                96, 96, PixelFormats.Bgr32, null, pixels, stride);        }    }    /// <summary>    /// 定义深度图像事件    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void _kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)    {        using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())        {            if (depthFrame == null)            {                return;            }            byte[] pixels = convertDepthFrameToColorFrame(depthFrame);            //BGR图片的步长            int stride = depthFrame.Width * 4;            //创建BRG32格式的图片            imageDepth.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);        }    }    /// <summary>    /// 定义骨骼跟踪事件    /// </summary>    /// <param name="sender"></param>    /// <param name="e"></param>    private void _kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)    {        bool isSkeletonDataReady = false;        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())        {            if(skeletonFrame != null)            {                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];                skeletonFrame.CopySkeletonDataTo(skeletons);                isSkeletonDataReady = true;            }            if (isSkeletonDataReady)            {                //从返回的骨骼对象集合中,定位第一个被跟踪的骨骼                Skeleton currentSkeleton = (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault();                if (currentSkeleton != null)                {                    //调用20个函数锁定20个骨骼点。                    lockHeadWithRedSpot(currentSkeleton);                    lockShoulderCenterWithRedSpot(currentSkeleton);                    lockShoulderLeftWithRedSpot(currentSkeleton);                    lockShoulderRightWithRedSpot(currentSkeleton);                    lockElbowLeftWithRedSpot(currentSkeleton);                    lockElbowRightWithRedSpot(currentSkeleton);                    lockWristLeftWithRedSpot(currentSkeleton);                    lockWristRightWithRedSpot(currentSkeleton);                    lockHandLeftWithRedSpot(currentSkeleton);                    lockHandRightWithRedSpot(currentSkeleton);                    lockSpineWithRedSpot(currentSkeleton);                    lockHipCenterWithRedSpot(currentSkeleton);                    lockHipLeftWithRedSpot(currentSkeleton);                    lockHipRightWithRedSpot(currentSkeleton);                    lockKneeLeftWithRedSpot(currentSkeleton);                    lockKneeRightWithRedSpot(currentSkeleton);                    lockAnkleLeftWithRedSpot(currentSkeleton);                    lockAnkleRightWithRedSpot(currentSkeleton);                    lockFootLeftWithRedSpot(currentSkeleton);                    lockFootRightWithRedSpot(currentSkeleton);                }            }        }    }     /// <summary>    /// 锁定头部骨骼点    /// </summary>    /// <param name="s"></param>    private void lockHeadWithRedSpot(Skeleton s)    {        Joint head = s.Joints[JointType.Head];        //将头部的骨骼点坐标映射到彩色视频流坐标中        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(head.Position, _kinect.ColorStream.Format);        //将坐标位置等比例显示缩小        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        //画布的位置与imageCamera重叠        Canvas.SetLeft(ellipseShoulderCenter, p.X);        Canvas.SetTop(ellipseShoulderCenter, p.Y);    }    private void lockHandRightWithRedSpot(Skeleton s)    {        Joint handRight = s.Joints[JointType.HandRight];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(handRight.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseHandRight, p.X);        Canvas.SetTop(ellipseHandRight, p.Y);    }    private void lockHandLeftWithRedSpot(Skeleton s)    {        Joint handLeft = s.Joints[JointType.HandLeft];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(handLeft.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseHandLeft, p.X);        Canvas.SetTop(ellipseHandLeft, p.Y);    }    private void lockWristRightWithRedSpot(Skeleton s)    {        Joint wristRight = s.Joints[JointType.WristRight];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(wristRight.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseWristRight, p.X);        Canvas.SetTop(ellipseWristRight, p.Y);    }    private void lockWristLeftWithRedSpot(Skeleton s)    {        Joint wristLeft = s.Joints[JointType.WristLeft];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(wristLeft.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseWristLeft, p.X);        Canvas.SetTop(ellipseWristLeft, p.Y);    }    private void lockElbowRightWithRedSpot(Skeleton s)    {        Joint elbowRight = s.Joints[JointType.ElbowRight];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(elbowRight.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseElbowRight, p.X);        Canvas.SetTop(ellipseElbowRight, p.Y);    }    private void lockElbowLeftWithRedSpot(Skeleton s)    {        Joint elbowLeft = s.Joints[JointType.ElbowLeft];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(elbowLeft.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseElbowLeft, p.X);        Canvas.SetTop(ellipseElbowLeft, p.Y);    }    private void lockShoulderLeftWithRedSpot(Skeleton s)    {        Joint shoulderLeft = s.Joints[JointType.ShoulderLeft];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(shoulderLeft.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseShoulderLeft, p.X);        Canvas.SetTop(ellipseShoulderLeft, p.Y);    }    private void lockShoulderRightWithRedSpot(Skeleton s)    {        Joint shoulderRight = s.Joints[JointType.ShoulderRight];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(shoulderRight.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseShoulderRight, p.X);        Canvas.SetTop(ellipseShoulderRight, p.Y);    }    private void lockShoulderCenterWithRedSpot(Skeleton s)    {        Joint shoulderCenter = s.Joints[JointType.ShoulderCenter];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(shoulderCenter.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseShoulderCenter, p.X);        Canvas.SetTop(ellipseShoulderCenter, p.Y);    }    private void lockSpineWithRedSpot(Skeleton s)    {        Joint spine = s.Joints[JointType.Spine];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(spine.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseSpine, p.X);        Canvas.SetTop(ellipseSpine, p.Y);    }    private void lockHipCenterWithRedSpot(Skeleton s)    {        Joint hipCenter = s.Joints[JointType.HipCenter];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(hipCenter.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseHipCenter, p.X);        Canvas.SetTop(ellipseHipCenter, p.Y);    }    private void lockHipLeftWithRedSpot(Skeleton s)    {        Joint hipLeft = s.Joints[JointType.HipLeft];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(hipLeft.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseHipLeft, p.X);        Canvas.SetTop(ellipseHipLeft, p.Y);    }    private void lockHipRightWithRedSpot(Skeleton s)    {        Joint hipRight = s.Joints[JointType.HipRight];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(hipRight.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseHipRight, p.X);        Canvas.SetTop(ellipseHipRight, p.Y);    }    private void lockKneeLeftWithRedSpot(Skeleton s)    {        Joint kneeLeft = s.Joints[JointType.KneeLeft];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(kneeLeft.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseKneeLeft, p.X);        Canvas.SetTop(ellipseKneeLeft, p.Y);    }    private void lockKneeRightWithRedSpot(Skeleton s)    {        Joint kneeRight = s.Joints[JointType.KneeRight];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(kneeRight.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseKneeRight, p.X);        Canvas.SetTop(ellipseKneeRight, p.Y);    }    private void lockAnkleLeftWithRedSpot(Skeleton s)    {        Joint ankleLeft = s.Joints[JointType.AnkleLeft];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(ankleLeft.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseAnkleLeft, p.X);        Canvas.SetTop(ellipseAnkleLeft, p.Y);    }    private void lockAnkleRightWithRedSpot(Skeleton s)    {        Joint ankleRight = s.Joints[JointType.AnkleRight];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(ankleRight.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseAnkleRight, p.X);        Canvas.SetTop(ellipseAnkleRight, p.Y);    }    private void lockFootLeftWithRedSpot(Skeleton s)    {        Joint footLeft = s.Joints[JointType.FootLeft];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(footLeft.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseFootLeft, p.X);        Canvas.SetTop(ellipseFootLeft, p.Y);    }    private void lockFootRightWithRedSpot(Skeleton s)    {        Joint footRight = s.Joints[JointType.FootRight];        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(footRight.Position, _kinect.ColorStream.Format);        Point p = new Point(            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));        Canvas.SetLeft(ellipseFootRight, p.X);        Canvas.SetTop(ellipseFootRight, p.Y);    }    public MainWindow()    {        InitializeComponent();    }    //窗体启动事件    private void Window_Loaded(object sender, RoutedEventArgs e)    {        startKinect();    }    private void Window_Closed(object sender, EventArgs e)    {        stopKinect();    }}

}

0 0
原创粉丝点击