Hola!
Ahora que ya pasé por los clásicos Body, Camera y Depth Sensor, vamos con uno de las novedades más interesantes del SDK: el sensor de infrarojos (a partir de ahora IR Sensor).
Al igual que en los ejemplos anteriores, es necesario abrir un READER del tipo IRReader y luego cuando capturamos un frame del tipo IR realizamos las siguientes acciones
- null validations
- validar el tamaño del IR Frame
- validar la intensidad de la señal del IR
- el valor de la señal está entre 1 y 8 con lo que utilizando estos valores, podemos definir los valores RGB a mostrar en la imagen
- Creamos un BITMAP y lo mostramos en la Window
1: public partial class MainWindow : INotifyPropertyChanged
2: {
3: private string _statusText;
4: private readonly WriteableBitmap _bitmap;
5: private KinectSensor _kinectSensor;
6: private readonly ushort[] _frameData;
7: private readonly int _bytesPerPixel = (PixelFormats.Bgr32.BitsPerPixel + 7) / 8;
8: private readonly byte[] _pixels;
9: private InfraredFrameReader _reader;
10:
11: public string StatusText
12: {
13: get
14: {
15: return _statusText;
16: }
17:
18: set
19: {
20: if (_statusText == value) return;
21: _statusText = value;
22:
23: if (PropertyChanged != null)
24: {
25: PropertyChanged(this, new PropertyChangedEventArgs("StatusText"));
26: }
27: }
28: }
29:
30: public ImageSource ImageSource
31: {
32: get
33: {
34: return _bitmap;
35: }
36: }
37:
38: public MainWindow()
39: {
40: _kinectSensor = KinectSensor.Default;
41:
42: if (_kinectSensor != null)
43: {
44: _kinectSensor.Open();
45:
46: var frameDescription = _kinectSensor.InfraredFrameSource.FrameDescription;
47: _reader = _kinectSensor.InfraredFrameSource.OpenReader();
48: _frameData = new ushort[frameDescription.Width * frameDescription.Height];
49: _pixels = new byte[frameDescription.Width * frameDescription.Height * _bytesPerPixel];
50: _bitmap = new WriteableBitmap(frameDescription.Width, frameDescription.Height, 96.0, 96.0, PixelFormats.Bgr32, null);
51: StatusText = "Kinect Started";
52: }
53: else
54: {
55: StatusText = "error initializing the kinect sensor";
56: }
57:
58: DataContext = this;
59: InitializeComponent();
60: Loaded += MainWindow_Loaded;
61: Closing += MainWindow_Closing;
62: }
63:
64: void MainWindow_Closing(object sender, CancelEventArgs e)
65: {
66: if (_reader != null)
67: {
68: _reader.Dispose();
69: _reader = null;
70: }
71:
72: if (_kinectSensor == null) return;
73: _kinectSensor.Close();
74: _kinectSensor = null;
75: }
76:
77: void MainWindow_Loaded(object sender, RoutedEventArgs e)
78: {
79: if (_reader != null)
80: {
81: _reader.FrameArrived += IrReaderFrameArrived;
82: }
83: }
84:
85: void IrReaderFrameArrived(object sender, InfraredFrameArrivedEventArgs e)
86: {
87: var frameReference = e.FrameReference;
88: try
89: {
90: var frame = frameReference.AcquireFrame();
91: if (frame == null) return;
92: using (frame)
93: {
94: var frameDescription = frame.FrameDescription;
95: if (((frameDescription.Width*frameDescription.Height) != _frameData.Length) ||
96: (frameDescription.Width != _bitmap.PixelWidth) ||
97: (frameDescription.Height != _bitmap.PixelHeight)) return;
98: frame.CopyFrameDataToArray(_frameData);
99: var colorPixelIndex = 0;
100: foreach (var ir in _frameData)
101: {
102: var intensity = (byte)(ir >> 8);
103: // Write out blue, green and red byte
104: _pixels[colorPixelIndex++] = intensity;
105: _pixels[colorPixelIndex++] = intensity;
106: _pixels[colorPixelIndex++] = intensity;
107: ++colorPixelIndex;
108: }
109:
110: _bitmap.WritePixels(
111: new Int32Rect(0, 0, frameDescription.Width, frameDescription.Height),
112: _pixels,
113: frameDescription.Width * _bytesPerPixel,
114: 0);
115: }
116: }
117: catch {}
118: }
119:
120: public event PropertyChangedEventHandler PropertyChanged;
121:
122: [NotifyPropertyChangedInvocator]
123: protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
124: {
125: PropertyChangedEventHandler handler = PropertyChanged;
126: if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
127: }
128: }
Como este ejemplo es más completo, dejo una grabación de la señal de IR de ejemplo, con la app que muestra el feed de la cámara al lado para comparar.
Y después de un repaso por Body, y como abrir y cerrar los sensores del Kinect, crearé alguna aplicación.
Disclaimer:
“This is preliminary software and/or hardware and APIs are preliminary and subject to change“
Saludos @ El medio de la montaña
El Bruno
Leave a comment