|
11 | 11 | using Windows.UI.Xaml.Data; |
12 | 12 | using Windows.UI.Xaml.Input; |
13 | 13 | using Windows.UI.Xaml.Media; |
| 14 | +using Windows.UI.Xaml.Media.Imaging; |
14 | 15 | using Windows.UI.Xaml.Navigation; |
15 | 16 | using WindowsPreview.Kinect; |
| 17 | +using System.ComponentModel; |
16 | 18 |
|
17 | 19 | namespace Kinect2Sample |
18 | 20 | { |
19 | 21 |
|
20 | | - public sealed partial class MainPage : Page |
| 22 | + public sealed partial class MainPage : Page, INotifyPropertyChanged |
21 | 23 | { |
| 24 | + /// <summary> |
| 25 | + /// The highest value that can be returned in the InfraredFrame. |
| 26 | + /// It is cast to a float for readability in the visualization code. |
| 27 | + /// </summary> |
| 28 | + private const float InfraredSourceValueMaximum = (float)ushort.MaxValue; |
| 29 | + |
| 30 | + /// <summary> |
| 31 | + /// Used to set the lower limit, post processing, of the |
| 32 | + /// infrared data that we will render. |
| 33 | + /// Increasing or decreasing this value sets a brightness |
| 34 | + /// "wall" either closer or further away. |
| 35 | + /// </summary> |
| 36 | + private const float InfraredOutputValueMinimum = 0.01f; |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// The upper limit, post processing, of the |
| 40 | + /// infrared data that will render. |
| 41 | + /// </summary> |
| 42 | + private const float InfraredOutputValueMaximum = 1.0f; |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// The InfraredSceneValueAverage value specifies the average infrared |
| 46 | + /// value of the scene. This value was selected by analyzing the average |
| 47 | + /// pixel intensity for a given scene. |
| 48 | + /// This could be calculated at runtime to handle different IR conditions |
| 49 | + /// of a scene (outside vs inside). |
| 50 | + /// </summary> |
| 51 | + private const float InfraredSceneValueAverage = 0.08f; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// The InfraredSceneStandardDeviations value specifies the number of |
| 55 | + /// standard deviations to apply to InfraredSceneValueAverage. |
| 56 | + /// This value was selected by analyzing data from a given scene. |
| 57 | + /// This could be calculated at runtime to handle different IR conditions |
| 58 | + /// of a scene (outside vs inside). |
| 59 | + /// </summary> |
| 60 | + private const float InfraredSceneStandardDeviations = 3.0f; |
| 61 | + |
| 62 | + // Size of the RGB pixel in the bitmap |
| 63 | + private const int BytesPerPixel = 4; |
22 | 64 |
|
23 | 65 | private KinectSensor kinectSensor = null; |
| 66 | + private string statusText = null; |
| 67 | + private WriteableBitmap bitmap = null; |
| 68 | + private FrameDescription currentFrameDescription; |
| 69 | + |
| 70 | + //Infrared Frame |
| 71 | + private InfraredFrameReader infraredFrameReader = null; |
| 72 | + private ushort[] infraredFrameData = null; |
| 73 | + private byte[] infraredPixels = null; |
| 74 | + |
| 75 | + public event PropertyChangedEventHandler PropertyChanged; |
| 76 | + public string StatusText |
| 77 | + { |
| 78 | + get { return this.statusText; } |
| 79 | + set |
| 80 | + { |
| 81 | + if (this.statusText != value) |
| 82 | + { |
| 83 | + this.statusText = value; |
| 84 | + if (this.PropertyChanged != null) |
| 85 | + { |
| 86 | + this.PropertyChanged(this, new PropertyChangedEventArgs("StatusText")); |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public FrameDescription CurrentFrameDescription |
| 93 | + { |
| 94 | + get { return this.currentFrameDescription; } |
| 95 | + set |
| 96 | + { |
| 97 | + if (this.currentFrameDescription != value) |
| 98 | + { |
| 99 | + this.currentFrameDescription = value; |
| 100 | + if (this.PropertyChanged != null) |
| 101 | + { |
| 102 | + this.PropertyChanged(this, new PropertyChangedEventArgs("CurrentFrameDescription")); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
24 | 107 |
|
25 | 108 | public MainPage() |
26 | 109 | { |
27 | 110 | // one sensor is currently supported |
28 | 111 | this.kinectSensor = KinectSensor.GetDefault(); |
29 | 112 |
|
| 113 | + // get the infraredFrameDescription from the InfraredFrameSource |
| 114 | + FrameDescription infraredFrameDescription = this.kinectSensor.InfraredFrameSource.FrameDescription; |
| 115 | + |
| 116 | + // open the reader for the infrared frames |
| 117 | + this.infraredFrameReader = this.kinectSensor.InfraredFrameSource.OpenReader(); |
| 118 | + |
| 119 | + // wire handler for frame arrival |
| 120 | + this.infraredFrameReader.FrameArrived += this.Reader_InfraredFrameArrived; |
| 121 | + |
| 122 | + // allocate space to put the pixels being received and converted |
| 123 | + this.infraredFrameData = new ushort[infraredFrameDescription.Width * infraredFrameDescription.Height]; |
| 124 | + this.infraredPixels = new byte[infraredFrameDescription.Width * infraredFrameDescription.Height * BytesPerPixel]; |
| 125 | + |
| 126 | + // create the bitmap to display |
| 127 | + this.bitmap = new WriteableBitmap(infraredFrameDescription.Width, infraredFrameDescription.Height); |
| 128 | + |
| 129 | + this.CurrentFrameDescription = infraredFrameDescription; |
| 130 | + |
| 131 | + // set IsAvailableChanged event notifier |
| 132 | + this.kinectSensor.IsAvailableChanged += this.Sensor_IsAvailableChanged; |
| 133 | + |
| 134 | + // use the window object as the view model in this simple example |
| 135 | + this.DataContext = this; |
| 136 | + |
30 | 137 | // open the sensor |
31 | 138 | this.kinectSensor.Open(); |
32 | 139 |
|
33 | 140 | this.InitializeComponent(); |
34 | 141 | } |
| 142 | + |
| 143 | + private void Sensor_IsAvailableChanged(KinectSensor sender, IsAvailableChangedEventArgs args) |
| 144 | + { |
| 145 | + this.StatusText = this.kinectSensor.IsAvailable ? "Running" : "Not Available"; |
| 146 | + } |
| 147 | + |
| 148 | + private void Reader_InfraredFrameArrived(object sender, |
| 149 | + InfraredFrameArrivedEventArgs e) |
| 150 | + { |
| 151 | + bool infraredFrameProcessed = false; |
| 152 | + |
| 153 | + // InfraredFrame is IDisposable |
| 154 | + using (InfraredFrame infraredFrame = e.FrameReference.AcquireFrame()) |
| 155 | + { |
| 156 | + if (infraredFrame != null) |
| 157 | + { |
| 158 | + FrameDescription infraredFrameDescription = |
| 159 | + infraredFrame.FrameDescription; |
| 160 | + |
| 161 | + // verify data and write the new infrared frame data to the display bitmap |
| 162 | + if (((infraredFrameDescription.Width * infraredFrameDescription.Height) |
| 163 | + == this.infraredFrameData.Length) && |
| 164 | + (infraredFrameDescription.Width == this.bitmap.PixelWidth) && |
| 165 | + (infraredFrameDescription.Height == this.bitmap.PixelHeight)) |
| 166 | + { |
| 167 | + // Copy the pixel data from the image to a temporary array |
| 168 | + infraredFrame.CopyFrameDataToArray(this.infraredFrameData); |
| 169 | + |
| 170 | + infraredFrameProcessed = true; |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // we got a frame, convert and render |
| 176 | + if (infraredFrameProcessed) |
| 177 | + { |
| 178 | + ConvertInfraredDataToPixels(); |
| 179 | + RenderPixelArray(this.infraredPixels); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private void ConvertInfraredDataToPixels() |
| 184 | + { |
| 185 | + // Convert the infrared to RGB |
| 186 | + int colorPixelIndex = 0; |
| 187 | + for (int i = 0; i < this.infraredFrameData.Length; ++i) |
| 188 | + { |
| 189 | + // normalize the incoming infrared data (ushort) to a float ranging from |
| 190 | + // [InfraredOutputValueMinimum, InfraredOutputValueMaximum] by |
| 191 | + // 1. dividing the incoming value by the source maximum value |
| 192 | + float intensityRatio = (float)this.infraredFrameData[i] / InfraredSourceValueMaximum; |
| 193 | + |
| 194 | + // 2. dividing by the (average scene value * standard deviations) |
| 195 | + intensityRatio /= InfraredSceneValueAverage * InfraredSceneStandardDeviations; |
| 196 | + |
| 197 | + // 3. limiting the value to InfraredOutputValueMaximum |
| 198 | + intensityRatio = Math.Min(InfraredOutputValueMaximum, intensityRatio); |
| 199 | + |
| 200 | + // 4. limiting the lower value InfraredOutputValueMinimum |
| 201 | + intensityRatio = Math.Max(InfraredOutputValueMinimum, intensityRatio); |
| 202 | + |
| 203 | + // 5. converting the normalized value to a byte and using the result |
| 204 | + // as the RGB components required by the image |
| 205 | + byte intensity = (byte)(intensityRatio * 255.0f); |
| 206 | + this.infraredPixels[colorPixelIndex++] = intensity; //Blue |
| 207 | + this.infraredPixels[colorPixelIndex++] = intensity; //Green |
| 208 | + this.infraredPixels[colorPixelIndex++] = intensity; //Red |
| 209 | + this.infraredPixels[colorPixelIndex++] = 255; //Alpha |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + private void RenderPixelArray(byte[] pixels) |
| 214 | + { |
| 215 | + pixels.CopyTo(this.bitmap.PixelBuffer); |
| 216 | + this.bitmap.Invalidate(); |
| 217 | + FrameDisplayImage.Source = this.bitmap; |
| 218 | + } |
| 219 | + |
35 | 220 | } |
36 | 221 | } |
0 commit comments