Skip to content

Commit c6440c1

Browse files
lab02
1 parent eff7c3e commit c6440c1

File tree

2 files changed

+206
-2
lines changed

2 files changed

+206
-2
lines changed

Kinect2Sample/MainPage.xaml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,25 @@
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
mc:Ignorable="d">
99

10-
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
10+
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
11+
<Grid Margin="30">
12+
<Grid.RowDefinitions>
13+
<RowDefinition Height="70"/>
14+
<RowDefinition Height="*"/>
15+
<RowDefinition Height="100"/>
16+
</Grid.RowDefinitions>
17+
<TextBlock Grid.Row="0" Style="{StaticResource SubheaderTextBlockStyle}"
18+
Text="Kinect For Windows 2"/>
19+
<StackPanel Grid.Row="0" Orientation="Horizontal"
20+
HorizontalAlignment="Right" VerticalAlignment="Bottom">
21+
<TextBlock Text="Kinect Status: "/>
22+
<TextBlock Text="{Binding StatusText}"/>
23+
<TextBlock Text=", FrameWidth = "/>
24+
<TextBlock Text="{Binding CurrentFrameDescription.Width}"/>
25+
<TextBlock Text=", FrameHeight = "/>
26+
<TextBlock Text="{Binding CurrentFrameDescription.Height}"/>
27+
</StackPanel>
28+
<Image x:Name="FrameDisplayImage" Grid.Row="1" Stretch="Uniform"/>
29+
</Grid>
1130
</Grid>
1231
</Page>

Kinect2Sample/MainPage.xaml.cs

Lines changed: 186 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,211 @@
1111
using Windows.UI.Xaml.Data;
1212
using Windows.UI.Xaml.Input;
1313
using Windows.UI.Xaml.Media;
14+
using Windows.UI.Xaml.Media.Imaging;
1415
using Windows.UI.Xaml.Navigation;
1516
using WindowsPreview.Kinect;
17+
using System.ComponentModel;
1618

1719
namespace Kinect2Sample
1820
{
1921

20-
public sealed partial class MainPage : Page
22+
public sealed partial class MainPage : Page, INotifyPropertyChanged
2123
{
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;
2264

2365
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+
}
24107

25108
public MainPage()
26109
{
27110
// one sensor is currently supported
28111
this.kinectSensor = KinectSensor.GetDefault();
29112

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+
30137
// open the sensor
31138
this.kinectSensor.Open();
32139

33140
this.InitializeComponent();
34141
}
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+
35220
}
36221
}

0 commit comments

Comments
 (0)