Kinect v2

资料
硬件部分连接上即可,SDK从微软官网下载,for windows 2.0 ,安装完毕后一定要重启
运行SDK Browser v2.0 (Kinect for Windows) 检查设备
运行Kinect Studio v2.0演示效果和 查看文档。
c#文档
vs2015中建立WPF APPLICATION 注意x:Class="xxxx.yyyyy"
xxxx = namespace名称 ,yyyyy = class名称
注意添加kinect presentioncore
资料

WPF中窗口添加对应事件


Kinect v2_第1张图片
image.png

image.png

多源使用时 要用multsource
打开 kinectsensor 建立reader 建立description framearrived += 订阅事件

WPF中
修改Name 的名称,对应修改 image1.Source 即可添加第二个image

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;
using System.ComponentModel;
using System.Diagnostics;

namespace 同时显示
{
    
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        //体感器设备  
        private KinectSensor sensor = null;
        //彩色帧读取变量  
        private MultiSourceFrameReader reader;
        /// ///////////////////////////
        /// 彩色变量定义///////////////////////////
        /// ////////////////////////////////
        //彩色帧描述  
        private FrameDescription colorFrameDescription = null;
        //位图对象  
        private WriteableBitmap colorBitmap;
        //存放一帧彩色图像的矩形框  
        private Int32Rect colorBitmapRect;
        //步长,一维彩色像素数组转化为矩形框的步长。  
        private int colorBitmapStride;
        //存放一帧彩色图像像素  
        private Byte[] colorPixelData;
        /// ///////////////////////////////
        /// ///深度变量定义///////////////////////////
        /// //////////////////////////////     
        private FrameDescription depthFrameDescription = null;
        private WriteableBitmap depthBitmap;
        //存放一帧深度图像的矩形框  
        private Int32Rect depthBitmapRect;
        //深度像素数组转化为矩形框的长度  
        private int depthStride;
        //深度图像像素数据  
        private ushort[] depthPixelData;
        /// //////////////////////////////////////////////////
        /// ///骨骼变量定义////////////////////////////////////////////
        /// ////////////////////////////////////////////////
        private FrameDescription bodyFrameDescription = null;
        private const double HandSize = 30;  //手部圆圈大小

        /// 
        /// Thickness of drawn joint lines
        /// 
        private const double JointThickness = 3;//关节点圆圈大小

        /// 
        /// Thickness of clip edge rectangles
        /// 
        private const double ClipBoundsThickness = 20;//视野边界 警告区域

        /// 
        /// Constant for clamping Z values of camera space points from being negative
        /// 
        private const float InferredZPositionClamp = 0.1f;

        /// 
        /// Brush used for drawing hands that are currently tracked as closed
        /// 
        private readonly Brush handClosedBrush = new SolidColorBrush(Color.FromArgb(128, 255, 0, 0));//手掌闭合 颜色显示

        /// 
        /// Brush used for drawing hands that are currently tracked as opened
        /// 
        private readonly Brush handOpenBrush = new SolidColorBrush(Color.FromArgb(128, 0, 255, 0));//手掌打开 颜色显示

        /// 
        /// Brush used for drawing hands that are currently tracked as in lasso (pointer) position
        /// 
        private readonly Brush handLassoBrush = new SolidColorBrush(Color.FromArgb(128, 0, 0, 255));  //不懂!!!!

        /// 
        /// Brush used for drawing joints that are currently tracked
        /// 
        private readonly Brush trackedJointBrush = new SolidColorBrush(Color.FromArgb(255, 68, 192, 68));//关节点 颜色

        /// 
        /// Brush used for drawing joints that are currently inferred
        ///         
        private readonly Brush inferredJointBrush = Brushes.Yellow;

        /// 
        /// Pen used for drawing bones that are currently inferred
        ///         
        private readonly Pen inferredBonePen = new Pen(Brushes.Gray, 1);

        /// 
        /// Drawing group for body rendering output
        /// 
        private DrawingGroup drawingGroup;

        /// 
        /// Drawing image that we will display
        /// 
        private DrawingImage imageSource;

        /// 
        /// Coordinate mapper to map one type of point to another
        /// 
        private CoordinateMapper coordinateMapper = null;

        /// 
        /// Reader for body frames
        /// 
        private BodyFrameReader bodyFrameReader = null;

        /// 
        /// Array for the bodies
        /// 
        private Body[] bodies = null;

        /// 
        /// definition of bones
        /// 
        private List> bones;

        /// 
        /// Width of display (depth space)
        /// 
        private int displayWidth;

        /// 
        /// Height of display (depth space)
        /// 
        private int displayHeight;

        /// 
        /// List of colors for each body tracked
        /// 
        private List bodyColors;



        private void Window_Loaded_1(object sender, RoutedEventArgs e)
        {
            this.sensor = KinectSensor.GetDefault();
            this.coordinateMapper = this.sensor.CoordinateMapper;
            this.reader = this.sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Color | FrameSourceTypes.Body);
            this.depthFrameDescription = this.sensor.DepthFrameSource.FrameDescription;
            this.colorFrameDescription = this.sensor.ColorFrameSource.FrameDescription;
            this.bodyFrameDescription = this.sensor.DepthFrameSource.FrameDescription;
            this.reader.MultiSourceFrameArrived += this.Reader_MultiSourceFrameArrived;

            this.displayWidth = this.bodyFrameDescription.Width;
            this.displayHeight = this.bodyFrameDescription.Height;
           
            /////////////////////               彩色                     /////////////////////////////////////

            this.colorPixelData = new Byte[colorFrameDescription.LengthInPixels * 4];
            this.colorBitmap = new WriteableBitmap(this.colorFrameDescription.Width,
            this.colorFrameDescription.Height, 96.0, 96.0, PixelFormats.Bgra32, null);
            this.colorBitmapRect = new Int32Rect(0, 0, this.colorBitmap.PixelWidth, this.colorBitmap.PixelHeight);
            this.colorBitmapStride = this.colorFrameDescription.Width * 4;

            image1.Source = this.colorBitmap;
            /////////////////////               深度                     /////////////////////////////////////

            this.depthPixelData = new ushort[this.depthFrameDescription.LengthInPixels];
            this.depthBitmap = new WriteableBitmap(this.depthFrameDescription.Width, this.depthFrameDescription.Height, 96.0, 96.0, PixelFormats.Gray16, null);
            this.depthBitmapRect = new Int32Rect(0, 0, this.depthBitmap.PixelWidth, this.depthBitmap.PixelHeight);
            this.depthStride = this.depthFrameDescription.Width * 2;

            image2.Source = this.depthBitmap;
            /////////////////////               骨骼                     /////////////////////////////////////

            this.bones = new List>();  //关节点之间画线

            this.bones.Add(new Tuple(JointType.Head, JointType.Neck));                    // Torso   
            this.bones.Add(new Tuple(JointType.Neck, JointType.SpineShoulder));
            this.bones.Add(new Tuple(JointType.SpineShoulder, JointType.SpineMid));
            this.bones.Add(new Tuple(JointType.SpineMid, JointType.SpineBase));
            this.bones.Add(new Tuple(JointType.SpineShoulder, JointType.ShoulderRight));
            this.bones.Add(new Tuple(JointType.SpineShoulder, JointType.ShoulderLeft));
            this.bones.Add(new Tuple(JointType.SpineBase, JointType.HipRight));
            this.bones.Add(new Tuple(JointType.SpineBase, JointType.HipLeft));
          
            this.bones.Add(new Tuple(JointType.ShoulderRight, JointType.ElbowRight));    // Right Arm
            this.bones.Add(new Tuple(JointType.ElbowRight, JointType.WristRight));
            this.bones.Add(new Tuple(JointType.WristRight, JointType.HandRight));
            this.bones.Add(new Tuple(JointType.HandRight, JointType.HandTipRight));
            this.bones.Add(new Tuple(JointType.WristRight, JointType.ThumbRight));
           
            this.bones.Add(new Tuple(JointType.ShoulderLeft, JointType.ElbowLeft));      // Left Arm
            this.bones.Add(new Tuple(JointType.ElbowLeft, JointType.WristLeft));
            this.bones.Add(new Tuple(JointType.WristLeft, JointType.HandLeft));
            this.bones.Add(new Tuple(JointType.HandLeft, JointType.HandTipLeft));
            this.bones.Add(new Tuple(JointType.WristLeft, JointType.ThumbLeft));
          
            this.bones.Add(new Tuple(JointType.HipRight, JointType.KneeRight));           // Right Leg
            this.bones.Add(new Tuple(JointType.KneeRight, JointType.AnkleRight));
            this.bones.Add(new Tuple(JointType.AnkleRight, JointType.FootRight));
           
            this.bones.Add(new Tuple(JointType.HipLeft, JointType.KneeLeft));             // Left Leg
            this.bones.Add(new Tuple(JointType.KneeLeft, JointType.AnkleLeft));
            this.bones.Add(new Tuple(JointType.AnkleLeft, JointType.FootLeft));
           
            this.bodyColors = new List();     // populate body colors, one for each BodyIndex                                                          

            this.bodyColors.Add(new Pen(Brushes.Red, 6));
            this.bodyColors.Add(new Pen(Brushes.Orange, 6));
            this.bodyColors.Add(new Pen(Brushes.Green, 6));
            this.bodyColors.Add(new Pen(Brushes.Blue, 6));
            this.bodyColors.Add(new Pen(Brushes.Indigo, 6));
            this.bodyColors.Add(new Pen(Brushes.Violet, 6));

            this.drawingGroup = new DrawingGroup();

            image3.Source = new DrawingImage(this.drawingGroup);

            this.sensor.Open();
        }

        public ImageSource ImageSource
        {
            get                                    //可删除                
            {
                return this.imageSource;
            }
        }


        private void Reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
        {
            //////////////////////////////////////骨骼事件处理
            bool dataReceived = false;
            MultiSourceFrameReference frameReference = e.FrameReference;
            MultiSourceFrame multiSourceFrame = null;
            multiSourceFrame = frameReference.AcquireFrame();
            using (BodyFrame bodyFrame = multiSourceFrame.BodyFrameReference.AcquireFrame())///这里是关键
            {
                if (bodyFrame != null)
                {
                    if (this.bodies == null)
                    {
                        this.bodies = new Body[bodyFrame.BodyCount];
                    }

                    // The first time GetAndRefreshBodyData is called, Kinect will allocate each Body in the array.
                    // As long as those body objects are not disposed and not set to null in the array,
                    // those body objects will be re-used.
                    bodyFrame.GetAndRefreshBodyData(this.bodies);
                    dataReceived = true;
                }
            }

            if (dataReceived)
            {
                using (DrawingContext dc = this.drawingGroup.Open())
                {
                    // Draw a transparent background to set the render size
                    dc.DrawRectangle(Brushes.Black, null, new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));

                    int penIndex = 0;
                    foreach (Body body in this.bodies)
                    {
                        Pen drawPen = this.bodyColors[penIndex++];

                        if (body.IsTracked)
                        {
                            this.DrawClippedEdges(body, dc);

                            IReadOnlyDictionary joints = body.Joints;

                            // convert the joint points to depth (display) space
                            Dictionary jointPoints = new Dictionary();

                            foreach (JointType jointType in joints.Keys)
                            {
                                // sometimes the depth(Z) of an inferred joint may show as negative
                                // clamp down to 0.1f to prevent coordinatemapper from returning (-Infinity, -Infinity)
                                CameraSpacePoint position = joints[jointType].Position;
                                if (position.Z < 0)
                                {
                                    position.Z = InferredZPositionClamp;
                                }

                                DepthSpacePoint depthSpacePoint = this.coordinateMapper.MapCameraPointToDepthSpace(position);
                                jointPoints[jointType] = new Point(depthSpacePoint.X, depthSpacePoint.Y);
                            }

                            this.DrawBody(joints, jointPoints, dc, drawPen);

                            this.DrawHand(body.HandLeftState, jointPoints[JointType.HandLeft], dc);
                            this.DrawHand(body.HandRightState, jointPoints[JointType.HandRight], dc);
                        }
                    }

                    // prevent drawing outside of our render area
                    this.drawingGroup.ClipGeometry = new RectangleGeometry(new Rect(0.0, 0.0, this.displayWidth, this.displayHeight));
                }


                //////////////////////////////彩色 深度事件处理/////////////////////////
                DepthFrame depthFrame = null;
                ColorFrame colorFrame = null;

                if (multiSourceFrame != null)
                {

                    ColorFrameReference colorFrameReference = multiSourceFrame.ColorFrameReference;
                    DepthFrameReference depthFrameReference = multiSourceFrame.DepthFrameReference;

                    colorFrame = colorFrameReference.AcquireFrame();
                    depthFrame = depthFrameReference.AcquireFrame();

                    if ((depthFrame != null) && (colorFrame != null))
                    {

                        colorFrame.CopyConvertedFrameDataToArray(this.colorPixelData, ColorImageFormat.Bgra);
                        this.colorBitmap.WritePixels(this.colorBitmapRect, this.colorPixelData, this.colorBitmapStride, 0);

                        depthFrame.CopyFrameDataToArray(this.depthPixelData);
                        this.depthBitmap.WritePixels(this.depthBitmapRect, this.depthPixelData, this.depthStride, 0);
                    }
                }


                if (depthFrame != null)
                {
                    depthFrame.Dispose();
                    depthFrame = null;
                }
                //个人理解为,清楚帧内存,不清楚的话只会显示第一帧就卡住了
                if (colorFrame != null)
                {
                    colorFrame.Dispose();
                    colorFrame = null;
                }

                //if (multiSourceFrame != null)
                //{
                //    multiSourceFrame = null;
                //}

            }
        }

             /// 
             /// Draws a body
             /// 
             /// joints to draw
             /// translated positions of joints to draw
             /// drawing context to draw to
             /// specifies color to draw a specific body
        private void DrawBody(IReadOnlyDictionary joints, IDictionary jointPoints, DrawingContext drawingContext, Pen drawingPen)
        {
            // Draw the bones
            foreach (var bone in this.bones)
            {
                this.DrawBone(joints, jointPoints, bone.Item1, bone.Item2, drawingContext, drawingPen);
            }

            // Draw the joints
            foreach (JointType jointType in joints.Keys)
            {
                Brush drawBrush = null;

                TrackingState trackingState = joints[jointType].TrackingState;

                if (trackingState == TrackingState.Tracked)
                {
                    drawBrush = this.trackedJointBrush;
                }
                else if (trackingState == TrackingState.Inferred)
                {
                    drawBrush = this.inferredJointBrush;
                }

                if (drawBrush != null)
                {
                    drawingContext.DrawEllipse(drawBrush, null, jointPoints[jointType], JointThickness, JointThickness);
                }
            }
        }

        /// 
        /// Draws one bone of a body (joint to joint)
        /// 
        /// joints to draw
        /// translated positions of joints to draw
        /// first joint of bone to draw
        /// second joint of bone to draw
        /// drawing context to draw to
        /// /// specifies color to draw a specific bone
        private void DrawBone(IReadOnlyDictionary joints, IDictionary jointPoints, JointType jointType0, JointType jointType1, DrawingContext drawingContext, Pen drawingPen)
        {
            Joint joint0 = joints[jointType0];
            Joint joint1 = joints[jointType1];

            // If we can't find either of these joints, exit
            if (joint0.TrackingState == TrackingState.NotTracked ||
                joint1.TrackingState == TrackingState.NotTracked)
            {
                return;
            }

            // We assume all drawn bones are inferred unless BOTH joints are tracked
            Pen drawPen = this.inferredBonePen;
            if ((joint0.TrackingState == TrackingState.Tracked) && (joint1.TrackingState == TrackingState.Tracked))
            {
                drawPen = drawingPen;
            }

            drawingContext.DrawLine(drawPen, jointPoints[jointType0], jointPoints[jointType1]);
        }

        /// 
        /// Draws a hand symbol if the hand is tracked: red circle = closed, green circle = opened; blue circle = lasso
        /// 
        /// state of the hand
        /// position of the hand
        /// drawing context to draw to
        private void DrawHand(HandState handState, Point handPosition, DrawingContext drawingContext)
        {
            switch (handState)
            {
                case HandState.Closed:
                    drawingContext.DrawEllipse(this.handClosedBrush, null, handPosition, HandSize, HandSize);
                    break;

                case HandState.Open:
                    drawingContext.DrawEllipse(this.handOpenBrush, null, handPosition, HandSize, HandSize);
                    break;

                case HandState.Lasso:
                    drawingContext.DrawEllipse(this.handLassoBrush, null, handPosition, HandSize, HandSize);
                    break;
            }
        }

        /// 
        /// Draws indicators to show which edges are clipping body data
        /// 
        /// body to draw clipping information for
        /// drawing context to draw to
        private void DrawClippedEdges(Body body, DrawingContext drawingContext)
        {
            FrameEdges clippedEdges = body.ClippedEdges;

            if (clippedEdges.HasFlag(FrameEdges.Bottom))
            {
                drawingContext.DrawRectangle(
                    Brushes.Red,
                    null,
                    new Rect(0, this.displayHeight - ClipBoundsThickness, this.displayWidth, ClipBoundsThickness));
            }

            if (clippedEdges.HasFlag(FrameEdges.Top))
            {
                drawingContext.DrawRectangle(
                    Brushes.Red,
                    null,
                    new Rect(0, 0, this.displayWidth, ClipBoundsThickness));
            }

            if (clippedEdges.HasFlag(FrameEdges.Left))
            {
                drawingContext.DrawRectangle(
                    Brushes.Red,
                    null,
                    new Rect(0, 0, ClipBoundsThickness, this.displayHeight));
            }

            if (clippedEdges.HasFlag(FrameEdges.Right))
            {
                drawingContext.DrawRectangle(
                    Brushes.Red,
                    null,
                    new Rect(this.displayWidth - ClipBoundsThickness, 0, ClipBoundsThickness, this.displayHeight));
            }
        }


    }
}

你可能感兴趣的:(Kinect v2)