Skip to content

Commit

Permalink
lab04
Browse files Browse the repository at this point in the history
  • Loading branch information
jesse-nsquared committed Mar 16, 2015
1 parent 5f2afd6 commit c3532b1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Kinect2Sample/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
Click="InfraredButton_Click"/>
<Button Content="Color" Style="{StaticResource FrameSelectorButtonStyle}"
Click="ColorButton_Click"/>
<Button Content="Depth" Style="{StaticResource FrameSelectorButtonStyle}"
Click="DepthButton_Click"/>
</StackPanel>
</ScrollViewer>
</Grid>
Expand Down
94 changes: 89 additions & 5 deletions Kinect2Sample/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ namespace Kinect2Sample
public enum DisplayFrameType
{
Infrared,
Color
Color,
Depth
}

public sealed partial class MainPage : Page, INotifyPropertyChanged
Expand Down Expand Up @@ -80,6 +81,10 @@ public sealed partial class MainPage : Page, INotifyPropertyChanged
private ushort[] infraredFrameData = null;
private byte[] infraredPixels = null;

//Depth Frame
private ushort[] depthFrameData = null;
private byte[] depthPixels = null;

public event PropertyChangedEventHandler PropertyChanged;
public string StatusText
{
Expand Down Expand Up @@ -120,7 +125,7 @@ public MainPage()

SetupCurrentDisplay(DEFAULT_DISPLAYFRAMETYPE);

this.multiSourceFrameReader = this.kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Infrared | FrameSourceTypes.Color);
this.multiSourceFrameReader = this.kinectSensor.OpenMultiSourceFrameReader(FrameSourceTypes.Infrared | FrameSourceTypes.Color | FrameSourceTypes.Depth);

this.multiSourceFrameReader.MultiSourceFrameArrived += this.Reader_MultiSourceFrameArrived;

Expand Down Expand Up @@ -157,6 +162,15 @@ private void SetupCurrentDisplay(DisplayFrameType newDisplayFrameType)
this.bitmap = new WriteableBitmap(colorFrameDescription.Width, colorFrameDescription.Height);
break;

case DisplayFrameType.Depth:
FrameDescription depthFrameDescription = this.kinectSensor.DepthFrameSource.FrameDescription;
this.CurrentFrameDescription = depthFrameDescription;
// allocate space to put the pixels being received and converted
this.depthFrameData = new ushort[depthFrameDescription.Width * depthFrameDescription.Height];
this.depthPixels = new byte[depthFrameDescription.Width * depthFrameDescription.Height * BytesPerPixel];
this.bitmap = new WriteableBitmap(depthFrameDescription.Width, depthFrameDescription.Height);
break;

default:
break;
}
Expand Down Expand Up @@ -191,11 +205,76 @@ private void Reader_MultiSourceFrameArrived(MultiSourceFrameReader sender, Multi
ShowColorFrame(colorFrame);
}
break;
case DisplayFrameType.Depth:
using (DepthFrame depthFrame =
multiSourceFrame.DepthFrameReference.AcquireFrame())
{
ShowDepthFrame(depthFrame);
}
break;
default:
break;
}
}

private void ShowDepthFrame(DepthFrame depthFrame)
{
bool depthFrameProcessed = false;
ushort minDepth = 0;
ushort maxDepth = 0;

if (depthFrame != null)
{
FrameDescription depthFrameDescription = depthFrame.FrameDescription;

// verify data and write the new infrared frame data to the display bitmap
if (((depthFrameDescription.Width * depthFrameDescription.Height)
== this.infraredFrameData.Length) &&
(depthFrameDescription.Width == this.bitmap.PixelWidth) &&
(depthFrameDescription.Height == this.bitmap.PixelHeight))
{
// Copy the pixel data from the image to a temporary array
depthFrame.CopyFrameDataToArray(this.depthFrameData);

minDepth = depthFrame.DepthMinReliableDistance;
maxDepth = depthFrame.DepthMaxReliableDistance;
//maxDepth = 8000;

depthFrameProcessed = true;
}
}

// we got a frame, convert and render
if (depthFrameProcessed)
{
ConvertDepthDataToPixels(minDepth, maxDepth);
RenderPixelArray(this.depthPixels);
}
}

private void ConvertDepthDataToPixels(ushort minDepth, ushort maxDepth)
{
int colorPixelIndex = 0;
// Shape the depth to the range of a byte
int mapDepthToByte = maxDepth / 256;

for (int i = 0; i < this.depthFrameData.Length; ++i)
{
// Get the depth for this pixel
ushort depth = this.depthFrameData[i];

// To convert to a byte, we're mapping the depth value to the byte range.
// Values outside the reliable depth range are mapped to 0 (black).
byte intensity = (byte)(depth >= minDepth &&
depth <= maxDepth ? (depth / mapDepthToByte) : 0);

this.depthPixels[colorPixelIndex++] = intensity; //Blue
this.depthPixels[colorPixelIndex++] = intensity; //Green
this.depthPixels[colorPixelIndex++] = intensity; //Red
this.depthPixels[colorPixelIndex++] = 255; //Alpha
}
}

private void ShowColorFrame(ColorFrame colorFrame)
{
bool colorFrameProcessed = false;
Expand Down Expand Up @@ -236,7 +315,7 @@ private void ShowInfraredFrame(InfraredFrame infraredFrame)
FrameDescription infraredFrameDescription = infraredFrame.FrameDescription;

// verify data and write the new infrared frame data to the display bitmap
if (((infraredFrameDescription.Width * infraredFrameDescription.Height)
if (((infraredFrameDescription.Width * infraredFrameDescription.Height)
== this.infraredFrameData.Length) &&
(infraredFrameDescription.Width == this.bitmap.PixelWidth) &&
(infraredFrameDescription.Height == this.bitmap.PixelHeight))
Expand All @@ -251,8 +330,8 @@ private void ShowInfraredFrame(InfraredFrame infraredFrame)
// we got a frame, convert and render
if (infraredFrameProcessed)
{
ConvertInfraredDataToPixels();
RenderPixelArray(this.infraredPixels);
this.ConvertInfraredDataToPixels();
this.RenderPixelArray(this.infraredPixels);
}
}

Expand Down Expand Up @@ -303,5 +382,10 @@ private void ColorButton_Click(object sender, RoutedEventArgs e)
SetupCurrentDisplay(DisplayFrameType.Color);
}

private void DepthButton_Click(object sender, RoutedEventArgs e)
{
SetupCurrentDisplay(DisplayFrameType.Depth);
}

}
}

0 comments on commit c3532b1

Please sign in to comment.