質問編集履歴

2

kinect追加

2016/11/30 08:59

投稿

BOSS723
BOSS723

スコア35

test CHANGED
File without changes
test CHANGED
File without changes

1

全体のソースを載せた

2016/11/30 08:59

投稿

BOSS723
BOSS723

スコア35

test CHANGED
File without changes
test CHANGED
@@ -6,17 +6,175 @@
6
6
 
7
7
  拙い文章で申し訳ないですが、なにかアドバイスがもらえるとうれしいです。
8
8
 
9
+ センサーはKinectです。
10
+
9
11
 
10
12
 
11
13
  ```C#
12
14
 
15
+ using System;
16
+
17
+ using System.Collections.Generic;
18
+
19
+ using System.Linq;
20
+
21
+ using System.Text;
22
+
23
+ using System.Threading.Tasks;
24
+
25
+ using System.Windows;
26
+
27
+ using System.Windows.Controls;
28
+
29
+ using System.Windows.Data;
30
+
31
+ using System.Windows.Documents;
32
+
33
+ using System.Windows.Input;
34
+
35
+ using System.Windows.Media;
36
+
37
+ using System.Windows.Media.Imaging;
38
+
39
+ using System.Windows.Navigation;
40
+
41
+ using System.Windows.Shapes;
42
+
43
+ using System.Drawing;
44
+
45
+ using System.Drawing.Imaging;
46
+
47
+
48
+
49
+ using Microsoft.Kinect;
50
+
51
+ using System.IO;
52
+
53
+
54
+
55
+ namespace KinectDepthRecoder3
56
+
13
- .
57
+ {
58
+
14
-
59
+ public partial class MainWindow : Window
60
+
15
- .
61
+ {
62
+
16
-
63
+ private KinectSensor sensor;
64
+
65
+ private DepthImagePixel[] depthPixels;
66
+
67
+ private byte[] colorPixels;
68
+
69
+ private WriteableBitmap colorBitmap;
70
+
71
+ private Int32Rect bitmapRect;
72
+
73
+
74
+
75
+ short[] depthFrame16 = new short[640 * 480];
76
+
77
+ byte[] depthFrame32 = new byte[640 * 480 * 4];
78
+
79
+
80
+
81
+ const int RED_IDX = 2;
82
+
83
+ const int GREEN_IDX = 1;
84
+
85
+ const int BLUE_IDX = 0;
86
+
87
+
88
+
89
+ int[] realDepth = new int[640 * 480];
90
+
91
+
92
+
93
+ public MainWindow()
94
+
17
- .
95
+ {
96
+
18
-
97
+ InitializeComponent();
98
+
99
+
100
+
101
+ //button.Click += new EventHandler(SensorDepthFrameReady);
102
+
103
+ }
104
+
105
+ private void Window_Loaded(object sender, RoutedEventArgs e)
106
+
107
+ {
108
+
109
+ foreach (var potentialSensor in KinectSensor.KinectSensors)
110
+
111
+ {
112
+
113
+ if (potentialSensor.Status == KinectStatus.Connected)
114
+
115
+ {
116
+
117
+ sensor = potentialSensor;
118
+
119
+ break;
120
+
121
+ }
122
+
123
+ }
124
+
125
+ if (null != sensor)
126
+
127
+ {
128
+
129
+ sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
130
+
131
+ sensor.DepthFrameReady += SensorDepthFrameReady;
132
+
133
+
134
+
135
+ sensor.DepthStream.Range = DepthRange.Near;
136
+
137
+
138
+
139
+ depthPixels = new DepthImagePixel[sensor.DepthStream.FramePixelDataLength];
140
+
141
+ colorPixels = new byte[sensor.DepthStream.FramePixelDataLength * sizeof(int)];
142
+
143
+
144
+
145
+ //画面上に表示するビットマップ
146
+
147
+ colorBitmap = new WriteableBitmap(sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
148
+
149
+
150
+
151
+ //画像のサイズ(640*480)
152
+
153
+ bitmapRect = new Int32Rect(0, 0, sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight);
154
+
155
+
156
+
157
+ try
158
+
159
+ {
160
+
161
+ sensor.Start();
162
+
163
+ }
164
+
165
+ catch (IOException)
166
+
167
+ {
168
+
169
+ sensor = null;
170
+
171
+ }
172
+
173
+ }
174
+
175
+ }
176
+
19
- private void convertDepthFrame(short[] depthFrame16)
177
+ private void convertDepthFrame(short[] depthFrame16)
20
178
 
21
179
  {
22
180
 
@@ -56,9 +214,7 @@
56
214
 
57
215
  }
58
216
 
59
-
60
-
61
- private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
217
+ private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
62
218
 
63
219
  {
64
220
 
@@ -78,6 +234,10 @@
78
234
 
79
235
  depthImage.Source = BitmapSource.Create(depthImageFrame.Width, depthImageFrame.Height, 96, 96, PixelFormats.Bgr32, null, depthFrame32, depthImageFrame.Width * 4);
80
236
 
237
+
238
+
239
+ //colorBitmap.WritePixels(bitmapRect, colorPixels, colorBitmap.PixelWidth * sizeof(int), 0);
240
+
81
241
  }
82
242
 
83
243
  }
@@ -88,7 +248,7 @@
88
248
 
89
249
  {
90
250
 
91
- var image = (WriteableBitmap)depthImage.Source;
251
+ var image = (WriteableBitmap)depthImage.Source; // ?
92
252
 
93
253
 
94
254
 
@@ -110,11 +270,23 @@
110
270
 
111
271
  }
112
272
 
273
+ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
274
+
113
- .
275
+ {
276
+
114
-
277
+ if (null != this.sensor)
278
+
115
- .
279
+ {
280
+
116
-
281
+ sensor.Stop();
282
+
117
- .
283
+ }
284
+
285
+ }
286
+
287
+ }
288
+
289
+ }
118
290
 
119
291
 
120
292