Kinect影象 骨骼點夾角處理

weixin_34320159發表於2017-09-15
7925105-c84a195976f70330.png

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;

using System.Windows.Forms;

namespace WpfApplication1

{

///

/// MainWindow.xaml 的互動邏輯

///

public partial class MainWindow : Window

{

Cl c = new Cl();

public MainWindow()

{

InitializeComponent();

}

//感測器裝置

KinectSensor _sensor;

//資源讀取

MultiSourceFrameReader _reader;

//骨骼點集合

IList _bodies;

private void Window_Loaded(object sender, RoutedEventArgs e)

{

//獲取預設裝置

_sensor = KinectSensor.GetDefault();

if (_sensor != null)

{

_sensor.Open();

//開啟一個新的流讀取器

_reader = _sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Color | FrameSourceTypes.Depth | FrameSourceTypes.Infrared | FrameSourceTypes.Body);

//幀變化事件

_reader.MultiSourceFrameArrived += _reader_MultiSourceFrameArrived;

}

}

void _reader_MultiSourceFrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)

{

//throw new NotImplementedException();

//從框架參考集中獲取框架

var reference = e.FrameReference.AcquireFrame();

//從該資源的顏色框架中獲取資源

using (var frame = reference.ColorFrameReference.AcquireFrame())

{

if (frame != null)

{

img.Source = frame.ToBitmap();

}

}

//從該資源的骨骼框架中獲取資源

using (var frame = reference.BodyFrameReference.AcquireFrame())

{

if (frame != null)

{

//建立一個骨骼點集合

_bodies = new Body[frame.BodyFrameSource.BodyCount];

//獲取更新的主體資料的列表。***********************************

frame.GetAndRefreshBodyData(_bodies);

foreach (var body in _bodies)

{

//判斷是否被跟蹤

if (body.IsTracked == true)

{

// 獲取左手骨骼點 point資訊

Joint leftHand = body.Joints[JointType.HandLeft];

//獲取左肩骨骼點 point資訊

Joint leftShoulder = body.Joints[JointType.ShoulderLeft];

//判斷檢測狀態

if (leftHand.TrackingState == TrackingState.Tracked)

{

//算夾角 以中心法線為標準

double a = c.deg(leftHand, leftShoulder);

leftPx.Text = a.ToString();

if (a < 90)

{

//模擬按鍵A

System.Windows.Forms.SendKeys.SendWait("{A}");

}

}

}

}

}

}

}

}

}

相關文章