WPF的.xaml代碼:編程
.xaml.cs代碼:
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 Kinect
{
/// segmentfault
const float MaxDepthDistance = 4095;//深度圖像最大視距 const float MinDepthDiatance = 850;//深度圖像最小視距 const float MaxDepthDistanceOffset = MaxDepthDistance - MinDepthDiatance; //BGR32圖像,紅色、綠色、藍色分別對應的偏移(32位,4字節中的第幾個) private const int RedIndex = 2; private const int GreenIndex = 1; private const int BlueIndex = 0; /// <summary> ///單色直方圖計算公式,返回256色灰階。顏色越黑則越遠,越白則越近 ///將MaxDepthDistanceOffset數值控制在0~255的範圍內 /// </summary> /// <param name="distance">深度值,有效範圍MinDepthDistance到MaxDepthDistance</param> /// <returns>256色灰階</returns> private static byte CalculateIntensityFromDepth(int distance) { return (byte)(255 - (255 * Math.Max(distance - MinDepthDiatance, 0) / (MaxDepthDistance))); } /// <summary> /// 生成BGR32格式的圖片字節數組 /// </summary> /// <param name="depthFrame"></param> /// <returns></returns> private byte[] convertDepthFrameToColorFrame(DepthImageFrame depthFrame) { //從深度圖像幀中獲取原始數據 short[] rawDepthData = new short[depthFrame.PixelDataLength]; depthFrame.CopyPixelDataTo(rawDepthData); //建立Height X Width X 4 的RGB數組(Red, Green, Blue, empty byte) Byte[] pixels = new byte[depthFrame.Height * depthFrame.Width * 4]; //Bgr32 - Blue, Green, Red, empty byte //Bgra32 - Blue, Green, Red, transparency //須要爲Bgra32設置透明度(transparency),.NET默認設置該字節爲0(全透明) for(int depthIndex = 0,colorIndex = 0;depthIndex < rawDepthData.Length && colorIndex<pixels.Length;depthIndex++,colorIndex += 4) { //用戶分割, 0表明該像素不屬於用戶身體,低於3位字節表示被追蹤的使用者的索引編號 int player = rawDepthData[depthIndex & DepthImageFrame.PlayerIndexBitmask]; //得到深度數值,高13位字節 int depth = rawDepthData[depthIndex]>>DepthImageFrame.PlayerIndexBitmaskWidth; //0.9米內 if(depth <= 900) { //離Kinect很近 pixels[colorIndex + BlueIndex] = 255; pixels[colorIndex + GreenIndex] = 0; pixels[colorIndex + RedIndex] = 0; } //0.9~2米之間 else if(depth > 900 && depth < 2000) { pixels[colorIndex + BlueIndex] = 0; pixels[colorIndex + GreenIndex] = 255; pixels[colorIndex + RedIndex] = 0; } // 2米+ else if(depth > 2000) { //離Kinect超過2米 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; } /// <summary> /// 啓動Kinect設備,初始化選項,並註冊AllFrameReady同步事件 /// </summary> private void startKinect() { if(KinectSensor.KinectSensors.Count > 0) { //選擇第一個Kinect設備 _kinect = KinectSensor.KinectSensors[0]; MessageBox.Show("Kinect目前狀態爲:" + _kinect.Status); //初始化設定,啓用彩色圖像、深度圖像和骨骼跟蹤 _kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30); _kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30); _kinect.SkeletonStream.Enable(); //註冊事件,該方法將保證彩色圖像、深度圖像和骨骼數據的同步 _kinect.AllFramesReady += new EventHandler<AllFramesReadyEventArgs>(_kinect_AllFramesReady); //啓動Kinect設備 _kinect.Start(); } else { MessageBox.Show("沒有發現任何Kinect設備"); } } private void _kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e) { //顯示彩色攝像頭 using (ColorImageFrame colorFrame = e.OpenColorImageFrame()) { if (colorFrame == null) { return; } byte[] pixels = new byte[colorFrame.PixelDataLength]; colorFrame.CopyPixelDataTo(pixels); //BGR32格式圖片一個像素爲4個字節 int stride = colorFrame.Width * 4; imageCanera.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride); } //紅外攝像頭 using (DepthImageFrame depthFrame = e.OpenDepthImageFrame()) { if(depthFrame ==null) { return; } byte[] pixels = convertDepthFrameToColorFrame(depthFrame); //BGR圖片的步長 int stride = depthFrame.Width * 4; //建立BRG32格式的圖片 imageDepth.Source = BitmapSource.Create(depthFrame.Width,depthFrame.Height, 96, 96, PixelFormats.Bgr32, null, pixels, stride); } } public MainWindow() { InitializeComponent(); } //窗體啓動事件 private void Window_Loaded(object sender, RoutedEventArgs e) { startKinect(); } }
}數組
效果如圖:
ide