Kinect2.0+OpenCV获取骨骼图
2015-10-05 00:34
513 查看
我用的是OpenCV3
在代码里进行了坐标转换,把CameraSpace转到DepthSpace。
然后用和示例代码(D2D的)里一样的方式进行绘制,但是由于Opencv里自带的显示图像的窗口默认以BGR3通道显示图像,Alpha值无法表示出来,所以,手势的半透明效果无法展示。最重要的一点,这程序运行起来的CPU占用率居然连1% 都不到,D2D那个要20%+。
运行时的资源截图:
在代码里进行了坐标转换,把CameraSpace转到DepthSpace。
然后用和示例代码(D2D的)里一样的方式进行绘制,但是由于Opencv里自带的显示图像的窗口默认以BGR3通道显示图像,Alpha值无法表示出来,所以,手势的半透明效果无法展示。最重要的一点,这程序运行起来的CPU占用率居然连1% 都不到,D2D那个要20%+。
#include "opencv2/core.hpp" #include "opencv2/imgproc.hpp" #include "opencv2/highgui.hpp" #include "opencv2/videoio.hpp" #include <iostream> #include <Kinect.h> #pragma comment ( lib, "kinect20.lib" ) using namespace cv; using namespace std; template<class Interface> inline void SafeRelease(Interface *& pInterfaceToRelease) { if (pInterfaceToRelease != NULL) { pInterfaceToRelease->Release(); pInterfaceToRelease = NULL; } } void DrawBone(JointType b, JointType c, ICoordinateMapper*coordinatemapper,Joint joint[], Mat&a) { DepthSpacePoint d1, d2; coordinatemapper->MapCameraPointToDepthSpace(joint[b].Position, &d1); coordinatemapper->MapCameraPointToDepthSpace(joint[c].Position, &d2); if(d1.X>0&&d1.X<512&&d1.Y>0&&d1.Y<424&& d2.X>0 && d2.X<512 && d2.Y>0 && d2.Y<424) line(a, Point(d1.X, d1.Y), Point(d2.X, d2.Y), Scalar(0, 255, 0, 255), 3); else line(a, Point(d1.X, d1.Y), Point(d2.X, d2.Y), Scalar(255, 255, 255, 255), 1); circle(a, Point(d1.X, d1.Y), 2, Scalar(255, 255, 255, 255), 4); circle(a, Point(d2.X, d2.Y), 2, Scalar(255, 255, 255, 255), 4); } int main() { HRESULT hResult = S_OK; IKinectSensor *kinect; GetDefaultKinectSensor(&kinect); kinect->Open(); IBodyFrameSource*bodysource; kinect->get_BodyFrameSource(&bodysource); IBodyFrameReader*bodyreader; bodysource->OpenReader(&bodyreader); ICoordinateMapper* coordinatemapper; kinect->get_CoordinateMapper(&coordinatemapper); while (1) { Mat asd(424, 512, CV_8UC4); IBodyFrame* bodyframe = nullptr; hResult = bodyreader->AcquireLatestFrame(&bodyframe); if (SUCCEEDED(hResult)) { IBody* body[BODY_COUNT] = { 0 }; hResult = bodyframe->GetAndRefreshBodyData(BODY_COUNT, body); if (SUCCEEDED(hResult)) { for (int i = 0; i < BODY_COUNT; i++) { BOOLEAN tracked = false; hResult = body[i]->get_IsTracked(&tracked); if (SUCCEEDED(hResult) && tracked) { Joint joint[JointType_Count]; hResult = body[i]->GetJoints(JointType_Count, joint); DepthSpacePoint depthspacepoint; if (SUCCEEDED(hResult)) { // Torso DrawBone(JointType_Head, JointType_Neck,coordinatemapper,joint,asd); DrawBone(JointType_Neck, JointType_SpineShoulder, coordinatemapper, joint, asd); DrawBone(JointType_SpineShoulder, JointType_SpineMid, coordinatemapper, joint, asd); DrawBone(JointType_SpineMid, JointType_SpineBase, coordinatemapper, joint, asd); DrawBone(JointType_SpineShoulder, JointType_ShoulderRight, coordinatemapper, joint, asd); DrawBone(JointType_SpineShoulder, JointType_ShoulderLeft, coordinatemapper, joint, asd); DrawBone(JointType_SpineBase, JointType_HipRight, coordinatemapper, joint, asd); DrawBone(JointType_SpineBase, JointType_HipLeft, coordinatemapper, joint, asd); // Right Arm DrawBone(JointType_ShoulderRight, JointType_ElbowRight, coordinatemapper, joint, asd); DrawBone(JointType_ElbowRight, JointType_WristRight, coordinatemapper, joint, asd); DrawBone(JointType_WristRight, JointType_HandRight, coordinatemapper, joint, asd); DrawBone(JointType_HandRight, JointType_HandTipRight, coordinatemapper, joint, asd); DrawBone(JointType_WristRight, JointType_ThumbRight, coordinatemapper, joint, asd); // Left Arm DrawBone( JointType_ShoulderLeft, JointType_ElbowLeft, coordinatemapper, joint, asd); DrawBone(JointType_ElbowLeft, JointType_WristLeft, coordinatemapper, joint, asd); DrawBone(JointType_WristLeft, JointType_HandLeft, coordinatemapper, joint, asd); DrawBone(JointType_HandLeft, JointType_HandTipLeft, coordinatemapper, joint, asd); DrawBone(JointType_WristLeft, JointType_ThumbLeft, coordinatemapper, joint, asd); // Right Leg DrawBone(JointType_HipRight, JointType_KneeRight, coordinatemapper, joint, asd); DrawBone(JointType_KneeRight, JointType_AnkleRight, coordinatemapper, joint, asd); DrawBone( JointType_AnkleRight, JointType_FootRight, coordinatemapper, joint, asd); // Left Leg DrawBone( JointType_HipLeft, JointType_KneeLeft, coordinatemapper, joint, asd); DrawBone( JointType_KneeLeft, JointType_AnkleLeft, coordinatemapper, joint, asd); DrawBone( JointType_AnkleLeft, JointType_FootLeft, coordinatemapper, joint, asd); DepthSpacePoint d1, d2; coordinatemapper->MapCameraPointToDepthSpace(joint[JointType_HandLeft].Position, &d1); coordinatemapper->MapCameraPointToDepthSpace(joint[JointType_HandRight].Position, &d2); HandState left; body[i]->get_HandLeftState(&left); HandState right; body[i]->get_HandRightState(&right); switch (left) { case HandState_Closed: circle(asd,Point(d1.X,d1.Y),10,Scalar(0,0,255,1),20); break; case HandState_Open: circle(asd, Point(d1.X, d1.Y), 10, Scalar(0, 255, 0, 1), 20); break; case HandState_Lasso: circle(asd, Point(d1.X, d1.Y), 10, Scalar(255, 0, 0, 1), 20); break; default: break; } switch (right) { case HandState_Closed: circle(asd, Point(d2.X, d2.Y), 10, Scalar(0, 0, 255, 1), 20); break; case HandState_Open: circle(asd, Point(d2.X, d2.Y), 10, Scalar(0, 255, 0, 1), 20); break; case HandState_Lasso: circle(asd, Point(d2.X, d2.Y), 10, Scalar(255, 0, 0, 1), 20); break; default: break; } } } } } for (int count = 0; count < BODY_COUNT; count++) { SafeRelease(body[count]); } } SafeRelease(bodyframe); imshow("aaaaaaa", asd); if (waitKey(33) == VK_ESCAPE) { break; } } SafeRelease(bodysource); SafeRelease(bodyreader); SafeRelease(coordinatemapper); if (kinect) { kinect->Close(); } SafeRelease(kinect); destroyAllWindows(); } 运行图:
运行时的资源截图:
相关文章推荐
- Linux系统结构 详解
- 维护关联关系的具体含义
- 安装Docker
- 下载Docker镜像
- 制作Docker镜像
- 监控服务器:设置用户登录邮件提醒
- linux安装weblogic
- Centos下配置用户sudo权限
- Linux入门的一些快捷键
- OOP—ECMAScript实现详解
- Linux FSCK自动修复文件系统
- LINUX下 ZeroMQ安装实例
- 正确配置 在controller中使用AOP
- CDN对流媒体和应用分发的支持及优化 | 高可用CDN架构详解
- 如何在 Linux 上使用 x2go 设置远程桌面
- 用nginx图片缓存服务器
- java 学习写架构必会几大技术点
- Wps for linux 安…
- tomcat6配置
- FlashLogoPCN