質問編集履歴
2
kinect追加
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
全体のソースを載せた
title
CHANGED
File without changes
|
body
CHANGED
@@ -2,13 +2,92 @@
|
|
2
2
|
下記に一部プログラムを示していますが、SensorDepthFrameReadyのdepthImage.Sourceをbutton_Clickのenc.Frames.Add(BitmapFrame.Create());にもっていきたいのですが...
|
3
3
|
button.Click += new EventHandler(SensorDepthFrameReady);や SensorDepthFrameReadyの関数呼び出しでいけるのかと思っていたのですがそれもできなかったので質問しました。
|
4
4
|
拙い文章で申し訳ないですが、なにかアドバイスがもらえるとうれしいです。
|
5
|
+
センサーはKinectです。
|
5
6
|
|
6
7
|
```C#
|
8
|
+
using System;
|
9
|
+
using System.Collections.Generic;
|
10
|
+
using System.Linq;
|
11
|
+
using System.Text;
|
12
|
+
using System.Threading.Tasks;
|
13
|
+
using System.Windows;
|
14
|
+
using System.Windows.Controls;
|
15
|
+
using System.Windows.Data;
|
16
|
+
using System.Windows.Documents;
|
17
|
+
using System.Windows.Input;
|
18
|
+
using System.Windows.Media;
|
19
|
+
using System.Windows.Media.Imaging;
|
20
|
+
using System.Windows.Navigation;
|
21
|
+
using System.Windows.Shapes;
|
22
|
+
using System.Drawing;
|
23
|
+
using System.Drawing.Imaging;
|
24
|
+
|
25
|
+
using Microsoft.Kinect;
|
26
|
+
using System.IO;
|
27
|
+
|
28
|
+
namespace KinectDepthRecoder3
|
7
|
-
|
29
|
+
{
|
30
|
+
public partial class MainWindow : Window
|
8
|
-
|
31
|
+
{
|
9
|
-
|
32
|
+
private KinectSensor sensor;
|
10
|
-
private
|
33
|
+
private DepthImagePixel[] depthPixels;
|
34
|
+
private byte[] colorPixels;
|
35
|
+
private WriteableBitmap colorBitmap;
|
36
|
+
private Int32Rect bitmapRect;
|
37
|
+
|
38
|
+
short[] depthFrame16 = new short[640 * 480];
|
39
|
+
byte[] depthFrame32 = new byte[640 * 480 * 4];
|
40
|
+
|
41
|
+
const int RED_IDX = 2;
|
42
|
+
const int GREEN_IDX = 1;
|
43
|
+
const int BLUE_IDX = 0;
|
44
|
+
|
45
|
+
int[] realDepth = new int[640 * 480];
|
46
|
+
|
47
|
+
public MainWindow()
|
11
48
|
{
|
49
|
+
InitializeComponent();
|
50
|
+
|
51
|
+
//button.Click += new EventHandler(SensorDepthFrameReady);
|
52
|
+
}
|
53
|
+
private void Window_Loaded(object sender, RoutedEventArgs e)
|
54
|
+
{
|
55
|
+
foreach (var potentialSensor in KinectSensor.KinectSensors)
|
56
|
+
{
|
57
|
+
if (potentialSensor.Status == KinectStatus.Connected)
|
58
|
+
{
|
59
|
+
sensor = potentialSensor;
|
60
|
+
break;
|
61
|
+
}
|
62
|
+
}
|
63
|
+
if (null != sensor)
|
64
|
+
{
|
65
|
+
sensor.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
|
66
|
+
sensor.DepthFrameReady += SensorDepthFrameReady;
|
67
|
+
|
68
|
+
sensor.DepthStream.Range = DepthRange.Near;
|
69
|
+
|
70
|
+
depthPixels = new DepthImagePixel[sensor.DepthStream.FramePixelDataLength];
|
71
|
+
colorPixels = new byte[sensor.DepthStream.FramePixelDataLength * sizeof(int)];
|
72
|
+
|
73
|
+
//画面上に表示するビットマップ
|
74
|
+
colorBitmap = new WriteableBitmap(sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight, 96, 96, PixelFormats.Bgr32, null);
|
75
|
+
|
76
|
+
//画像のサイズ(640*480)
|
77
|
+
bitmapRect = new Int32Rect(0, 0, sensor.DepthStream.FrameWidth, sensor.DepthStream.FrameHeight);
|
78
|
+
|
79
|
+
try
|
80
|
+
{
|
81
|
+
sensor.Start();
|
82
|
+
}
|
83
|
+
catch (IOException)
|
84
|
+
{
|
85
|
+
sensor = null;
|
86
|
+
}
|
87
|
+
}
|
88
|
+
}
|
89
|
+
private void convertDepthFrame(short[] depthFrame16)
|
90
|
+
{
|
12
91
|
for (int i16 = 0, i32 = 0, i = 0; i16 < depthFrame16.Length && i < depthFrame32.Length; i16 += 1, i32 += 4, i++)
|
13
92
|
{
|
14
93
|
realDepth[i] = (ushort)depthFrame16[i16] >> 3;
|
@@ -27,8 +106,7 @@
|
|
27
106
|
depthFrame32[i32 + BLUE_IDX] = (byte)pixel;
|
28
107
|
}
|
29
108
|
}
|
30
|
-
|
31
|
-
private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
|
109
|
+
private void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
|
32
110
|
{
|
33
111
|
using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame())
|
34
112
|
{
|
@@ -38,12 +116,14 @@
|
|
38
116
|
convertDepthFrame(depthFrame16);
|
39
117
|
|
40
118
|
depthImage.Source = BitmapSource.Create(depthImageFrame.Width, depthImageFrame.Height, 96, 96, PixelFormats.Bgr32, null, depthFrame32, depthImageFrame.Width * 4);
|
119
|
+
|
120
|
+
//colorBitmap.WritePixels(bitmapRect, colorPixels, colorBitmap.PixelWidth * sizeof(int), 0);
|
41
121
|
}
|
42
122
|
}
|
43
123
|
}
|
44
124
|
private void button_Click(object sender, RoutedEventArgs e)
|
45
125
|
{
|
46
|
-
var image = (WriteableBitmap)depthImage.Source;
|
126
|
+
var image = (WriteableBitmap)depthImage.Source; // ?
|
47
127
|
|
48
128
|
using (System.IO.FileStream fs = System.IO.File.Create(@"Z:kageE/kitsune.png"))
|
49
129
|
{
|
@@ -54,8 +134,14 @@
|
|
54
134
|
MessageBox.Show("保存しました");
|
55
135
|
}
|
56
136
|
}
|
137
|
+
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
|
57
|
-
|
138
|
+
{
|
139
|
+
if (null != this.sensor)
|
58
|
-
|
140
|
+
{
|
141
|
+
sensor.Stop();
|
59
|
-
|
142
|
+
}
|
143
|
+
}
|
144
|
+
}
|
145
|
+
}
|
60
146
|
|
61
147
|
```
|