Exemplo n.º 1
0
        /// <summary>
        /// スケルトンの更新通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
        {
            try {
                // Kinectのインスタンスを取得する
                KinectSensor kinect = sender as KinectSensor;
                if (kinect == null)
                {
                    return;
                }

                // スケルトンのフレームを取得する
                using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) {
                    if (skeletonFrame != null)
                    {
                        Skeleton skeleton = skeletonFrame.GetFirstTrackedSkeleton();
                        if (skeleton != null)
                        {
                            // ポーズの検出用に、スケルトンデータを追加する
                            postureDetector.TrackPostures(skeleton);

                            // 右手がトラッキングされていた場合、ジェスチャーの検出用にジョイントを追加する
                            Joint hand = skeleton.Joints[JointType.HandRight];
                            if (hand.TrackingState == JointTrackingState.Tracked)
                            {
                                swipeDetector.Add(hand.Position, kinect);
                            }
                        }
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// スケルトンを描画する
        /// </summary>
        /// <param name="kinect"></param>
        /// <param name="skeletonFrame"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        private void Power(KinectSensor kinect, SkeletonFrame skeletonFrame)
        {
            // スケルトンのデータを取得する
            Skeleton skeleton = skeletonFrame.GetFirstTrackedSkeleton();

            if (skeleton == null)
            {
                return;
            }

            // 操作に必要なジョイントを取得する
            Joint rightHand  = skeleton.Joints[JointType.HandRight];
            Joint rightElbow = skeleton.Joints[JointType.ElbowRight];
            Joint leftHand   = skeleton.Joints[JointType.HandLeft];
            Joint leftElbow  = skeleton.Joints[JointType.ElbowLeft];

            // ジョイントの描画
            DrawSkeleton(kinect, new Joint[] { rightHand, rightElbow, leftHand, leftElbow });

            // ジョイントすべてがトラッキング状態のときのみ操作する
            if ((rightHand.TrackingState != JointTrackingState.Tracked) ||
                (rightElbow.TrackingState != JointTrackingState.Tracked) ||
                (leftHand.TrackingState != JointTrackingState.Tracked) ||
                (leftElbow.TrackingState != JointTrackingState.Tracked))
            {
                return;
            }

            // 腕の角度を、モーターのパワーに変換して、NXTに送信する
            sbyte rightPower = 0, leftPower = 0;

            if (rightHand.Position.Y > rightElbow.Position.Y)
            {
                rightPower = GetPower(rightHand, rightElbow);
            }

            if (leftHand.Position.Y > leftElbow.Position.Y)
            {
                leftPower = GetPower(leftHand, leftElbow);
            }

            SetMotorPower(rightPower, leftPower);
        }
Exemplo n.º 3
0
 /// <summary>
 /// スケルトンを追跡しているか調べる
 /// </summary>
 /// <param name="frame"></param>
 /// <returns></returns>
 public static bool IsTrackedSkeleton(this SkeletonFrame frame)
 {
     return(frame.GetFirstTrackedSkeleton() != null);
 }
Exemplo n.º 4
0
        /// <summary>
        /// RGBカメラ、スケルトンのフレーム更新イベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
        {
            try {
                KinectSensor kinect = sender as KinectSensor;

                using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) {
                    if (colorFrame != null)
                    {
                        byte[] colorPixel = new byte[colorFrame.PixelDataLength];
                        colorFrame.CopyPixelDataTo(colorPixel);
                        imageRgb.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96,
                                                              PixelFormats.Bgr32, null, colorPixel, colorFrame.Width * colorFrame.BytesPerPixel);
                    }
                }

                using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame()) {
                    if (skeletonFrame != null)
                    {
                        // トラッキングされているスケルトンのジョイントを描画する
                        Skeleton skeleton = skeletonFrame.GetFirstTrackedSkeleton();

                        if ((skeleton != null) && (skeleton.TrackingState == SkeletonTrackingState.Tracked))
                        {
                            Joint hand = skeleton.Joints[JointType.HandRight];
                            if (hand.TrackingState == JointTrackingState.Tracked)
                            {
                                ImageSource   source        = imageRgb.Source;
                                DrawingVisual drawingVisual = new DrawingVisual();

                                using (DrawingContext drawingContext = drawingVisual.RenderOpen()) {
                                    //バイト列をビットマップに展開
                                    //描画可能なビットマップを作る
                                    drawingContext.DrawImage(imageRgb.Source,
                                                             new Rect(0, 0, source.Width, source.Height));

                                    // 手の位置に円を描画
                                    DrawSkeletonPoint(drawingContext, hand);
                                }

                                // 描画可能なビットマップを作る
                                // http://stackoverflow.com/questions/831860/generate-bitmapsource-from-uielement
                                RenderTargetBitmap bitmap = new RenderTargetBitmap((int)source.Width,
                                                                                   (int)source.Height, 96, 96, PixelFormats.Default);
                                bitmap.Render(drawingVisual);

                                imageRgb.Source = bitmap;

                                // Frame中の手の位置をディスプレイの位置に対応付ける
                                ColorImagePoint point = kinect.MapSkeletonPointToColor(hand.Position,           // スケルトン座標 → RGB画像座標
                                                                                       kinect.ColorStream.Format);
                                System.Windows.Forms.Screen screen = System.Windows.Forms.Screen.AllScreens[0]; // メインディスプレイの情報を取得
                                point.X = (point.X * screen.Bounds.Width) / kinect.ColorStream.FrameWidth;
                                point.Y = (point.Y * screen.Bounds.Height) / kinect.ColorStream.FrameHeight;

                                // マウスカーソルの移動
                                SendInput.MouseMove(point.X, point.Y, screen);

                                // クリック動作
                                if (IsClicked(skeletonFrame, point))
                                {
                                    SendInput.LeftClick();
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }