num 1:深度流
代码如下:
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 HelloKinectzzzzz//深度图像原始灰度图。彩色深度图
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
KinectSensor kinectSensor;//定义一个设备
private short[] pixelData;//定义像素数
byte[] depthFrame32;
private const int RedIndex = 2 ;//索引每个像素的rgb值
private const int GreenIndex = 1;
private const int BlueIndex = 0;
public MainWindow()//?
{
InitializeComponent();//初始化
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
kinectSensor = (from sensor in KinectSensor.KinectSensors//寻找设备
where sensor.Status == KinectStatus.Connected
select sensor).FirstOrDefault();
kinectSensor.DepthStream.Enable(DepthImageFormat.Resolution320x240Fps30);//启动深度流
kinectSensor.Start();//启动设备
kinectSensor.DepthFrameReady += kinectSensor_DepthFrameReady;//调用函数
}
//获取深度数据,并打印到控制台上
private void kinectSensor_DepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
{
using (DepthImageFrame depthImageFrame = e.OpenDepthImageFrame())
{
if (depthImageFrame != null)
{
pixelData = new short[depthImageFrame.PixelDataLength];//深度数据数组,长度由深度数据的长度决定
depthImageFrame.CopyPixelDataTo(pixelData);
this.depthFrame32 = new byte[depthImageFrame.Width * depthImageFrame.Height * 4];
this.depthFrame32 = ConvertDepthFrame(pixelData, ((KinectSensor)sender).DepthStream);
this.DepthImage.Source=BitmapSource.Create(depthImageFrame.Width,
depthImageFrame.Height,96,96,PixelFormats.Bgr32,null,depthFrame32,
depthImageFrame.Width * 4);
}
}
}
private byte[] ConvertDepthFrame(short[] depthFrame, DepthImageStream depthStream)//convertdepthframe函数
{
int tooNearDepth = depthStream.TooNearDepth;//距离kinect太近的最大深度值
int tooFarDepth = depthStream.TooFarDepth;//距离太远的最小深度值
int unknowDepth = depthStream.UnknownDepth;//获取不到深度值时返回的值
for (int i = 0, j = 0; i < depthFrame.Length && j < this.depthFrame32.Length; i++, j += 4)
{
int player = depthFrame[i] & DepthImageFrame.PlayerIndexBitmask;
int realDepth = depthFrame[i] >> DepthImageFrame.PlayerIndexBitmaskWidth;
//根据深度值的不同着以不同的颜色
if (player == 0 && realDepth == 0)
{
//白色
this.depthFrame32[j + RedIndex] = 255;
this.depthFrame32[j + GreenIndex] = 255;
this.depthFrame32[j + BlueIndex] = 255;
}
else if (player == 0 && realDepth > 0 && realDepth <= tooNearDepth)
{
//青色
this.depthFrame32[j + RedIndex] = 0;
this.depthFrame32[j + RedIndex] = 255;
this.depthFrame32[j + RedIndex] = 255;
}
else if (player == 0 && realDepth > tooNearDepth && realDepth < tooFarDepth)
{
this.depthFrame32[j + RedIndex] = 160;
this.depthFrame32[j + RedIndex] = 32;
this.depthFrame32[j + RedIndex] = 240;
}
else if (player == 0 && realDepth >= tooFarDepth)
{
//灰色
this.depthFrame32[j + RedIndex] = 192;
this.depthFrame32[j + RedIndex] = 192;
this.depthFrame32[j + RedIndex] = 192;
}
else if (player == 0 && realDepth == unknowDepth)
{
//黄色
this.depthFrame32[j + RedIndex] = 255;
this.depthFrame32[j + RedIndex] = 255;
this.depthFrame32[j + RedIndex] = 0;
}
else if (player > 0)
{
switch (player)
{
case 1://红色
this.depthFrame32[j + RedIndex] = 255;
this.depthFrame32[j + RedIndex] = 0;
this.depthFrame32[j + RedIndex] = 0;
break;
case 2://绿色
this.depthFrame32[j + RedIndex] =0;
this.depthFrame32[j + RedIndex] = 255;
this.depthFrame32[j + RedIndex] =0;
break;
default:
this.depthFrame32[j + RedIndex] = 0;
this.depthFrame32[j + RedIndex] = 0;
this.depthFrame32[j + RedIndex] = 255;
break;
}
}
}
return this.depthFrame32;
}
}
}
深度图像既可以是原始的灰度图,也可以转化为彩色深度图。
num 2:彩色流
代码:
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 WpfApplication1gtrsyhfmuujujjj//彩色图与红外图
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
KinectSensor kinectSensor;//定义一个设备
private byte[] pixelData;//像素存放数组
public MainWindow()
{
InitializeComponent();//初始化
}
private void window_Loaded(object sender, RoutedEventArgs e)
{
kinectSensor = (from sensor in KinectSensor.KinectSensors where sensor.Status == //寻找设备
KinectStatus.Connected select sensor).FirstOrDefault();
kinectSensor.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);//修改即可得到红外图//启动
kinectSensor.Start();
kinectSensor.ColorFrameReady += kinectSensor_ColorFrameReady;//调用函数
}
private void window_Unloaded(object sender, RoutedEventArgs e)
{
kinectSensor.Stop();//停止设备
}
private void kinectSensor_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)//e是一个参数,前面是一个参数名称
{
using (ColorImageFrame imageFrame=e.OpenColorImageFrame())//调用方法
{
if (imageFrame != null)
{
this.pixelData = new byte[imageFrame.PixelDataLength];//定义数组
imageFrame.CopyPixelDataTo(this.pixelData);//将数据复制到数组中
this.ColorImage.Source = BitmapSource.Create(imageFrame.Width, imageFrame.Height, 96, 96,
PixelFormats.Bgr32, null, pixelData, imageFrame.Width * imageFrame.BytesPerPixel);//创建bit。。。
}
}
}
}
}
彩色图只要修改部分代码就可以转化为红外图,第一个地方是彩色流启动位置,第二个是代码最后创建bitmapsource位置。
num 3:骨骼流
代码:暂未学会