using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Kinect;
namespace 深度数据捕捉
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
KinectSensor _kinect;
const float MaxDepthDistance = 4095;
const float MinDepthDistance = 850;
const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDistance;
private const int RedIndex = 2;
private const int GreenIndex = 1;
private const int BlueIndex = 0;
public MainWindow()
{
InitializeComponent();
}
private static byte CalculateIntensityFromDepth(int distance)
{
return (byte)(255 - (255 * Math.Max(distance - MinDepthDistance, 0) / (MaxDepthDistanceOffset)));
}
private byte[] convertDepthFrameToColorFrame(DepthImageFrame depthFrame)
{
short[] rawDepthData = new short[depthFrame.PixelDataLength];
depthFrame.CopyPixelDataTo(rawDepthData);
byte[] pixels = new byte[depthFrame.Width * depthFrame.Height * 4];
for(int depthIndex=0,colorIndex=0;depthIndex<rawDepthData.Length && colorIndex<pixels.Length;depthIndex++,colorIndex+=4){
int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask;
int depth = rawDepthData[depthIndex] >> DepthImageFrame.PlayerIndexBitmaskWidth;
if (depth <= 900)
{
pixels[colorIndex + BlueIndex] = 66;
pixels[colorIndex + GreenIndex] = 0;
pixels[colorIndex+RedIndex]=66;
}
else if (depth > 900 && depth < 2000)
{
pixels[colorIndex + BlueIndex] = 0;
pixels[colorIndex + GreenIndex] = 255;
pixels[colorIndex + RedIndex] = 0;
}
else if (depth > 2000)
{
pixels[colorIndex + BlueIndex] = 0;
pixels[colorIndex + GreenIndex] = 0;
pixels[colorIndex + RedIndex] = 255;
}
byte intensity = CalculateIntensityFromDepth(depth);
pixels[colorIndex + BlueIndex] = intensity;
pixels[colorIndex + GreenIndex] = intensity;
pixels[colorIndex + RedIndex] = intensity;
if (player > 0)
{
pixels[colorIndex + BlueIndex] = Colors.LightGreen.B;
pixels[colorIndex + GreenIndex] = Colors.LightGreen.G;
pixels[colorIndex + RedIndex] = Colors.LightGreen.R;
}
}
return pixels;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if(KinectSensor.KinectSensors.Count>0){
_kinect=KinectSensor.KinectSensors[0];
_kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
_kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
_kinect.SkeletonStream.Enable();
_kinect.AllFramesReady+=_kinect_AllFramesReady;
_kinect.Start();
}
else{
Console.WriteLine("There's not Kinect!");
}
}
void _kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{
using (ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if (colorFrame != null)
{
byte[] pixels = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(pixels);
int stride = colorFrame.Width * 4;
imageCamera.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride);
}
}
using (DepthImageFrame depthFrame = e.OpenDepthImageFrame())
{
if (depthFrame != null)
{
byte[] pixelss = convertDepthFrameToColorFrame(depthFrame);
int strides = depthFrame.Width * 4;
imageDepth.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixelss, strides);
}
}
}
private void Window_UnLoaded(object sender, RoutedEventArgs e)
{
_kinect.Stop();
}
}
}
</Grid>
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用。你还可以使用@来通知其他用户。