-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExtensions.cs
More file actions
206 lines (158 loc) · 7.85 KB
/
Extensions.cs
File metadata and controls
206 lines (158 loc) · 7.85 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Microsoft.Kinect;
namespace FutureCloth
{
public static class Extensions
{
#region Camera
public static ImageSource ToBitmap(this ColorFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
PixelFormat format = PixelFormats.Bgr32;
byte[] pixels = new byte[width * height * ((format.BitsPerPixel + 7) / 8)];
if (frame.RawColorImageFormat == ColorImageFormat.Bgra)
{
frame.CopyRawFrameDataToArray(pixels);
}
else
{
frame.CopyConvertedFrameDataToArray(pixels, ColorImageFormat.Bgra);
}
int stride = width * format.BitsPerPixel / 8;
return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride);
}
public static ImageSource ToBitmap(this DepthFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
PixelFormat format = PixelFormats.Bgr32;
ushort minDepth = frame.DepthMinReliableDistance;
ushort maxDepth = frame.DepthMaxReliableDistance;
ushort[] pixelData = new ushort[width * height];
byte[] pixels = new byte[width * height * (format.BitsPerPixel + 7) / 8];
frame.CopyFrameDataToArray(pixelData);
int colorIndex = 0;
for (int depthIndex = 0; depthIndex < pixelData.Length; ++depthIndex)
{
ushort depth = pixelData[depthIndex];
byte intensity = (byte)(depth >= minDepth && depth <= maxDepth ? depth : 0);
pixels[colorIndex++] = intensity; // Blue
pixels[colorIndex++] = intensity; // Green
pixels[colorIndex++] = intensity; // Red
++colorIndex;
}
int stride = width * format.BitsPerPixel / 8;
return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride);
}
public static ImageSource ToBitmap(this InfraredFrame frame)
{
int width = frame.FrameDescription.Width;
int height = frame.FrameDescription.Height;
PixelFormat format = PixelFormats.Bgr32;
ushort[] frameData = new ushort[width * height];
byte[] pixels = new byte[width * height * (format.BitsPerPixel + 7) / 8];
frame.CopyFrameDataToArray(frameData);
int colorIndex = 0;
for (int infraredIndex = 0; infraredIndex < frameData.Length; infraredIndex++)
{
ushort ir = frameData[infraredIndex];
byte intensity = (byte)(ir >> 7);
pixels[colorIndex++] = (byte)(intensity / 1); // Blue
pixels[colorIndex++] = (byte)(intensity / 1); // Green
pixels[colorIndex++] = (byte)(intensity / 0.4); // Red
colorIndex++;
}
int stride = width * format.BitsPerPixel / 8;
return BitmapSource.Create(width, height, 96, 96, format, null, pixels, stride);
}
#endregion
/*
#region Drawing
public static void DrawSkeleton(this Canvas canvas, Body body)
{
if (body == null) return;
foreach (Joint joint in body.Joints.Values)
{
canvas.DrawPoint(joint);
}
canvas.DrawLine(body.Joints[JointType.Head], body.Joints[JointType.Neck]);
canvas.DrawLine(body.Joints[JointType.Neck], body.Joints[JointType.SpineShoulder]);
canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.ShoulderLeft]);
canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.ShoulderRight]);
canvas.DrawLine(body.Joints[JointType.SpineShoulder], body.Joints[JointType.SpineMid]);
canvas.DrawLine(body.Joints[JointType.ShoulderLeft], body.Joints[JointType.ElbowLeft]);
canvas.DrawLine(body.Joints[JointType.ShoulderRight], body.Joints[JointType.ElbowRight]);
canvas.DrawLine(body.Joints[JointType.ElbowLeft], body.Joints[JointType.WristLeft]);
canvas.DrawLine(body.Joints[JointType.ElbowRight], body.Joints[JointType.WristRight]);
canvas.DrawLine(body.Joints[JointType.WristLeft], body.Joints[JointType.HandLeft]);
canvas.DrawLine(body.Joints[JointType.WristRight], body.Joints[JointType.HandRight]);
canvas.DrawLine(body.Joints[JointType.HandLeft], body.Joints[JointType.HandTipLeft]);
canvas.DrawLine(body.Joints[JointType.HandRight], body.Joints[JointType.HandTipRight]);
canvas.DrawLine(body.Joints[JointType.HandTipLeft], body.Joints[JointType.ThumbLeft]);
canvas.DrawLine(body.Joints[JointType.HandTipRight], body.Joints[JointType.ThumbRight]);
canvas.DrawLine(body.Joints[JointType.SpineMid], body.Joints[JointType.SpineBase]);
canvas.DrawLine(body.Joints[JointType.SpineBase], body.Joints[JointType.HipLeft]);
canvas.DrawLine(body.Joints[JointType.SpineBase], body.Joints[JointType.HipRight]);
canvas.DrawLine(body.Joints[JointType.HipLeft], body.Joints[JointType.KneeLeft]);
canvas.DrawLine(body.Joints[JointType.HipRight], body.Joints[JointType.KneeRight]);
canvas.DrawLine(body.Joints[JointType.KneeLeft], body.Joints[JointType.AnkleLeft]);
canvas.DrawLine(body.Joints[JointType.KneeRight], body.Joints[JointType.AnkleRight]);
canvas.DrawLine(body.Joints[JointType.AnkleLeft], body.Joints[JointType.FootLeft]);
canvas.DrawLine(body.Joints[JointType.AnkleRight], body.Joints[JointType.FootRight]);
}
public static void DrawPointColorPoint(this Canvas canvas, ColorSpacePoint point)
{
Ellipse ellipse = new Ellipse
{
Width = 20,
Height = 20,
Fill = Brushes.Red
};
Canvas.SetLeft(ellipse, point.X - ellipse.Width / 2);
Canvas.SetTop(ellipse, point.Y - ellipse.Height / 2);
// Add the ellipse to the canvas.
canvas.Children.Add(ellipse);
}
public static void DrawPoint(this Canvas canvas, Joint joint)
{
if (joint.TrackingState == TrackingState.NotTracked) return;
joint = joint.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);
Ellipse ellipse = new Ellipse
{
Width = 20,
Height = 20,
Fill = new SolidColorBrush(Colors.Red)
};
Canvas.SetLeft(ellipse, joint.Position.X - ellipse.Width / 2);
Canvas.SetTop(ellipse, joint.Position.Y - ellipse.Height / 2);
canvas.Children.Add(ellipse);
}
public static void DrawLine(this Canvas canvas, Joint first, Joint second)
{
if (first.TrackingState == TrackingState.NotTracked || second.TrackingState == TrackingState.NotTracked) return;
first = first.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);
second = second.ScaleTo(canvas.ActualWidth, canvas.ActualHeight);
Line line = new Line
{
X1 = first.Position.X,
Y1 = first.Position.Y,
X2 = second.Position.X,
Y2 = second.Position.Y,
StrokeThickness = 8,
Stroke = new SolidColorBrush(Colors.LightBlue)
};
canvas.Children.Add(line);
}
#endregion
*/
}
}