建立好WPF專案,首先需先加入Kinect的dll,在右邊方案總管-->參考 加入Microsoft.Research.Kinect.dll
專案前面用引用Kinect的dll
using Microsoft.Research.Kinect.Nui;
source code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
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.Research.Kinect.Nui;
namespace KinectSDKdepth
{
/// <summary>
/// MainWindow.xaml 的互動邏輯
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Runtime kinectNui = new Runtime();
private readonly byte[] _depthFrame32 = new byte[320 * 240 * 4];
private const int RedIdx = 2;
private const int GreenIdx = 1;
private const int BlueIdx = 0;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
kinectNui.Initialize(RuntimeOptions.UseDepthAndPlayerIndex);
}
catch (InvalidOperationException)
{
System.Windows.MessageBox.Show("Runtime initialization failed. Please make sure Kinect device is plugged in.");
return;
}
try
{
kinectNui.DepthStream.Open(ImageStreamType.Depth, 2, ImageResolution.Resolution320x240, ImageType.DepthAndPlayerIndex);
}
catch (InvalidOperationException)
{
System.Windows.MessageBox.Show("Failed to open stream. Please make sure to specify a supported image type and resolution.");
return;
}
kinectNui.DepthFrameReady += new EventHandler<ImageFrameReadyEventArgs>(kinectNui_DepthFrameReady);
}
void kinectNui_DepthFrameReady(object sender, ImageFrameReadyEventArgs e)
{
PlanarImage image = e.ImageFrame.Image;
byte[] convertedDepthFrame = ConvertDepthFrame(image.Bits);
depthImage.Source = BitmapSource.Create(
image.Width, image.Height, 96, 96, PixelFormats.Bgr32, null, convertedDepthFrame, image.Width * 4);
}
private byte[] ConvertDepthFrame(byte[] depthFrame16)
{
for (int i16 = 0, i32 = 0; i16 < depthFrame16.Length && i32 < _depthFrame32.Length; i16 += 2, i32 += 4)
{
//玩家的資料
int player = depthFrame16[i16] & 0x07;
//深度的資料
int realDepth = (depthFrame16[i16 + 1] << 5) | (depthFrame16[i16] >> 3);
// transform 13-bit depth information into an 8-bit intensity appropriate
// for display (we disregard information in most significant bit)
var intensity = (byte)(255 - (255 * realDepth / 0x0fff));
_depthFrame32[i32 + RedIdx] = 0;
_depthFrame32[i32 + GreenIdx] = 0;
_depthFrame32[i32 + BlueIdx] = 0;
// choose different display colors based on player
switch (player)
{
case 0:
_depthFrame32[i32 + RedIdx] = (byte)(intensity / 2);
_depthFrame32[i32 + GreenIdx] = (byte)(intensity / 2);
_depthFrame32[i32 + BlueIdx] = (byte)(intensity / 2);
break;
case 1:
_depthFrame32[i32 + RedIdx] = intensity;
break;
case 2:
_depthFrame32[i32 + GreenIdx] = intensity;
break;
case 3:
_depthFrame32[i32 + RedIdx] = (byte)(intensity / 4);
_depthFrame32[i32 + GreenIdx] = intensity;
_depthFrame32[i32 + BlueIdx] = intensity;
break;
case 4:
_depthFrame32[i32 + RedIdx] = intensity;
_depthFrame32[i32 + GreenIdx] = intensity;
_depthFrame32[i32 + BlueIdx] = (byte)(intensity / 4);
break;
case 5:
_depthFrame32[i32 + RedIdx] = intensity;
_depthFrame32[i32 + GreenIdx] = (byte)(intensity / 4);
_depthFrame32[i32 + BlueIdx] = intensity;
break;
case 6:
_depthFrame32[i32 + RedIdx] = (byte)(intensity / 2);
_depthFrame32[i32 + GreenIdx] = (byte)(intensity / 2);
_depthFrame32[i32 + BlueIdx] = intensity;
break;
case 7:
_depthFrame32[i32 + RedIdx] = (byte)(255 - intensity);
_depthFrame32[i32 + GreenIdx] = (byte)(255 - intensity);
_depthFrame32[i32 + BlueIdx] = (byte)(255 - intensity);
break;
}
}
return _depthFrame32;
}
}
}
留言列表