彩色图像流、深度数据和骨骼跟踪的集合处理(暂无骨骼跟踪)

整个编程模型

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
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
KinectSensor _kinect;

    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();
    }
}

}

效果如图:
请输入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值