彩色圖像流、深度數據和骨骼點跟蹤的集合處理

.cs文件代碼:數組

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;
namespace Kinect
{
/// ide


/// MainWindow.xaml 的交互邏輯
///

public partial class MainWindow : Window
{
KinectSensor _kinect;

const float MaxDepthDistance = 4095;//深度圖像最大視距
    const float MinDepthDiatance = 850;//深度圖像最小視距
    const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDiatance;
    //BGR32圖像,紅色、綠色、藍色分別對應的偏移(32位,4字節中的第幾個)
    private const int RedIndex = 2;
    private const int GreenIndex = 1;
    private const int BlueIndex = 0;

    //定義骨骼幀數組
    private Skeleton[] skeletons;

    /// <summary>
    ///單色直方圖計算公式,返回256色灰階。顏色越黑則越遠,越白則越近
    ///將MaxDepthDistanceOffset數值控制在0~255的範圍內
    /// </summary>
    /// <param name="distance">深度值,有效範圍MinDepthDistance到MaxDepthDistance</param>
    /// <returns>256色灰階</returns>
    private static byte CalculateIntensityFromDepth(int distance)
    {
        return (byte)(255 - (255 * Math.Max(distance - MinDepthDiatance, 0) / (MaxDepthDistance)));
    }




    /// <summary>
    /// 生成BGR32格式的圖片字節數組
    /// </summary>
    /// <param name="depthFrame"></param>
    /// <returns></returns>
    private byte[] convertDepthFrameToColorFrame(DepthImageFrame depthFrame)
    {
        //從深度圖像幀中獲取原始數據
        short[] rawDepthData = new short[depthFrame.PixelDataLength];
        depthFrame.CopyPixelDataTo(rawDepthData);
        //建立Height X Width X 4 的RGB數組(Red, Green, Blue, empty byte)
        Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4];
        //Bgr32  - Blue, Green, Red, empty byte
        //Bgra32 - Blue, Green, Red, transparency
        //須要爲Bgra32設置透明度(transparency),.NET默認設置該字節爲0(全透明)
        for(int depthIndex = 0,colorIndex = 0;depthIndex < rawDepthData.Length && colorIndex<pixels.Length;depthIndex++,colorIndex  += 4)
        {
            //用戶分割, 0表明該像素不屬於用戶身體,低於3位字節表示被追蹤的使用者的索引編號
            int player = rawDepthData[depthIndex & DepthImageFrame.PlayerIndexBitmask];

            //得到深度數值,高13位字節
            int depth = rawDepthData[depthIndex]>>DepthImageFrame.PlayerIndexBitmaskWidth;
            //0.9米內
            if(depth <= 900)
            {
                //離Kinect很近
                pixels[colorIndex + BlueIndex] = 255;
                pixels[colorIndex + GreenIndex] = 0;
                pixels[colorIndex + RedIndex] = 0;
            }
            //0.9~2米之間
            else if(depth > 900 && depth < 2000)
            {
                pixels[colorIndex + BlueIndex] = 0;
                pixels[colorIndex + GreenIndex] = 255;
                pixels[colorIndex + RedIndex] = 0;
            }
            // 2米+
            else if(depth > 2000)
            {
                //離Kinect超過2米
                pixels[colorIndex + BlueIndex] = 0;
                pixels[colorIndex + GreenIndex] = 0;
                pixels[colorIndex + RedIndex] = 255;
            }
            //單色直方圖着色
            byte intensity = CalculateIntensityFromDepth(depth);
            pixels[colorIndex + BlueIndex] = intensity;
            pixels[colorIndex + GreenIndex] = intensity;
            pixels[colorIndex + RedIndex] = intensity;
            //若是是人體區域,用「亮綠色標註」
            if(player > 0)
            {
                pixels[colorIndex + BlueIndex] = Colors.LightGreen.B;
                pixels[colorIndex + GreenIndex] = Colors.LightGreen.G;
                pixels[colorIndex + RedIndex] = Colors.LightGreen.R;
            }
        }
        return pixels;
    }


    /// <summary>
    /// 啓動Kinect設備,初始化選項,並註冊彩色圖像,深度圖像和骨骼跟蹤事件
    /// </summary>
    private void startKinect()
    {
        if(KinectSensor.KinectSensors.Count > 0)
        {
            //選擇第一個Kinect設備
            _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.ColorFrameReady += new EventHandler<ColorImageFrameReadyEventArgs>(_kinect_ColorFrameReady);
            _kinect.SkeletonFrameReady += new EventHandler<SkeletonFrameReadyEventArgs>(_kinect_SkeletonFrameReady);

            //啓動Kinect設備
            _kinect.Start();
        }
        else
        {
            MessageBox.Show("沒有發現任何Kinect設備");
        }
    }


    private void stopKinect()
    {
        if (_kinect != null)
        {
            if (_kinect.Status == KinectStatus.Connected)
            {
                _kinect.Stop();
            }
        }
    }
    /// <summary>
    /// 定義彩色圖像事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void _kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
    {
        //顯示彩色攝像頭
        using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
        {
            if (colorFrame == null)
            {
                return;
            }

            byte[] pixels = new byte[colorFrame.PixelDataLength];
            colorFrame.CopyPixelDataTo(pixels);

            //BGR32格式圖片一個像素爲4個字節
            int stride = colorFrame.Width * 4;

            imageCanera.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height,
                96, 96, PixelFormats.Bgr32, null, pixels, stride);
        }
    }

    /// <summary>
    /// 定義深度圖像事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void _kinect_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
    {
        using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
        {
            if (depthFrame == null)
            {
                return;
            }
            byte[] pixels = convertDepthFrameToColorFrame(depthFrame);
            //BGR圖片的步長
            int stride = depthFrame.Width * 4;
            //建立BRG32格式的圖片
            imageDepth.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
        }
    }

    /// <summary>
    /// 定義骨骼跟蹤事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void _kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
    {
        bool isSkeletonDataReady = false;
        using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
        {
            if(skeletonFrame != null)
            {
                skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
                skeletonFrame.CopySkeletonDataTo(skeletons);
                isSkeletonDataReady = true;
            }

            if (isSkeletonDataReady)
            {
                //從返回的骨骼對象集合中,定位第一個被跟蹤的骨骼
                Skeleton currentSkeleton = (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault();

                if (currentSkeleton != null)
                {
                    //調用20個函數鎖定20個骨骼點。
                    lockHeadWithRedSpot(currentSkeleton);
                    lockShoulderCenterWithRedSpot(currentSkeleton);
                    lockShoulderLeftWithRedSpot(currentSkeleton);
                    lockShoulderRightWithRedSpot(currentSkeleton);
                    lockElbowLeftWithRedSpot(currentSkeleton);
                    lockElbowRightWithRedSpot(currentSkeleton);
                    lockWristLeftWithRedSpot(currentSkeleton);
                    lockWristRightWithRedSpot(currentSkeleton);
                    lockHandLeftWithRedSpot(currentSkeleton);
                    lockHandRightWithRedSpot(currentSkeleton);
                    lockSpineWithRedSpot(currentSkeleton);
                    lockHipCenterWithRedSpot(currentSkeleton);
                    lockHipLeftWithRedSpot(currentSkeleton);
                    lockHipRightWithRedSpot(currentSkeleton);
                    lockKneeLeftWithRedSpot(currentSkeleton);
                    lockKneeRightWithRedSpot(currentSkeleton);
                    lockAnkleLeftWithRedSpot(currentSkeleton);
                    lockAnkleRightWithRedSpot(currentSkeleton);
                    lockFootLeftWithRedSpot(currentSkeleton);
                    lockFootRightWithRedSpot(currentSkeleton);
                }
            }

        }
    } 

    /// <summary>
    /// 鎖定頭部骨骼點
    /// </summary>
    /// <param name="s"></param>
    private void lockHeadWithRedSpot(Skeleton s)
    {
        Joint head = s.Joints[JointType.Head];

        //將頭部的骨骼點座標映射到彩色視頻流座標中
        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(head.Position, _kinect.ColorStream.Format);

        //將座標位置等比例顯示縮小
        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        //畫布的位置與imageCamera重疊
        Canvas.SetLeft(ellipseShoulderCenter, p.X);
        Canvas.SetTop(ellipseShoulderCenter, p.Y);
    }

    private void lockHandRightWithRedSpot(Skeleton s)
    {
        Joint handRight = s.Joints[JointType.HandRight];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(handRight.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseHandRight, p.X);
        Canvas.SetTop(ellipseHandRight, p.Y);
    }
    private void lockHandLeftWithRedSpot(Skeleton s)
    {
        Joint handLeft = s.Joints[JointType.HandLeft];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(handLeft.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseHandLeft, p.X);
        Canvas.SetTop(ellipseHandLeft, p.Y);
    }
    private void lockWristRightWithRedSpot(Skeleton s)
    {
        Joint wristRight = s.Joints[JointType.WristRight];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(wristRight.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseWristRight, p.X);
        Canvas.SetTop(ellipseWristRight, p.Y);
    }
    private void lockWristLeftWithRedSpot(Skeleton s)
    {
        Joint wristLeft = s.Joints[JointType.WristLeft];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(wristLeft.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseWristLeft, p.X);
        Canvas.SetTop(ellipseWristLeft, p.Y);
    }
    private void lockElbowRightWithRedSpot(Skeleton s)
    {
        Joint elbowRight = s.Joints[JointType.ElbowRight];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(elbowRight.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseElbowRight, p.X);
        Canvas.SetTop(ellipseElbowRight, p.Y);
    }

    private void lockElbowLeftWithRedSpot(Skeleton s)
    {
        Joint elbowLeft = s.Joints[JointType.ElbowLeft];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(elbowLeft.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseElbowLeft, p.X);
        Canvas.SetTop(ellipseElbowLeft, p.Y);
    }

    private void lockShoulderLeftWithRedSpot(Skeleton s)
    {
        Joint shoulderLeft = s.Joints[JointType.ShoulderLeft];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(shoulderLeft.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseShoulderLeft, p.X);
        Canvas.SetTop(ellipseShoulderLeft, p.Y);
    }

    private void lockShoulderRightWithRedSpot(Skeleton s)
    {
        Joint shoulderRight = s.Joints[JointType.ShoulderRight];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(shoulderRight.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseShoulderRight, p.X);
        Canvas.SetTop(ellipseShoulderRight, p.Y);
    }


    private void lockShoulderCenterWithRedSpot(Skeleton s)
    {
        Joint shoulderCenter = s.Joints[JointType.ShoulderCenter];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(shoulderCenter.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseShoulderCenter, p.X);
        Canvas.SetTop(ellipseShoulderCenter, p.Y);
    }

    private void lockSpineWithRedSpot(Skeleton s)
    {
        Joint spine = s.Joints[JointType.Spine];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(spine.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseSpine, p.X);
        Canvas.SetTop(ellipseSpine, p.Y);
    }

    private void lockHipCenterWithRedSpot(Skeleton s)
    {
        Joint hipCenter = s.Joints[JointType.HipCenter];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(hipCenter.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseHipCenter, p.X);
        Canvas.SetTop(ellipseHipCenter, p.Y);
    }

    private void lockHipLeftWithRedSpot(Skeleton s)
    {
        Joint hipLeft = s.Joints[JointType.HipLeft];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(hipLeft.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseHipLeft, p.X);
        Canvas.SetTop(ellipseHipLeft, p.Y);
    }

    private void lockHipRightWithRedSpot(Skeleton s)
    {
        Joint hipRight = s.Joints[JointType.HipRight];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(hipRight.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseHipRight, p.X);
        Canvas.SetTop(ellipseHipRight, p.Y);
    }

    private void lockKneeLeftWithRedSpot(Skeleton s)
    {
        Joint kneeLeft = s.Joints[JointType.KneeLeft];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(kneeLeft.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseKneeLeft, p.X);
        Canvas.SetTop(ellipseKneeLeft, p.Y);
    }

    private void lockKneeRightWithRedSpot(Skeleton s)
    {
        Joint kneeRight = s.Joints[JointType.KneeRight];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(kneeRight.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseKneeRight, p.X);
        Canvas.SetTop(ellipseKneeRight, p.Y);
    }


    private void lockAnkleLeftWithRedSpot(Skeleton s)
    {
        Joint ankleLeft = s.Joints[JointType.AnkleLeft];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(ankleLeft.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseAnkleLeft, p.X);
        Canvas.SetTop(ellipseAnkleLeft, p.Y);
    }


    private void lockAnkleRightWithRedSpot(Skeleton s)
    {
        Joint ankleRight = s.Joints[JointType.AnkleRight];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(ankleRight.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseAnkleRight, p.X);
        Canvas.SetTop(ellipseAnkleRight, p.Y);
    }


    private void lockFootLeftWithRedSpot(Skeleton s)
    {
        Joint footLeft = s.Joints[JointType.FootLeft];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(footLeft.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseFootLeft, p.X);
        Canvas.SetTop(ellipseFootLeft, p.Y);
    }

    private void lockFootRightWithRedSpot(Skeleton s)
    {
        Joint footRight = s.Joints[JointType.FootRight];

        ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(footRight.Position, _kinect.ColorStream.Format);

        Point p = new Point(
            (int)(imageCanera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
            (int)(imageCanera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight));

        Canvas.SetLeft(ellipseFootRight, p.X);
        Canvas.SetTop(ellipseFootRight, p.Y);
    }
    public MainWindow()
    {
        InitializeComponent();
    }

    //窗體啓動事件
    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        startKinect();
    }

    private void Window_Closed(object sender, EventArgs e)
    {
        stopKinect();
    }
}

}函數

相關文章
相關標籤/搜索