/
MainWindow.xaml.cs
232 lines (211 loc) · 10 KB
/
MainWindow.xaml.cs
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// -----------------------------------------------------------------------
// <copyright file="MainWindow.xaml.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// -----------------------------------------------------------------------
namespace FaceTrackingBasics
{
using System;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Microsoft.Kinect;
using Microsoft.Kinect.Toolkit;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private static readonly int Bgr32BytesPerPixel = (PixelFormats.Bgr32.BitsPerPixel + 7) / 8;
private readonly KinectSensorChooser sensorChooser = new KinectSensorChooser();
private WriteableBitmap colorImageWritableBitmap;
private byte[] colorImageData;
private ColorImageFormat currentColorImageFormat = ColorImageFormat.Undefined;
private double currentDepth = 0;
private int depthRange = 400;
public MainWindow()
{
InitializeComponent();
var faceTrackingViewerBinding = new Binding("Kinect") { Source = sensorChooser };
faceTrackingViewer.SetBinding(FaceTrackingViewer.KinectProperty, faceTrackingViewerBinding);
sensorChooser.KinectChanged += SensorChooserOnKinectChanged;
sensorChooser.Start();
}
private void SensorChooserOnKinectChanged(object sender, KinectChangedEventArgs kinectChangedEventArgs)
{
KinectSensor oldSensor = kinectChangedEventArgs.OldSensor;
KinectSensor newSensor = kinectChangedEventArgs.NewSensor;
if (oldSensor != null)
{
oldSensor.AllFramesReady -= KinectSensorOnAllFramesReady;
oldSensor.ColorStream.Disable();
oldSensor.DepthStream.Disable();
oldSensor.DepthStream.Range = DepthRange.Default;
oldSensor.SkeletonStream.Disable();
oldSensor.SkeletonStream.EnableTrackingInNearRange = false;
oldSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Default;
}
if (newSensor != null)
{
try
{
newSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
newSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);
try
{
// This will throw on non Kinect For Windows devices.
newSensor.DepthStream.Range = DepthRange.Near;
newSensor.SkeletonStream.EnableTrackingInNearRange = true;
}
catch (InvalidOperationException)
{
newSensor.DepthStream.Range = DepthRange.Default;
newSensor.SkeletonStream.EnableTrackingInNearRange = false;
}
newSensor.SkeletonStream.TrackingMode = SkeletonTrackingMode.Seated;
newSensor.SkeletonStream.Enable();
newSensor.AllFramesReady += KinectSensorOnAllFramesReady;
}
catch (InvalidOperationException)
{
// This exception can be thrown when we are trying to
// enable streams on a device that has gone away. This
// can occur, say, in app shutdown scenarios when the sensor
// goes away between the time it changed status and the
// time we get the sensor changed notification.
//
// Behavior here is to just eat the exception and assume
// another notification will come along if a sensor
// comes back.
}
}
}
private void WindowClosed(object sender, EventArgs e)
{
sensorChooser.Stop();
faceTrackingViewer.Dispose();
}
private void KinectSensorOnAllFramesReady(object sender, AllFramesReadyEventArgs allFramesReadyEventArgs)
{
var colorImageFrame = allFramesReadyEventArgs.OpenColorImageFrame();
var depthImageFrame = allFramesReadyEventArgs.OpenDepthImageFrame();
var skeltImageFrame = allFramesReadyEventArgs.OpenSkeletonFrame();
try
{
{
if (colorImageFrame == null)
{
return;
}
if (depthImageFrame == null)
{
return;
}
if (skeltImageFrame == null)
{
return;
}
DepthImagePixel[] depthPixels = new DepthImagePixel[depthImageFrame.PixelDataLength];
depthImageFrame.CopyDepthImagePixelDataTo(depthPixels);
int minDepth = depthImageFrame.MinDepth;
int maxDepth = depthImageFrame.MaxDepth;
var ratio = colorImageFrame.PixelDataLength / depthImageFrame.PixelDataLength;
var heightC = colorImageFrame.Height;
var heightD = depthImageFrame.Height;
var LengthC = colorImageFrame.PixelDataLength;
var LengthD = depthPixels.Length;
var ratH = colorImageFrame.Height / depthImageFrame.Height;
var ratW = colorImageFrame.Width / depthImageFrame.Width;
// Make a copy of the color frame for displaying.
var haveNewFormat = this.currentColorImageFormat != colorImageFrame.Format;
if (haveNewFormat)
{
this.currentColorImageFormat = colorImageFrame.Format;
this.colorImageData = new byte[colorImageFrame.PixelDataLength];
this.colorImageWritableBitmap = new WriteableBitmap(
colorImageFrame.Width, colorImageFrame.Height, 96, 96, PixelFormats.Bgr32, null);
ColorImage.Source = this.colorImageWritableBitmap;
}
colorImageFrame.CopyPixelDataTo(this.colorImageData);
for (int i = 0; i < colorImageFrame.Width; ++i)
{
for (int j = 0; j < colorImageFrame.Height; ++j)
{
// var ratH = colorImageFrame.Height / depthImageFrame.Height;
// var ratW = colorImageFrame.Width / depthImageFrame.Width;
// var srcX = (i / ratH) + (i / ratW);
// var srcPixel = i * depthImageFrame.Height / colorImageFrame.Height + ((int)(i * depthImageFrame.Height / colorImageFrame.Height) % depthImageFrame.Height);
// var srcY = 1;
int srcX = i / ratW;
int srcY = j / ratH;
int srcPixel = srcX + 2 + ((srcY - 15) * depthImageFrame.Width);
int tgtPixel = (i + (j * colorImageFrame.Width));
if (srcPixel >= 0 && srcPixel < depthPixels.Length)
{
currentDepth = currentDepth + .00001;
short depth = depthPixels[(int)srcPixel].Depth;
if (depth < (int)minDepth + currentDepth)
{
changePixel(tgtPixel, 255);
// changePixel(tgtPixel, new byte[]{255, 255, 255});
}
//else if (depth > maxDepth)
else if (depth > (int) (minDepth + currentDepth + depthRange))
{
changePixel(tgtPixel, 255);
}
else
{
// changePixel(tgtPixel, 0);
}
if (currentDepth + depthRange>=maxDepth + 300)
{
currentDepth = minDepth;
}
}
else
{
}
}
}
this.colorImageWritableBitmap.WritePixels(
new Int32Rect(0, 0, colorImageFrame.Width, colorImageFrame.Height),
this.colorImageData,
colorImageFrame.Width * Bgr32BytesPerPixel,
0);
}
}
finally
{
if (colorImageFrame != null)
{
colorImageFrame.Dispose();
}
if (depthImageFrame != null)
{
depthImageFrame.Dispose();
}
if (skeltImageFrame != null)
{
skeltImageFrame.Dispose();
}
}
}
private void changePixel(int pixel, byte value)
{
changePixel(pixel, new byte[]{value, value, value});
}
private void changePixel(int pixel, byte[] values)
{
int tgt = pixel * 4;
if (tgt >= 0 && tgt < this.colorImageData.Length)
{
for (int k = 0; k < 3; k++)
{
this.colorImageData[tgt++] = values[k];
}
}
}
}
}