Kinect For Windows V2开发日志六:人体的轮廓的表示
> Kinect中带了一种数据源,叫做`BodyIndex`,简单来说就是它利用深度摄像头识别出最多6个人体,并且用数据将属于人体的部分标记,将人体和背景区别开来。利用这一特性,就可以在环境中显示出人体的轮廓而略去背景的细节。我采用了下面两种方式来实现。
用OpenCV表示
代码
#include <iostream>
#include <Kinect.h>
#include <opencv2\highgui.hpp>
using namespace std;
using namespace cv;
int main(void)
{
IKinectSensor * mySensor = nullptr; //Sensor
GetDefaultKinectSensor(&mySensor);
mySensor->Open();
IBodyIndexFrameSource * mySource = nullptr; //Source
mySensor->get_BodyIndexFrameSource(&mySource);
int height = 0, width = 0;
IFrameDescription * myDescription = nullptr;
mySource->get_FrameDescription(&myDescription);
myDescription->get_Height(&height);
myDescription->get_Width(&width);
IBodyIndexFrameReader * myReader = nullptr; //Reader
mySource->OpenReader(&myReader);
IBodyIndexFrame * myFrame = nullptr; //Frame
Mat img(height,width,CV_8UC3);
Vec3b color[7] = { Vec3b(0,0,255),Vec3b(0,255,255),Vec3b(255,255,255),Vec3b(0,255,0),Vec3b(255,0,0),Vec3b(255,0,255),Vec3b(0,0,0) };
while (1)
{
if (myReader->AcquireLatestFrame(&myFrame) == S_OK)
{
UINT size = 0;
BYTE * buffer = nullptr;
myFrame->AccessUnderlyingBuffer(&size,&buffer);
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
int index = buffer[i * width + j]; //0-5代表人体,其它值代表背景,用此将人体和背景渲染成不同颜色
if (index <= 5)
img.at<Vec3b>(i, j) = color[index];
else
img.at<Vec3b>(i, j) = color[6];
}
imshow("TEST",img);
myFrame->Release();
}
if (waitKey(30) == VK_ESCAPE)
break;
}
myReader->Release();
myDescription->Release();
mySource->Release();
mySensor->Close();
mySensor->Release();
return 0;
}
详细说明
步骤和前面相似,不再赘述,关键在于对数据的处理。IBodyIndexFrame 里的数据分两种,值在0-5之间的点代表的是人体(因此最多识别出6个人),大于5的值代表的是背景。所以要显示人体时,只要简单的把代表人体的点渲染成一种颜色,背景渲染成另外一种颜色就可以了。值得注意的是在写颜色表color时,要用Vec3b把数据强转一下,不然会有问题。
最终的效果就是这样:

直接用数据勾画出人体
代码:
#include <iostream>
#include <Kinect.h>
#include <Windows.h>
using namespace std;
using namespace cv;
int main(void)
{
IKinectSensor * mySensor = nullptr; //Sensor
GetDefaultKinectSensor(&mySensor);
mySensor->Open();
IBodyIndexFrameSource * mySource = nullptr; //Source
mySensor->get_BodyIndexFrameSource(&mySource);
int height = 0, width = 0;
IFrameDescription * myDescription = nullptr;
mySource->get_FrameDescription(&myDescription);
myDescription->get_Height(&height);
myDescription->get_Width(&width);
IBodyIndexFrameReader * myReader = nullptr; //Reader
mySource->OpenReader(&myReader);
IBodyIndexFrame * myFrame = nullptr; //Frame
while (1)
{
Sleep(1000);
if (myReader->AcquireLatestFrame(&myFrame) == S_OK)
{
UINT size = 0;
BYTE * buffer = nullptr;
myFrame->AccessUnderlyingBuffer(&size,&buffer);
for (int i = 50; i < 350; i++) //调出一个合适的尺寸
{
for (int j = 0; j < width; j++)
{
int index = buffer[i * width + j];
if (index <= 5)
cout << 0;
else
cout << 1;
}
cout << endl;
}
cout << endl << endl;
myFrame->Release();
}
}
myReader->Release();
myDescription->Release();
mySource->Release();
mySensor->Close();
mySensor->Release();
return 0;
}
说明
实际上,因为有可以用数字来区别人体和背景这一特性,所以甚至可以不用openCV,直接用数据来显示人体。将识别为人体的数据作为0输出,背景作为1输出,同时把控制台的窗口调大一些,字体调到最小,每秒钟输出一帧,就能直接看到数据画出的图。真是有趣。
效果如下:

Kinect For Windows V2开发日志六:人体的轮廓的表示的更多相关文章
- Kinect For Windows V2开发日志七:照片合成与背景消除
上一篇里讲到了Kinect可以从环境中区分出人体来.因此可以利用这个功能,来把摄像头前的人合成进照片里,和利用Photoshop不同的是,这样合成进去的人是动态且实时的. 简单的思路 BodyInde ...
- Kinect For Windows V2开发日志八:侦测、追踪人体骨架
简介 Kinect一个很强大的功能就是它可以侦测到人体的骨骼信息并追踪,在Kinect V2的SDK 2.0中,它最多可以同时获取到6个人.每个人25个关节点的信息,并且通过深度摄像头,可以同时获取到 ...
- Kinect For Windows V2开发日志九:侦测并绘制人体骨架
简介 在上一篇<侦测.追踪人体骨架>里,介绍了关节点的使用办法,这一篇记录将关节点与OpenCV结合的绘图方法. 代码 #include <iostream> #include ...
- Kinect For Windows V2开发日志一:开发环境的配置
算是正式进军Kinect了,前段时间学的东西现在就忘了,于是从此开始记录一下. 目前为止大部分的学习资料来自于Heresy的博客,写的非常优秀,清晰明了,十分感谢.开发语言为C++,应该会一直使用,但 ...
- Kinect For Windows V2开发日志五:使用OpenCV显示彩色图像及红外图像
彩色图像 #include <iostream> #include <Kinect.h> #include <opencv2\highgui.hpp> using ...
- Kinect For Windows V2开发日志二:Kinect V2的基本参数
以下内容节选自Heresy的博客: 彩色影像:1920 x 1080 @ 30 / 15 FPS(根据环境亮度) 深度影像:512 x 424 @ 30 FPS.16bit 距离值(mm).可侦测 ...
- Kinect For Windows V2开发日志四:使用OpenCV显示深度图像
代码示例: #include <Kinect.h> #include <iostream> #include <opencv2\highgui.hpp> using ...
- Kinect For Windows V2开发日志三:简单的深度读取
代码示例: #include <Kinect.h> #include <iostream> using namespace std; int main(void) { IKin ...
- Kinect for Windows V2开发教程
教程 https://blog.csdn.net/openbug/article/details/80921437 Windows版Kinect SDK https://docs.microsoft. ...
随机推荐
- git免登录-ssh-key
1.生成ssh key公钥与私钥 ssh-keygen -t rsa -C "youname@example.com" 需输入三个内容:第一个,生成公私钥的路径及名称:后两个输入回 ...
- SetWindowsHookEx 相关
SetWindowsHookEx function https://msdn.microsoft.com/en-us/library/windows/desktop/ms644990(v=vs.85) ...
- 【M23】考虑使用其他程序库
1.程序库的设计是一种权衡的结果.体积小,速度快往往不能移植.可移植,通用的程序库往往意味着冗余和效率低下. 2.因此,选择程序库的时候,需要进行取舍.比如:iostream和stdio.iostre ...
- hdu 5273 Dylans loves sequence 逆序数简单递推
Dylans loves sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem ...
- 一步步学Mybatis-怎么样实现动态SQL查询(6)
上一章我们已经讲完了关于Mybatis的分页用法,其实MyBatis 还具有的一个强大的特性之一通常是它的动态 SQL 能力. 如果你有使用 JDBC 或其他 相似框架的经验,你就明白要动态的串联 S ...
- dsPIC33EP timer1 初始化设置及应用
//文件 p33timer1.h #ifndef _P33TIMER1_H_ #define _P33TIMER1_H_ //#include "p33timer1.h" #def ...
- 2012 East Central Regional Contest 解题报告
昨晚各种莫名其妙卡题. 不过细看这套题还挺简单的.全是各种暴力. 除了最后一道题计算几何看起来很麻烦的样子,其他题都是很好写的吧. A. Babs' Box Boutique 题目大意是给出不超过10 ...
- [Angular 2] Generate Angular 2 Components Programmatically with entryComponents & ViewContainerRef
You can generate Angular 2 components using a combination of ViewContainer and ComponentFactory, but ...
- MySql 日期格式化函数date_format()
mysql> select date_format(now(),'%Y'); +-------------------------+ | date_format(now(),'%Y') | +- ...
- cocos2d-x jsbinding 资源下载实现
cocos2dx没有直接给出资源下载的api,可能是因为资源的管理每个项目的需求不太一样,所以完整的资源下载功能需要我们自己去实现. 资源下载分为两部分,一部分是资源请求,另一部分是资源文件写入.资源 ...