我用的是OpenCV3
在代码里进行了坐标转换,把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();
}
运行图:
运行时的资源截图: