Kinect 入门心得

感谢胡哥的支持,我和宇神才能够有一点点的进步。
更要感谢网络上面各路大神无私奉献的精神
尽管之前也或多或少的看过C#,也玩过Kinect,但是只停留在了玩的程度。
今天晚上在胡哥的鼓励下,最终还是比较正式比较努力的开始进行了Kinect的一步步学习。

由于我们没有比较全面的学习资料,所以我和宇神就在网上各种搜索资料学习,我也成功的翻到墙的另一头去看了看精彩的世界,最后还是没有能够找到比较合适的资源。宇神用他的破电脑找到了一个blog,先贴在这里,供大家学习参考,感谢博主的无私分享
http://www.cnblogs.com/yangecnu/

恩,是这样的,因为博文是翻译的文章,我们是根据这个来进行的学习,第一篇是基础的环境配置之类的东西,就不再赘述了。
需要注意的是各位如果想要进行开发学习的话要注意设备和SDK包的版本匹配,1.0可以和Kinect搭配使用,2.0的应该是需要和最新的Kinect2.0搭配使用的,因为笔者囊中羞涩,就没有用最新的进行实验了。

今晚花费比较多的时间就在于我们在进行第二套实验的时候,因为我们有源代码的原因,所以就直接看着上手,先不论理解多少,先尝试着run一下

源代码先送上:
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;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private KinectSensor kinect;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += (s, e) => DiscoverKinectSensor();
            this.Unloaded += (s, e) => this.kinect = null;
        }

        public KinectSensor Kinect
        {
            get { return this.kinect; }
            set
            {
                //如果带赋值的传感器和目前的不一样
                if (this.kinect != value)
                {
                    //如果当前的传感对象不为null
                    if (this.kinect != null)
                    {
                        UninitializeKinectSensor(this.kinect);
                        //uninitailize当前对象
                        this.kinect = null;
                    }
                    //如果传入的对象不为空,且状态为连接状态
                    if (value != null && value.Status == KinectStatus.Connected)
                    {
                        this.kinect = value;
                        InitializeKinectSensor(this.kinect);
                    }
                }
            }
        }

        private void DiscoverKinectSensor()
        {
            KinectSensor.KinectSensors.StatusChanged += KinectSensors_StatusChanged;
            this.Kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
        }

        private void KinectSensors_StatusChanged(object sender, StatusChangedEventArgs e)
        {
            switch (e.Status)
            {
                case KinectStatus.Connected:
                    if (this.kinect == null)
                        this.kinect = e.Sensor;
                    break;
                case KinectStatus.Disconnected:
                    if (this.kinect == e.Sensor)
                    {
                        this.kinect = null;
                        this.kinect = KinectSensor.KinectSensors.FirstOrDefault(x => x.Status == KinectStatus.Connected);
                        if (this.kinect == null)
                        {
                            //TODO:通知用于Kinect已拔出
                        }
                    }
                    break;
                //TODO:处理其他情况下的状态
            }
        }

        private void InitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                kinectSensor.ColorStream.Enable();
                kinectSensor.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
                kinectSensor.Start();
            }
        }

        private void UninitializeKinectSensor(KinectSensor kinectSensor)
        {
            if (kinectSensor != null)
            {
                kinectSensor.Stop();
                kinectSensor.ColorFrameReady -= new EventHandler<ColorImageFrameReadyEventArgs>(kinectSensor_ColorFrameReady);
            }
        }

        void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
        {
            using (ColorImageFrame frame = e.OpenColorImageFrame())
            {
                if (frame != null)
                {
                    byte[] pixelData = new byte[frame.PixelDataLength];
                    frame.CopyPixelDataTo(pixelData);
                    ColorImageElement.Source = BitmapImage.Create(frame.Width, frame.Height, 96, 96,
                                                                 PixelFormats.Bgr32, null, pixelData,
                                                                 frame.Width * frame.BytesPerPixel);

                }
            }
        }
    }
}


<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="350" Width="525">
    <Grid>
        <Image x:Name="ColorImageElement"></Image>
    </Grid>
</Window>


在调试的时候,有这么几个问题可能是大家会发生的。
大家在创建工程的时候记得要选择WPF应用程序
其次,要注意命名空间,文件名不要照抄代码
当然,大家要记得添加引用集,这是初学者很多时候可能会犯的一个错误,也有很多人呢不会做这一步,所以我就图示教学如下:
先点击项目->添加引用
这个时候会弹出一个对话框,在左边选择程序集,之后再右上角的搜索框中搜索“Kinect”关键词,就会出现Microsoft Kinect 在右边有版本号,由于我使用的硬件设备是kinect,所以就选择了1.0的版本,大家视情况而定。之后点击确定,就可以引用kinect了。

猜你喜欢

转载自4ljy.iteye.com/blog/2195653
今日推荐