Kinect测量身高

来源:互联网 发布:can数据传递系统是根据 编辑:程序博客网 时间:2024/05/17 04:33
直接上代码:
using System;using System.Collections.Generic;using System.Linq;using System.Text;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.Runtime.InteropServices;namespace WpfApplication3{    /// <summary>    /// MainWindow.xaml 的交互逻辑    /// </summary>    public partial class MainWindow : Window    {        [DllImport("User32.dll")]        public static extern void LockWorkStation();        private KinectSensor _kinect;        private short[] PixelData;        readonly List<double> PreviousDepthAvgValueList = new List<double>();        private const double DepthAvgDiffThreadhold = 300;        private const int PrevDepthAvgListLength = 100;        public MainWindow()        {            InitializeComponent();        }        private void startKinect()        {            if (KinectSensor.KinectSensors.Count > 0)            {                _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.AllFramesReady +=                //    new EventHandler<AllFramesReadyEventArgs>(_kinect_AllFramesReady);                _kinect.SkeletonFrameReady +=                    new EventHandler<SkeletonFrameReadyEventArgs>(_kinect_SkeletonFrameReady);                //_kinect.DepthStream.Range = DepthRange.Near;                _kinect.Start();            }//if            else            {                MessageBox.Show("没有任何Kinect设备");            }        }        private void stopKinect()        {            if (_kinect != null)            {                if (_kinect.Status == KinectStatus.Connected)                {                    _kinect.Stop();                }            }//null        }//stopp        void _kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)        {            using (SkeletonFrame Sframe = e.OpenSkeletonFrame())            {                if (Sframe == null)                    return;                Skeleton s = GetClosetSkeleton(Sframe);                if (s == null)                    return;                if (s.ClippedEdges == FrameEdges.None)                    return;                SkeletonPoint head = s.Joints[JointType.Head].Position;                SkeletonPoint centerShoulder = s.Joints[JointType.ShoulderCenter].Position;                SkeletonPoint leftFoot = s.Joints[JointType.FootLeft].Position;                float height = Math.Abs(head.Y - leftFoot.Y);                float heightEx = Math.Abs(head.Y - centerShoulder.Y) + 0.1f;                float realHeight = height + heightEx;                mylabel.Content = realHeight.ToString();            }//susing         }        const int MaxSkeletonTrackingCount = 6;        private Skeleton[] allSkeletons = new Skeleton[MaxSkeletonTrackingCount];        Skeleton GetClosetSkeleton(SkeletonFrame frame)        {            frame.CopySkeletonDataTo(allSkeletons);            Skeleton closetSkeleton = (from s in allSkeletons                                       where s.TrackingState == SkeletonTrackingState.Tracked                                       && s.Joints[JointType.Head].TrackingState == JointTrackingState.Tracked                                       select s).OrderBy(s => s.Joints[JointType.Head].Position.Z).FirstOrDefault();            return closetSkeleton;        }        void _kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)        {            bool receivedData = false;            using (DepthImageFrame DFrame = e.OpenDepthImageFrame())            {                if (DFrame != null)                {                    PixelData = new short[DFrame.PixelDataLength];                    DFrame.CopyPixelDataTo(PixelData);                    var depthAvg = PixelData.Average(pixel => pixel);                    PreviousDepthAvgValueList.Add(depthAvg);                    var avgOfdepthAvgList = PreviousDepthAvgValueList.Average(value => value);                    if (PreviousDepthAvgValueList.Count > PrevDepthAvgListLength)                        PreviousDepthAvgValueList.RemoveAt(0);                    labelDepthAvg.Content = depthAvg.ToString();                    lablelAvgOfhistory.Content = avgOfdepthAvgList.ToString();                    if (PreviousDepthAvgValueList.Count == PrevDepthAvgListLength                        &&                        (Math.Abs(avgOfdepthAvgList - depthAvg) > DepthAvgDiffThreadhold))                    {                        //mylabel.Content = "true";                        PreviousDepthAvgValueList.Clear();                    }//if                    else                    {                        //mylabel.Content = "false";                    }                    receivedData = true;                }//null            }//using             if (receivedData)            {                BitmapSource source = BitmapSource.Create(                    640, 480, 96, 96,                    PixelFormats.Gray16, null, PixelData, 640 * 2);                image1.Source = source;            }//        }        private void Window_Loaded(object sender, RoutedEventArgs e)        {            startKinect();        }        private void Window_Closed(object sender, EventArgs e)        {            stopKinect();        }    }//class }//namespace ;

原创粉丝点击