-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainWindow.xaml.cs
228 lines (184 loc) · 8.73 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Diagnostics;
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.IO;
namespace Kinect_RGBD_Recorder
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
const int MapDepthToByte = 8000 / 256;
KinectSensor ks;
MultiSourceFrameReader multiSourceFrameReader;
CoordinateMapper coordinateMapper;
byte[] colorPixels;
byte[] depthPixels;
ColorImageFormat colorFormat;
ColorImageFormat depthFormat;
WriteableBitmap writeableBitmap;
BitmapSource colorBitmapSource;
BitmapSource depthBitmapSource;
int imageSerial;
bool recordStarted;
DepthSpacePoint[] colorMappedToDepthPoints;
FrameDescription colorFrameDesc;
FrameDescription depthFrameDesc;
public MainWindow()
{
InitializeComponent();
ks = KinectSensor.GetDefault();
ks.Open();
coordinateMapper = ks.CoordinateMapper;
colorFrameDesc = ks.ColorFrameSource.CreateFrameDescription(ColorImageFormat.Bgra);
int colorWidth = colorFrameDesc.Width;
int colorHeight = colorFrameDesc.Height;
depthFrameDesc = ks.DepthFrameSource.FrameDescription;
uint colorFrameSize = colorFrameDesc.BytesPerPixel * colorFrameDesc.LengthInPixels;
uint depthFrameSize = depthFrameDesc.BytesPerPixel * depthFrameDesc.LengthInPixels;
colorPixels = new byte[colorFrameSize];
depthPixels = new byte[depthFrameSize];
colorMappedToDepthPoints = new DepthSpacePoint[colorWidth * colorHeight];
recordStarted = false;
// Deleting all previous image in ./rgb directory
System.IO.DirectoryInfo rgb_directory = new DirectoryInfo("./rgb/");
foreach (FileInfo file in rgb_directory.GetFiles())
{
file.Delete();
}
// Deleting all previous image in ./d directory
System.IO.DirectoryInfo d_directory = new DirectoryInfo("./d/");
foreach (FileInfo file in d_directory.GetFiles())
{
file.Delete();
}
multiSourceFrameReader = ks.OpenMultiSourceFrameReader(FrameSourceTypes.Depth | FrameSourceTypes.Color);
multiSourceFrameReader.MultiSourceFrameArrived += msfr_FrameArrived;
start.Click += start_Click;
stop.Click += stop_Click;
}
private void msfr_FrameArrived(object sender, MultiSourceFrameArrivedEventArgs e)
{
bool depthFrameProcessed = false;
if (e.FrameReference == null) return;
DepthFrame depthFrame = null;
ColorFrame colorFrame = null;
MultiSourceFrame multiSourceFrame = e.FrameReference.AcquireFrame();
// If the Frame has expired by the time we process this event, return.
if (multiSourceFrame == null)
{
return;
}
// We use a try/finally to ensure that we clean up before we exit the function.
// This includes calling Dispose on any Frame objects that we may have and unlocking the bitmap back buffer.
try
{
colorFrame = multiSourceFrame.ColorFrameReference.AcquireFrame();
depthFrame = multiSourceFrame.DepthFrameReference.AcquireFrame();
// If any frame has expired by the time we process this event, return.
// The "finally" statement will Dispose any that are not null.
if ( (colorFrame == null) || (depthFrame == null) )
{
return;
}
// Access the depth frame data directly via LockImageBuffer to avoid making a copy
using (KinectBuffer depthBuffer = depthFrame.LockImageBuffer())
{
coordinateMapper.MapColorFrameToDepthSpaceUsingIntPtr(
depthBuffer.UnderlyingBuffer,
depthBuffer.Size,
this.colorMappedToDepthPoints);
// Note: In order to see the full range of depth (including the less reliable far field depth)
// we are setting maxDepth to the extreme potential depth threshold
ushort maxDepth = ushort.MaxValue;
// If you wish to filter by reliable depth distance, uncomment the following line:
//// maxDepth = depthFrame.DepthMaxReliableDistance
ProcessDepthFrameData(depthBuffer.UnderlyingBuffer, depthBuffer.Size, depthFrame.DepthMinReliableDistance, maxDepth);
depthFrameProcessed = true;
}
colorFrame.CopyConvertedFrameDataToArray(colorPixels, ColorImageFormat.Bgra);
//depthFrame.CopyFrameDataToArray(depthPixels);
// Creating BitmapSource
var bytesPerPixel = (PixelFormats.Bgr32.BitsPerPixel) / 8;
var colorStride = bytesPerPixel * colorFrame.FrameDescription.Width;
//var depthStride = bytesPerPixel * depthFrame.FrameDescription.Width;
colorBitmapSource = BitmapSource.Create(colorFrameDesc.Width, colorFrameDesc.Height, 96.0, 96.0, PixelFormats.Bgr32, null, colorPixels, colorStride);
//depthBitmapSource = BitmapSource.Create(depthFrameDesc.Width, depthFrameDesc.Height, 96.0, 96.0, PixelFormats.Gray8, null);
// WriteableBitmap to show on UI
writeableBitmap = new WriteableBitmap(this.depthFrameDesc.Width, this.depthFrameDesc.Height, 96.0, 96.0, PixelFormats.Gray8, null);
kinectImage.Source = writeableBitmap;
if (depthFrameProcessed)
{
writeableBitmap.WritePixels(new Int32Rect(0, 0, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight),
depthPixels,
writeableBitmap.PixelWidth,
0);
}
// We're done with the DepthFrame
depthFrame.Dispose();
depthFrame = null;
// We're done with the ColorFrame
colorFrame.Dispose();
colorFrame = null;
}
finally
{
if (depthFrame != null)
{
depthFrame.Dispose();
}
if (colorFrame != null)
{
colorFrame.Dispose();
}
}
}
/// <summary>
/// Directly accesses the underlying image buffer of the DepthFrame to
/// create a displayable bitmap.
/// This function requires the /unsafe compiler option as we make use of direct
/// access to the native memory pointed to by the depthFrameData pointer.
/// </summary>
/// <param name="depthFrameData">Pointer to the DepthFrame image data</param>
/// <param name="depthFrameDataSize">Size of the DepthFrame image data</param>
/// <param name="minDepth">The minimum reliable depth value for the frame</param>
/// <param name="maxDepth">The maximum reliable depth value for the frame</param>
private unsafe void ProcessDepthFrameData(IntPtr depthFrameData, uint depthFrameDataSize, ushort minDepth, ushort maxDepth)
{
// depth frame data is a 16 bit value
ushort* frameData = (ushort*)depthFrameData;
// convert depth to a visual representation
for (int i = 0; i < (int)(depthFrameDataSize / this.depthFrameDesc.BytesPerPixel); ++i)
{
// Get the depth for this pixel
ushort depth = frameData[i];
// To convert to a byte, we're mapping the depth value to the byte range.
// Values outside the reliable depth range are mapped to 0 (black).
this.depthPixels[i] = (byte)(depth >= minDepth && depth <= maxDepth ? (depth / MapDepthToByte) : 0);
}
}
private void start_Click(object sender, RoutedEventArgs e)
{
imageSerial = 0;
recordStarted = true;
}
private void stop_Click(object sender, RoutedEventArgs e)
{
recordStarted = false;
}
}
}