Kinect-播放ppt

来源:互联网 发布:vb语言的书 编辑:程序博客网 时间:2024/04/30 23:10

Kinect-播放ppt

        继骨骼帧之后,有了这些人体骨骼数据之后,利用这些数据,可以做好多有趣的事情,比如控制鼠标,键盘等。在Kinect实战开发里面,找到了一个比较简单的程序,Kinect骨骼帧播放ppt。很多时候我们演讲时候或者讲课的时候用的都是鼠标或者是激光笔来播放ppt,我们感觉已经够了,假如说当你的两只手都被占用时,没办法用激光笔来播放,这时候用手势播放ppt功能就显得方便了。当然Kinect比激光笔贵很多。。。
        废话不多说,其实原理很简单,但我们使用Kinect的骨骼帧来获取人体数据时,利用头部和两只手的位置关系,利用摄像头中这三点(x,y)坐标的变化并将键盘事件中的向左{->}和向右键{<-}联系起来就可以实现用手势来播放ppt的功能。
         1.总共分为以下几个步骤:
        (1)骨骼帧的初始化
        (2)骨骼帧到达事件注册
        (3)对捕捉到的骨骼数据进行处理
         2.如下具体代码详细解释:
        头文件和定义各种变量:
        头文件
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;//这个名称空间就不用说了,Kinect的using System.Windows.Forms;//这个要单独写,和键盘关联的事件需要用到这个名称空间
         定义的各种变量
        private KinectSensor mykinect;//kinect变量初始化        private BodyFrameReader bodyframereader;//骨骼帧读取变量初始化        private Body[] bodies;//存储人体数据的数组,Kinect最大支持为6个人;        private bool lefthandbool, righthandbool;//控制向左或者向右播放ppt的变量
         (1)骨骼帧的初始化
        this.mykinect = KinectSensor.GetDefault();//其实初始化非常简单,三句话就行。获得默认的kinect设备        this.bodyframereader = this.mykinect.BodyFrameSource.OpenReader();//获得获得Kinect的骨骼帧资源        this.bodies = new Body[6];//储存人体数据的数组初始化.
         (2)骨骼帧到达注册事件
        private void _bodyframereader_framearrived(object sender, BodyFrameArrivedEventArgs e)//这个是Kinect传过来的骨骼帧数据        {            using (BodyFrame bodyframe = e.FrameReference.AcquireFrame()) {//捕捉到一帧                if (bodyframe != null) {                    bone_point.Children.Clear();                    bodyframe.GetAndRefreshBodyData(this.bodies);将骨骼数据传到定义的人体骨骼数组里面,方便我们处理数据                                    }                drawBodies();//为了方便观看效果,画出头部,左手,右手这三个骨骼点。处理数据也在里面            }        }
         (3)对捕捉到的骨骼数据进行处理
        private void drawBodies()        {            for (int i = 0; i < this.bodies.Length; i++) {                if (this.bodies[i].IsTracked == true) {                    //定义三个圆对象用以显示捕捉到的三个骨骼点                    Ellipse eclipsehead = new Ellipse();                    eclipsehead.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));                    eclipsehead.Width = 20;                    eclipsehead.Height = 20;                    Ellipse eclipserighthand = new Ellipse();                    eclipserighthand.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));                    eclipserighthand.Width = 20;                    eclipserighthand.Height = 20;                    Ellipse eclipselefthand = new Ellipse();                    eclipselefthand.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));                    eclipselefthand.Width = 20;                    eclipselefthand.Height = 20;                    Joint headjoint = bodies[i].Joints[JointType.Head];//将捕捉到的骨骼点提取出来                    Joint handleft = bodies[i].Joints[JointType.HandLeft];                    Joint handright = bodies[i].Joints[JointType.HandRight];                    if (handright.Position.X > headjoint.Position.X + 0.45) {//注意这里还是摄像头上里面的点,空间中的,并不是屏幕上的点                                                    righthandbool = true;//当右手位置的横坐标大于头部位置的横坐标加上45厘米时,判定ppt是要往后面翻页                            System.Windows.Forms.SendKeys.SendWait("{Right}");//向系统发送指令,等待回应。说白了就是系统自动按一次向右键                                            }                    if (handleft.Position.X < headjoint.Position.X - 0.45) {//当左手位置的横坐标小于头部位置的横坐标减去45厘米时,判定ppt要往前面翻页                            lefthandbool = true;//标志位置true                            System.Windows.Forms.SendKeys.SendWait("{Left}");//按一次向左键                    }                    setEclipseposition(eclipsehead,headjoint,true);//这个函数作用是将表示将关节点坐标转换成屏幕坐标,并将圆画在相应的位置.                    setEclipseposition(eclipserighthand,handright,righthandbool);                    setEclipseposition(eclipselefthand, handleft, lefthandbool);                }            }        }
        private void setEclipseposition(Ellipse eclipse,Joint joint, bool isHighlight) {            ColorSpacePoint colorspacepoint = this.mykinect.CoordinateMapper.MapCameraPointToColorSpace(joint.Position);//将摄像头的左边转换成彩色空间的            //坐标            colorspacepoint.X = (int)((colorspacepoint.X * bone_point.Width) / 1920);            colorspacepoint.Y = (int)((colorspacepoint.Y * bone_point.Height) / 1080);//转化成屏幕坐标            if (isHighlight)            {                eclipse.Width = 60;                eclipse.Height = 60;                eclipse.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));            }            else {                eclipse.Width = 20;                eclipse.Height = 20;                eclipse.Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));            }            bone_point.Children.Add(eclipse);//加入控件            Canvas.SetLeft(eclipse,colorspacepoint.X-eclipse.ActualWidth/2);//以关节点为中心            Canvas.SetTop(eclipse,colorspacepoint.Y-eclipse.ActualHeight/2);        }
至此完成播放ppt的功能。

原创粉丝点击