一、kinect开发的一个流程图

1、我们可以知道一个简单的框架就是几部分

(1)选择使用的kinect传感器

KinectSensor.KinectSensors[]

(2)打开需要的数据流

_kinect.DepthStream.Enable();
_kinect.ColorStream.Enable();
_kinect.SkeletonStream.Enable();

(3)注册事件

其实就是主要的算法在这里体现。

有个小窍门:VS的CodeSnippet快速生成事件代码,如在代码“_kinect.DepthFrameReady+=”后面连续按两次“Tab”键,就会生成相应的时间并处理相应的代码。

二、初始化、启用kinect设备

代码如下,记得要声明一个私有成员变量_kinect,并在MainWindow()里面调用。

KinectSensor _kinect;

        private void startKinect()
{
if(KinectSensor.KinectSensors.Count>)
{
//选择第一个kinect设备
_kinect = KinectSensor.KinectSensors[];
MessageBox.Show("Kinect目前状态为:" + _kinect.Status); //初始化设定,启用彩色图像,深度图像和骨骼追踪
_kinect.ColorStream.Enable(ColorImageFormat.RgbResolution640x480Fps30);
_kinect.DepthStream.Enable(DepthImageFormat.Resolution640x480Fps30);
_kinect.SkeletonStream.Enable(); //注册时间,该方法将保证彩色图像,深度图像和骨骼图像的同步
_kinect.AllFramesReady += _kinect_AllFramesReady; //启动kinect
_kinect.Start();
}
else
{
MessageBox.Show("没有发现任何kinect设备");
}
}

void _kinect_AllFramesReady(object sender, AllFramesReadyEventArgs e)
{

}

三、彩色图像流数据处理

1、在MainWindows窗体上新增一个Image控件,命名为imageCamera

2、在_kinect_AllFramesReady事件处理中增加如下代码、

//显示彩色摄像头
using(ColorImageFrame colorFrame = e.OpenColorImageFrame())
{
if(colorFrame == null)
{
return;
} byte[] pixels = new byte[colorFrame.PixelDataLength];
colorFrame.CopyPixelDataTo(pixels); //BGR32格式图片一个像素为4个字节
int stride = colorFrame.Width * ;
ImageCamera.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, , , PixelFormats.Bgr32, null, pixels, stride); }

BGR32图像的1像素对应4字节(32位),分别是B,G,R,阿尔法通道(透明度)。

BitmapSource.Create是一个从数组到二维矩阵的过程。

Stride为图片步长,代表图片一行像素所占的字节数,为摄像头传输图片的宽度乘以4,

DPI,越高越清晰,普通的显示器就是96差不多。

3、效果图

四、深度数据捕获

1、定义深度图像的有效视角范围。

const float MaxDepthDistance = ;
const float MinDepthDistance = ;
const float MaxDepthDistanceOffest = MaxDepthDistance - MinDepthDistance; private const int RedIndex = ;
private const int GreenIndex = ;
private const int BlueIndex = ;

2、代码

private byte[]convertDepthFrameToColorFrame(DepthImageFrame depthFrame)
{
short[] rawDepthData = new short[depthFrame.PixelDataLength];
depthFrame.CopyPixelDataTo(rawDepthData); Byte[] piexls = new byte[depthFrame.Height * depthFrame.Width * ]; for(int depthIndex = ,colorIndex=;depthIndex<rawDepthData.Length&&colorIndex<piexls.Length;depthIndex++,colorIndex+=)
{
int player = rawDepthData[depthIndex] & DepthImageFrame.PlayerIndexBitmask; int depth = rawDepthData[depthIndex];
if(depth<=)
{
piexls[colorIndex + BlueIndex] = ;
piexls[colorIndex + GreenIndex] = ;
piexls[colorIndex + RedIndex] = ;
}
else if(depth>&&depth<)
{
piexls[colorIndex + BlueIndex] = ;
piexls[colorIndex + GreenIndex] = ;
piexls[colorIndex + RedIndex] = ;
}
else if(depth>)
{
piexls[colorIndex + BlueIndex] = ;
piexls[colorIndex + GreenIndex] = ;
piexls[colorIndex + RedIndex] = ;
} byte intensity = CalculateIntensityFromDepth(depth);
piexls[colorIndex + BlueIndex] = intensity;
piexls[colorIndex + GreenIndex] = intensity;
piexls[colorIndex + RedIndex] = intensity; if(player>)
{
piexls[colorIndex + BlueIndex] = Colors.LightGreen.B;
piexls[colorIndex + GreenIndex] = Colors.LightGreen.G;
piexls[colorIndex + RedIndex] = Colors.LightGreen.R;
}
}
return piexls;
} 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 * ;
ImageCamera.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, , , PixelFormats.Bgr32, null, pixels, stride); } using(DepthImageFrame depthFrame = e.OpenDepthImageFrame())
{
if(depthFrame==null)
{
return;
} byte[] piexls = convertDepthFrameToColorFrame(depthFrame); int stride = depthFrame.Width * ;
imageDepth.Source = BitmapSource.Create(depthFrame.Width, depthFrame.Height, , , PixelFormats.Bgr32, null, piexls, stride); }

3、效果图

五、骨骼追踪

1、首先注释掉我们之前注册的时间,重新注册一个彩色数据流时间和一个骨骼事件,然后添加如下代码

private Skeleton[] skeletons;
void _kinect_SkeletonFrameReady(object sender, SkeletonFrameReadyEventArgs e)
{
bool isSkeletonDataEeady = false;
using (SkeletonFrame skeletonFrame = e.OpenSkeletonFrame())
{
if(skeletonFrame!=null)
{
skeletons = new Skeleton[skeletonFrame.SkeletonArrayLength];
skeletonFrame.CopySkeletonDataTo(skeletons);
isSkeletonDataEeady = true;
} } if(isSkeletonDataEeady==true)
{
Skeleton currentSkeleton = (from s in skeletons where s.TrackingState == SkeletonTrackingState.Tracked select s).FirstOrDefault();
if(currentSkeleton !=null)
{
lockHeadWithSpot(currentSkeleton);
} }
} void lockHeadWithSpot(Skeleton s)
{
Joint head = s.Joints[JointType.Head];
ColorImagePoint colorPoint = _kinect.MapSkeletonPointToColor(head.Position, _kinect.ColorStream.Format); Point p = new Point((int)(ImageCamera.Width * colorPoint.X / _kinect.ColorStream.FrameWidth),
(int)(ImageCamera.Height * colorPoint.Y / _kinect.ColorStream.FrameHeight)); Canvas.SetLeft(ellipseHead, p.X);
Canvas.SetRight(ellipseHead, p.Y);
} void _kinect_ColorFrameReady(object sender, ColorImageFrameReadyEventArgs 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 * ;
ImageCamera.Source = BitmapSource.Create(colorFrame.Width, colorFrame.Height, , , PixelFormats.Bgr32, null, pixels, stride); } }

2、效果图

六关闭kinect设备

private void stopKinect()
{
if(_kinect != null)
{
if(_kinect.Status== KinectStatus.Connected)
{
_kinect.Stop();
}
}
}

kinect学习笔记(四)——各种数据流的更多相关文章

  1. Learning ROS for Robotics Programming Second Edition学习笔记(四) indigo devices

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  2. 零拷贝详解 Java NIO学习笔记四(零拷贝详解)

    转 https://blog.csdn.net/u013096088/article/details/79122671 Java NIO学习笔记四(零拷贝详解) 2018年01月21日 20:20:5 ...

  3. C#可扩展编程之MEF学习笔记(四):见证奇迹的时刻

    前面三篇讲了MEF的基础和基本到导入导出方法,下面就是见证MEF真正魅力所在的时刻.如果没有看过前面的文章,请到我的博客首页查看. 前面我们都是在一个项目中写了一个类来测试的,但实际开发中,我们往往要 ...

  4. IOS学习笔记(四)之UITextField和UITextView控件学习

    IOS学习笔记(四)之UITextField和UITextView控件学习(博客地址:http://blog.csdn.net/developer_jiangqq) Author:hmjiangqq ...

  5. java之jvm学习笔记四(安全管理器)

    java之jvm学习笔记四(安全管理器) 前面已经简述了java的安全模型的两个组成部分(类装载器,class文件校验器),接下来学习的是java安全模型的另外一个重要组成部分安全管理器. 安全管理器 ...

  6. Typescript 学习笔记四:回忆ES5 中的类

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  7. ES6学习笔记<四> default、rest、Multi-line Strings

    default 参数默认值 在实际开发 有时需要给一些参数默认值. 在ES6之前一般都这么处理参数默认值 function add(val_1,val_2){ val_1 = val_1 || 10; ...

  8. muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制

    目录 muduo网络库学习笔记(四) 通过eventfd实现的事件通知机制 eventfd的使用 eventfd系统函数 使用示例 EventLoop对eventfd的封装 工作时序 runInLoo ...

  9. python3.4学习笔记(四) 3.x和2.x的区别,持续更新

    python3.4学习笔记(四) 3.x和2.x的区别 在2.x中:print html,3.x中必须改成:print(html) import urllib2ImportError: No modu ...

  10. Go语言学习笔记四: 运算符

    Go语言学习笔记四: 运算符 这章知识好无聊呀,本来想跨过去,但没准有初学者要学,还是写写吧. 运算符种类 与你预期的一样,Go的特点就是啥都有,爱用哪个用哪个,所以市面上的运算符基本都有. 算术运算 ...

随机推荐

  1. 二分图最大匹配的K&#246;nig定理及其证明

     二分图最大匹配的K?nig定理及其证明 本文将是这一系列里最短的一篇,因为我只打算把K?nig定理证了,其它的废话一概没有.    以下五个问题我可能会在以后的文章里说,如果你现在很想知道的话,网上 ...

  2. E. Tetrahedron(数学推导)

    E. Tetrahedron 分类: AC路漫漫2013-08-08 16:07 465人阅读 评论(0) 收藏 举报 time limit per test 2 seconds memory lim ...

  3. storyboard和xib的各种问题

    1.prepareFoSegue注意问题使用该方法设置的值, 必须要 viewWillApear之后用 2.storayboard的使用autolayout, constant = -16, 刚好在f ...

  4. IOS APP的所有图标尺寸规范

    转自: http://blog.csdn.net/chonbj/article/details/25133247 像我一样记不住iOS应用图标像素尺寸的开发者不在少数,我经常需要查询不同设备上的应用尺 ...

  5. c++字符串详解(转)

    之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够.字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是 ...

  6. Nmap备忘单:从探索到漏洞利用(Part 4)

    这是我们的Nmap备忘单的第四部分(Part 1. Part 2. Part 3).本文中我们将讨论更多东西关于扫描防火墙,IDS / IPS 逃逸,Web服务器渗透测试等.在此之前,我们应该了解一下 ...

  7. HDOJ 1856

    #include<cstdio> #include<cstdlib> typedef struct ufse *ufset; struct ufse { ]; ]; }UFS; ...

  8. javascript memoization递归优化

    memoize优化递归 function createRec(callback, cache) { cache = cache || []; var rec = function(n) { (n in ...

  9. centos7 安装kvm, 并创建虚拟机

    # yum –y install qemu-kvm qemu-img bridge-utils # yum –y install libvirt virt-install virt-manager # ...

  10. SQL按照指定顺序对字段进行排序

    SqlServer按照指定顺序对字段进行排序 问题如下,在SqlServer有一个这样的SQL查询 SELECT *FROM ProductWHERE ID IN ( 12490, 12494, 12 ...