[Kinect] Day 1 - 讓程式碼先能動

Kinect Data Source

- Color
- Infrared
- Depth
- Body
- BodyIndex    

For Windows 8.1 App

- 開啟microphone, webcam權限
- 加入WindowsPreview.Kinect
- 建置CPU設定為x86

取得kinectSensor的方式

1
2
3
this.sensor = KinectSensor.GetDefault();
this.sensor.Open();
this.sensor.Close();

Readers

1
2
InfraredFrameReader reader = this.sensor.InfraredFrameSource.OpenReader();
reader.FrameArrived += InfraredReaderFrameArrvied;

Frame references

- Send in frame event args
- AcquireFrame : access to the actual frame
- RelativeTime : allow to templorally correlate frames
1
2
3
4
5
6
7
8
9
10
11
private void InfraredReaderFrameArrvied(InfraredFrameReader sender, InfraredFrameArrivedEventArgs args)
{
using (InfraredFrame frame = args.FrameReference.AcquireFrame())
{
if (frame != null)
{
// Get What you need from the frame

}
}
}

Frame

- Gives access to the frame data
    - Make a local copy of access the underlying buffer directly
- Contains metadata for the frame
    - Color, Format, width, height,etc.
- Important: Not Disposing frames will cause you to not receive more frames

Demo Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WindowsPreview.Kinect;
using Windows.UI.Xaml.Media.Imaging;

namespace KinectStudy_01
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}

KinectSensor sensor;
InfraredFrameReader reader;
ushort[] irData;
byte[] irDateConverted;
WriteableBitmap irBitmap;

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
sensor = KinectSensor.GetDefault();
reader = sensor.InfraredFrameSource.OpenReader();
FrameDescription fd = sensor.InfraredFrameSource.FrameDescription;
irData = new ushort[fd.LengthInPixels];
irDateConverted = new byte[fd.LengthInPixels * 4];
irBitmap = new WriteableBitmap(fd.Width, fd.Height);
image.Source = irBitmap;

sensor.Open();
reader.FrameArrived += InfraredReaderFrameArrvied;
}


private void InfraredReaderFrameArrvied(InfraredFrameReader sender, InfraredFrameArrivedEventArgs args)
{
using (InfraredFrame frame = args.FrameReference.AcquireFrame())
{
if (frame != null)
{
// Get What you need from the frame
frame.CopyFrameDataToArray(irData);
for (int i = 0; i < irData.Length; i++)
{
byte intensity = (byte)(irData[i] >> 8);
irDateConverted[i * 4] = intensity;
irDateConverted[i * 4 + 1] = intensity;
irDateConverted[i * 4 + 2] = intensity;
irDateConverted[i * 4 + 3] = 255;
}

irDateConverted.CopyTo(irBitmap.PixelBuffer);
irBitmap.Invalidate();
}
}

}
}
}

Demo2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using WindowsPreview.Kinect;
using Windows.UI.Xaml.Media.Imaging;

using Windows.UI.Xaml.Shapes;
using Windows.UI;


namespace KinectStudy_01
{
public sealed partial class MainPage : Page
{
KinectSensor sensor;
InfraredFrameReader reader;
ushort[] irData;
byte[] irDateConverted;
WriteableBitmap irBitmap;

Body[] bodies;
MultiSourceFrameReader msfr;

public MainPage()
{
this.InitializeComponent();
this.Loaded += MainPage_Loaded;
}

private void MainPage_Loaded(object sender, RoutedEventArgs e)
{
sensor = KinectSensor.GetDefault();
reader = sensor.InfraredFrameSource.OpenReader();
FrameDescription fd = sensor.InfraredFrameSource.FrameDescription;
irData = new ushort[fd.LengthInPixels];
irDateConverted = new byte[fd.LengthInPixels * 4];
irBitmap = new WriteableBitmap(fd.Width, fd.Height);
image.Source = irBitmap;

bodies = new Body[6];
msfr = sensor.OpenMultiSourceFrameReader(FrameSourceTypes.Body | FrameSourceTypes.Infrared);
msfr.MultiSourceFrameArrived += msfr_MultiSourceFrameArrived;

sensor.Open();
}

void msfr_MultiSourceFrameArrived(MultiSourceFrameReader sender, MultiSourceFrameArrivedEventArgs args)
{
using (MultiSourceFrame frame = args.FrameReference.AcquireFrame())
{
if (frame != null)
{
using (BodyFrame bodyframe = frame.BodyFrameReference.AcquireFrame())
{
using (InfraredFrame ifFrame = frame.InfraredFrameReference.AcquireFrame())
{
if (bodyframe != null && ifFrame != null)
{
// Get What you need from the frame
ifFrame.CopyFrameDataToArray(irData);
for (int i = 0; i < irData.Length; i++)
{
byte intensity = (byte)(irData[i] >> 8);
irDateConverted[i * 4] = intensity;
irDateConverted[i * 4 + 1] = intensity;
irDateConverted[i * 4 + 2] = intensity;
irDateConverted[i * 4 + 3] = 255;
}

irDateConverted.CopyTo(irBitmap.PixelBuffer);
irBitmap.Invalidate();

// 取得身體的相關資料
bodyframe.GetAndRefreshBodyData(bodies);
bodyCanvas.Children.Clear();
foreach (Body body in bodies)
{
if (body.IsTracked)
{
// 取得頭的關節位置
Joint headJoint = body.Joints[JointType.Head];
if (headJoint.TrackingState == TrackingState.Tracked)
{
// 轉換成DepthSpacePoint
DepthSpacePoint dsp = sensor.CoordinateMapper.MapCameraPointToDepthSpace(headJoint.Position);
// 在該位置上畫圓圈
Ellipse headCircle = new Ellipse() { Width = 50, Height = 50, Fill = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0)) };
bodyCanvas.Children.Add(headCircle);
Canvas.SetLeft(headCircle, dsp.X - 25);
Canvas.SetTop(headCircle, dsp.Y - 25);
}
}
}
}
}
}
}
}
}
}
}