zoukankan      html  css  js  c++  java
  • Kinect 开发 —— 显示骨骼用户插件

    public partial class SkeletonViewer : UserControl
        {
            private readonly Brush[] _SkeletonBrushes = new Brush[] { Brushes.Black, Brushes.Crimson, Brushes.Indigo, Brushes.DodgerBlue, Brushes.Purple, Brushes.Pink };
            private Skeleton[] _FrameSkeletons;
            protected const string KinectDevicePropertyName = "KinectDevice";
            public static readonly DependencyProperty KinectDeviceProperty = DependencyProperty.Register(KinectDevicePropertyName, typeof(KinectSensor), typeof(SkeletonViewer), new PropertyMetadata(null, KinectDeviceChanged));
    
            public KinectSensor KinectDevice
            {
                get { return (KinectSensor)GetValue(KinectDeviceProperty); }
                set { SetValue(KinectDeviceProperty, value); }
            }
    
            public SkeletonViewer()
            {
                InitializeComponent();
            }
    
            private static void KinectDeviceChanged(DependencyObject owner, DependencyPropertyChangedEventArgs e)
            {
                SkeletonViewer viewer = (SkeletonViewer)owner;
    
                if (e.OldValue != null)
                {
                    ((KinectSensor)e.OldValue).SkeletonFrameReady -= viewer.KinectDevice_SkeletonFrameReady;
                    viewer._FrameSkeletons = null;
                }
    
                if (e.NewValue != null)
                {
                    viewer.KinectDevice = (KinectSensor)e.NewValue;
                    viewer.KinectDevice.SkeletonFrameReady += viewer.KinectDevice_SkeletonFrameReady;
                    viewer._FrameSkeletons = new Skeleton[viewer.KinectDevice.SkeletonStream.FrameSkeletonArrayLength];
                }
            }
    
            private void KinectDevice_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
            {
                SkeletonsPanel.Children.Clear();
                JointInfoPanel.Children.Clear();
    
                using (SkeletonFrame frame = e.OpenSkeletonFrame())
                {
                    if (frame != null)
                    {
                        if (this.IsEnabled)
                        {
                            frame.CopySkeletonDataTo(this._FrameSkeletons);
    
                            for (int i = 0; i < this._FrameSkeletons.Length; i++)
                            {
                                DrawSkeleton(this._FrameSkeletons[i], this._SkeletonBrushes[i]);
    
                                TrackJoint(this._FrameSkeletons[i].Joints[JointType.HandLeft], this._SkeletonBrushes[i]);
                                TrackJoint(this._FrameSkeletons[i].Joints[JointType.HandRight], this._SkeletonBrushes[i]);
                            }
                        }
                    }
                }
            }
    
            private void TrackJoint(Joint joint, Brush brush)
            {
                if (joint.TrackingState != JointTrackingState.NotTracked)
                {
                    Canvas container = new Canvas();
                    Point jointPoint = GetJointPoint(joint);
    
                    double z = joint.Position.Z;
    
                    Ellipse element = new Ellipse();
                    element.Height = 15;
                    element.Width = 15;
                    element.Fill = brush;
                    Canvas.SetLeft(element, 0 - (element.Width / 2));
                    Canvas.SetTop(element, 0 - (element.Height / 2));
                    container.Children.Add(element);
    
                    TextBlock positionText = new TextBlock();
                    positionText.Text = string.Format("<{0:0.00}, {1:0.00}, {2:0.00}>", jointPoint.X, jointPoint.Y, z);
                    positionText.Foreground = brush;
                    positionText.FontSize = 24;
                    positionText.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
                    Canvas.SetLeft(positionText, 35);
                    Canvas.SetTop(positionText, 15);
                    container.Children.Add(positionText);
    
                    Canvas.SetLeft(container, jointPoint.X);
                    Canvas.SetTop(container, jointPoint.Y);
    
                    JointInfoPanel.Children.Add(container);
                }
            }
    
            private void DrawSkeleton(Skeleton skeleton, Brush brush)
            {
                if (skeleton != null && skeleton.TrackingState == SkeletonTrackingState.Tracked)
                {
                    //Draw head and torso
                    Polyline figure = CreateFigure(skeleton, brush, new[] { JointType.Head, JointType.ShoulderCenter, JointType.ShoulderLeft, JointType.Spine,
                                                                                 JointType.ShoulderRight, JointType.ShoulderCenter, JointType.HipCenter});
                    SkeletonsPanel.Children.Add(figure);
    
                    figure = CreateFigure(skeleton, brush, new[] { JointType.HipLeft, JointType.HipRight });
                    SkeletonsPanel.Children.Add(figure);
    
                    //Draw left leg
                    figure = CreateFigure(skeleton, brush, new[] { JointType.HipCenter, JointType.HipLeft, JointType.KneeLeft, JointType.AnkleLeft, JointType.FootLeft });
                    SkeletonsPanel.Children.Add(figure);
    
                    //Draw right leg
                    figure = CreateFigure(skeleton, brush, new[] { JointType.HipCenter, JointType.HipRight, JointType.KneeRight, JointType.AnkleRight, JointType.FootRight });
                    SkeletonsPanel.Children.Add(figure);
    
                    //Draw left arm
                    figure = CreateFigure(skeleton, brush, new[] { JointType.ShoulderLeft, JointType.ElbowLeft, JointType.WristLeft, JointType.HandLeft });
                    SkeletonsPanel.Children.Add(figure);
    
                    //Draw right arm
                    figure = CreateFigure(skeleton, brush, new[] { JointType.ShoulderRight, JointType.ElbowRight, JointType.WristRight, JointType.HandRight });
                    SkeletonsPanel.Children.Add(figure);
                }
            }
    
            private Polyline CreateFigure(Skeleton skeleton, Brush brush, JointType[] joints)
            {
                Polyline figure = new Polyline();
    
                figure.StrokeThickness = 18;
                figure.Stroke = brush;
    
                for (int i = 0; i < joints.Length; i++)
                {
                    figure.Points.Add(GetJointPoint(skeleton.Joints[joints[i]]));
                }
    
                return figure;
            }
    
            private Point GetJointPoint(Joint joint)
            {
                DepthImagePoint point = this.KinectDevice.MapSkeletonPointToDepth(joint.Position, this.KinectDevice.DepthStream.Format);
                point.X *= (int)this.LayoutRoot.ActualWidth / KinectDevice.DepthStream.FrameWidth;
                point.Y *= (int)this.LayoutRoot.ActualHeight / KinectDevice.DepthStream.FrameHeight;
    
                return new Point(point.X, point.Y);
            }
    
    
        }


    <UserControl x:Class="KinectSimonSay.SkeletonViewer"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 mc:Ignorable="d" 
                 d:DesignHeight="300" d:DesignWidth="300">
        <Grid x:Name="LayoutRoot">
            <Grid x:Name="SkeletonsPanel"/>
            <Canvas x:Name="JointInfoPanel"/>
        </Grid>
    </UserControl>

    SkeletonViewerElement.KinectDevice = this.KinectDevice;   

    // 依赖属性

  • 相关阅读:
    hdu 4396(spfs/二维最短路)
    hdu 4360(最短路变形)
    hdu 3501(欧拉函数的应用)
    bnu 4359(数位dp)
    用Hermite插值方法估计积分值
    虞旦盛老师 的《数学分析续》课件
    数值积分中的梯形法则
    数值积分中的梯形法则
    辛普森三八律
    利用自然数的标准分解证明可数集合的所有有限子集形成的集合是可数集
  • 原文地址:https://www.cnblogs.com/sprint1989/p/3844458.html
Copyright © 2011-2022 走看看