配置环境:windows10+vs2015+opencv3.0
首先到windows官网下载kinect for windows sdk ,我用的是2.0版本,下载后安装即可。安装完成后,将Kinect V2连接到电脑USB3.0的口,我的电脑是左手边最下面的那个,然后打开设备管理器,查看kinect是否连接正常,如下图所示即为连接正常,如果有感叹号,就把这个驱动删了,然后拔掉Kinect,再连接到电脑上,电脑会再次自动安装驱动,如果还不行,就重启一下。
装好驱动后,打开刚刚安装好的SDK Browser(Kinect for windows)v2.0软件,找到如下图所示的选项,我只测试了其中有Run按键的几个,这几个点击Run之后,会出现一个黑色的窗口,这时候不要以为是坏的,当你在kinect摄像头前面动的时候,它才会显示东西,并且只显示人体骨骼。
现在我们算是把整个kinect都安装好了,接下来就配置系统了。
首先是安装好opencv,官网上下载解压即可,还有vs2015也是到windows官网下载安装,网上相应的安装教程特别多,找一下就好。
接下来是把opencv和kinect sdk for windows配置到vs2015里面。
1.新建环境变量,变量名:OPENCV,变量值:D:…\opencv\build,要修改为你自己的opencv安装路径,然后再环境变量Path里面,输入D:…\opencv\build\x86\vc12\bin,也是相应的你的opencv的安装路径。
2.打开vs2015,新建一个C++项目,然后在【解决方案资源管理器】中右键项目名,选择【属性】,在【C/C++】的【常规】里,【附加包含目录】中加入
【 (KINECTSDK20DIR)\inc】,在【链接器】的【常规】里,【附加库目录】中加入【 (KINECTSDK20_DIR)\Lib\x86】,在【链接器】的【输入】里,【附加依赖项】中加入【kinect20.lib】
上面做完之后应该就可以编译运行了,但是我发现写代码时不会对【Kinect.h】中出现的函数这些进行自动补全,而且语法检查时提示【Kinect.h】找不到,如果你也出现此问题,那就在【解决方案资源管理器】中的【头文件】这里右键添加【Kinect.h】,它位于【C:\Program Files\Microsoft SDKs\Kinect\v2.0_1409\inc】中。
参考于:http://www.cnblogs.com/xz816111/p/5184273.html
3.配置opencv,可参考这篇文章:http://blog.csdn.net/lanergaming/article/details/48689841
最后,也是我们最终的目的,识别人体骨骼,代码如下(声明:代码是我借鉴的复制的别人的,链接在后面):
#include
#include //opencv头文件
#include
#include
#include //Kinect头文件
using namespace std;
using namespace cv;
void draw(Mat & img, Joint & r_1, Joint & r_2, ICoordinateMapper * myMapper);
int main(void)
{
IKinectSensor * mySensor = nullptr;
GetDefaultKinectSensor(&mySensor);
mySensor->Open();
IColorFrameSource * myColorSource = nullptr;
mySensor->get_ColorFrameSource(&myColorSource);
IColorFrameReader * myColorReader = nullptr;
myColorSource->OpenReader(&myColorReader);
int colorHeight = 0, colorWidth = 0;
IFrameDescription * myDescription = nullptr;
myColorSource->get_FrameDescription(&myDescription);
myDescription->get_Height(&colorHeight);
myDescription->get_Width(&colorWidth);
IColorFrame * myColorFrame = nullptr;
Mat original(colorHeight, colorWidth, CV_8UC4);
//**********************以上为ColorFrame的读取前准备**************************
IBodyFrameSource * myBodySource = nullptr;
mySensor->get_BodyFrameSource(&myBodySource);
IBodyFrameReader * myBodyReader = nullptr;
myBodySource->OpenReader(&myBodyReader);
int myBodyCount = 0;
myBodySource->get_BodyCount(&myBodyCount);
IBodyFrame * myBodyFrame = nullptr;
ICoordinateMapper * myMapper = nullptr;
mySensor->get_CoordinateMapper(&myMapper);
//**********************以上为BodyFrame以及Mapper的准备***********************
while (1)
{
while (myColorReader->AcquireLatestFrame(&myColorFrame) != S_OK);
myColorFrame->CopyConvertedFrameDataToArray(colorHeight * colorWidth * 4, original.data, ColorImageFormat_Bgra);
Mat copy = original.clone(); //读取彩色图像并输出到矩阵
while (myBodyReader->AcquireLatestFrame(&myBodyFrame) != S_OK); //读取身体图像
IBody ** myBodyArr = new IBody *[myBodyCount]; //为存身体数据的数组做准备
for (int i = 0; i < myBodyCount; i++)
myBodyArr[i] = nullptr;
if (myBodyFrame->GetAndRefreshBodyData(myBodyCount, myBodyArr) == S_OK) //把身体数据输入数组
for (int i = 0; i < myBodyCount; i++)
{
BOOLEAN result = false;
if (myBodyArr[i]->get_IsTracked(&result) == S_OK && result) //先判断是否侦测到
{
Joint myJointArr[JointType_Count];
if (myBodyArr[i]->GetJoints(JointType_Count, myJointArr) == S_OK) //如果侦测到就把关节数据输入到数组并画图
{
draw(copy, myJointArr[JointType_Head], myJointArr[JointType_Neck], myMapper);
draw(copy, myJointArr[JointType_Neck], myJointArr[JointType_SpineShoulder], myMapper);
draw(copy, myJointArr[JointType_SpineShoulder], myJointArr[JointType_ShoulderLeft], myMapper);
draw(copy, myJointArr[JointType_SpineShoulder], myJointArr[JointType_SpineMid], myMapper);
draw(copy, myJointArr[JointType_SpineShoulder], myJointArr[JointType_ShoulderRight], myMapper);
draw(copy, myJointArr[JointType_ShoulderLeft], myJointArr[JointType_ElbowLeft], myMapper);
draw(copy, myJointArr[JointType_SpineMid], myJointArr[JointType_SpineBase], myMapper);
draw(copy, myJointArr[JointType_ShoulderRight], myJointArr[JointType_ElbowRight], myMapper);
draw(copy, myJointArr[JointType_ElbowLeft], myJointArr[JointType_WristLeft], myMapper);
draw(copy, myJointArr[JointType_SpineBase], myJointArr[JointType_HipLeft], myMapper);
draw(copy, myJointArr[JointType_SpineBase], myJointArr[JointType_HipRight], myMapper);
draw(copy, myJointArr[JointType_ElbowRight], myJointArr[JointType_WristRight], myMapper);
draw(copy, myJointArr[JointType_WristLeft], myJointArr[JointType_ThumbLeft], myMapper);
draw(copy, myJointArr[JointType_WristLeft], myJointArr[JointType_HandLeft], myMapper);
draw(copy, myJointArr[JointType_HipLeft], myJointArr[JointType_KneeLeft], myMapper);
draw(copy, myJointArr[JointType_HipRight], myJointArr[JointType_KneeRight], myMapper);
draw(copy, myJointArr[JointType_WristRight], myJointArr[JointType_ThumbRight], myMapper);
draw(copy, myJointArr[JointType_WristRight], myJointArr[JointType_HandRight], myMapper);
draw(copy, myJointArr[JointType_HandLeft], myJointArr[JointType_HandTipLeft], myMapper);
draw(copy, myJointArr[JointType_KneeLeft], myJointArr[JointType_FootLeft], myMapper);
draw(copy, myJointArr[JointType_KneeRight], myJointArr[JointType_FootRight], myMapper);
draw(copy, myJointArr[JointType_HandRight], myJointArr[JointType_HandTipRight], myMapper);
}
}
}
delete[]myBodyArr;
myBodyFrame->Release();
myColorFrame->Release();
imshow("TEST", copy);
if (waitKey(30) == VK_ESCAPE)
break;
}
myMapper->Release();
myDescription->Release();
myColorReader->Release();
myColorSource->Release();
myBodyReader->Release();
myBodySource->Release();
mySensor->Close();
mySensor->Release();
return 0;
}
void draw(Mat & img, Joint & r_1, Joint & r_2, ICoordinateMapper * myMapper)
{
//用两个关节点来做线段的两端,并且进行状态过滤
if (r_1.TrackingState == TrackingState_Tracked && r_2.TrackingState == TrackingState_Tracked)
{
ColorSpacePoint t_point; //要把关节点用的摄像机坐标下的点转换成彩色空间的点
Point p_1, p_2;
myMapper->MapCameraPointToColorSpace(r_1.Position, &t_point);
p_1.x = t_point.X;
p_1.y = t_point.Y;
myMapper->MapCameraPointToColorSpace(r_2.Position, &t_point);
p_2.x = t_point.X;
p_2.y = t_point.Y;
line(img, p_1, p_2, Vec3b(0, 255, 0), 5);
circle(img, p_1, 10, Vec3b(255, 0, 0), -1);
circle(img, p_2, 10, Vec3b(255, 0, 0), -1);
}
}
链接:http://blog.csdn.net/baolinq/article/details/52373574
至此,大功告成。