使用微软Kinect进行手势识别操作的一个简单范例(2)(WPF-C#)

在我上一篇博客中:http://blog.csdn.net/soft2buy/article/details/6589326

介绍了一个用微软的Kinect Sensor 捕捉双手动作的简单例子。今天我给大家介绍用Kinect来进行操作者手势的识别。

看完本篇文章,你可以使用C#(WPF)程序来判断操作者左右手的几个手势:

1.从左 到右挥动

2.从右到左挥动

3.从后到前(按下动作)

4.从前到后(按下后离开)

要正确运行本文的程序,你需要下载和安装:

1.Kinect for Windows SDK
http://research.microsoft.com/en-us/um/redmond/projects/kinectsdk/download.aspx

2.Microsoft XNA Game Studio 4.0
http://www.microsoft.com/download/en/details.aspx?id=23714

因为我们要利用XNA来计算双手的X,Y,Z坐标。

3.微软最新版的Kinect Toolkit :
http://kinecttoolkit.codeplex.com/

微软最新版的Kinect Toolkit 还有很多其他的动作,比如 SwipeToLeft(左下摆),SwipeToRight(右下摆),LeftHello(左手挥手),RightHello(右手挥手)动作,大家有兴趣可以自己好好研究,本篇文章不做解释。

唯一不足的是,没有LeftToRight(左到右挥手)和RightToLeft(右到左挥手),FrontToBack(按下动作),BackToFront(离开动作),我们知道这4个动作是非常有用的。前两个可以用在界面图片或文章的上下翻页,而后两个可以用来选择所需要的图片或者文章。

所以我在微软的Kinect Toolkit里添加了这两个动作的支持,代码如下:

大家可以在Gestures/SwipeGestureDetector.cs

内添加:

void LookForGesture()
        {
            // 左到右
            if (ScanPositions((p1, p2) => Math.Abs(p2.Y - p1.Y) < 0.20f, (p1, p2) => p2.X - p1.X > - 0.01f , (p1, p2) => Math.Abs(p2.X - p1.X) > 0.2f, 250, 2500))
            {
                RaiseGestureDetected(“LeftToRight”);
                return;
            }

            // 右到左
            if (ScanPositions((p1, p2) => Math.Abs(p2.Y - p1.Y) < 0.20f, (p1, p2) => p2.X - p1.X < 0.01f, (p1, p2) => Math.Abs(p2.X - p1.X) > 0.2f, 250, 2500))
            {
                RaiseGestureDetected(“RightToLeft”);
                return;
            }

            // 后到前
            if (ScanPositions((p1, p2) => Math.Abs(p2.Y - p1.Y) < 0.15f, (p1, p2) => p2.Z - p1.Z < 0.01f, (p1, p2) => Math.Abs(p2.Z - p1.Z) > 0.2f, 250, 2500))
            {
                RaiseGestureDetected(“BackToFront”);
                return;
            }

            // 前到后
            if (ScanPositions((p1, p2) => Math.Abs(p2.Y - p1.Y) < 0.15f, (p1, p2) => p2.Z - p1.Z > -0.04f, (p1, p2) => Math.Abs(p2.Z - p1.Z) > 0.4f, 250, 2500))
            {
                RaiseGestureDetected(“FrontToBack”);
                return;
            }
        }

我们稍微修改下上一篇文章的代码:

void nui_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            SkeletonFrame skeletonFrame = e.SkeletonFrame;
            foreach (SkeletonData data in skeletonFrame.Skeletons)
            {
                if (SkeletonTrackingState.Tracked == data.TrackingState)
                {
                    foreach (Joint joint in data.Joints)
                    {
                        Point jointPos = getDisplayPosition(joint);
                       
                        //判断左手或右手的手势
                        switch (joint.ID)
                        {
                            case JointID.HandLeft://左手
                                if (joint.Position.W > 0.6f) //准确概率>0.6,1.0f 为100% 准确
                                    leftHandGestureRecognizer.Add(joint.Position.ToVector3());
                                break;
                            case JointID.HandRight://右手
                                if (joint.Position.W > 0.6f) //准确概率>0.6,1.0f 为100% 准确
                                    rightHandGestureRecognizer.Add(joint.Position.ToVector3());
                                break;                      
                        }

                    }
                }
            }
        }

下面就可以接收手势判断的事件了:

#region 手势判断
        void rightHandGestureRecognizer_OnGestureDetected(SupportedGesture gesture)//右手手势
        {

            switch (gesture)
            {
                case SupportedGesture.LeftToRight:
                    _receiveGestures("右手:从左到右");
                    break;
                case SupportedGesture.RightToLeft:
                    _receiveGestures("右手:从右到左");
                    break;
                case SupportedGesture.BackToFront:
                    _receiveGestures("右手:从后到前");
                    break;
                case SupportedGesture.FrontToBack:
                    _receiveGestures("右手:从前到后");
                    break;
            }
        }

        void leftHandGestureRecognizer_OnGestureDetected(SupportedGesture gesture)//左手手势
        {

            switch (gesture)
            {
                case SupportedGesture.LeftToRight:
                    _receiveGestures("左手:从左到右");
                    break;
                case SupportedGesture.RightToLeft:
                    _receiveGestures("左手:从右到左");
                    break;
                case SupportedGesture.BackToFront:
                    _receiveGestures("左手:从后到前");
                    break;
                case SupportedGesture.FrontToBack:
                    _receiveGestures("左手:从前到后");
                    break;
            }
        }
        #endregion

本文范例下载

KinectLib for Xbox 360 (含 驱动)

作者做的范例视频:

http://v.youku.com/v_show/id_XMjkxODc3NzEy.html

 


 

你可能感兴趣的:(windows,object,C#,WPF,微软)