#include <cv.h>
#include <highgui.h>
#include <Kinect.h>
#include<WinUser.h>
#pragma comment ( lib, "kinect20.lib" )
# pragma comment(lib,"user32.lib" )
using namespace std;
using namespace cv;
//释放接口需要自己定义
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease)
{
if (pInterfaceToRelease != NULL){
pInterfaceToRelease->Release();
pInterfaceToRelease = NULL;
}
}
void DrawBone(Mat& SkeletonImage, CvPoint pointSet[], const Joint* pJoints, int whichone, JointType joint0, JointType joint1);
void drawSkeleton(Mat& SkeletonImage, CvPoint pointSet[], const Joint* pJoints, int whichone);
int main(int argc, char **argv[])
{
//OpenCV中开启CPU的硬件指令优化功能函数
setUseOptimized(true);
// Sensor
IKinectSensor* pSensor;
HRESULT hResult = S_OK;
hResult = GetDefaultKinectSensor(&pSensor);
if (FAILED(hResult)){
std::cerr << "Error : GetDefaultKinectSensor" << std::endl;
return -1;
}
hResult = pSensor->Open();
if (FAILED(hResult)){
std::cerr << "Error : IKinectSensor::Open()" << std::endl;
return -1;
}
//Source
IDepthFrameSource *pDepthrSource;
hResult = pSensor->get_DepthFrameSource(&pDepthrSource);
if (FAILED(hResult)){
std::cerr << "Error : IKinectSensor::get_DepthFrameSource()" << std::endl;
}
IBodyFrameSource* pBodySource;
hResult = pSensor->get_BodyFrameSource(&pBodySource);
if (FAILED(hResult)){
std::cerr << "Error : IKinectSensor::get_BodyFrameSource()" << std::endl;
return -1;
}
// Reader
IDepthFrameReader*pDepthReader;
hResult = pDepthrSource->OpenReader(&pDepthReader);
if (FAILED(hResult)){
std::cerr << "Error : IDepthFrameReader::OpenReader()" << std::endl;
return -1;
}
IBodyFrameReader* pBodyReader;
hResult = pBodySource->OpenReader(&pBodyReader);
if (FAILED(hResult)){
std::cerr << "Error : IBodyFrameSource::OpenReader()" << std::endl;
return -1;
}
// Description
IFrameDescription* pDescription;
hResult = pDepthrSource->get_FrameDescription(&pDescription);
if (FAILED(hResult)){
std::cerr << "Error : IDepthFrameSource::get_FrameDescription()" << std::endl;
return -1;
}
int width = 0;
int height = 0;
pDescription->get_Width(&width);
pDescription->get_Height(&height);
UINT16 *data = new UINT16[width*height];
cv::Mat bufferMat(height, width, CV_8UC1);
cv::Mat bodyMat(height / 2, width / 2, CV_8UC4);
cv::namedWindow("Body");
// Color Table
cv::Vec3b color[BODY_COUNT];
color[0] = cv::Vec3b(255, 0, 0);
color[1] = cv::Vec3b(0, 255, 0);
color[2] = cv::Vec3b(0, 0, 255);
color[3] = cv::Vec3b(255, 255, 0);
color[4] = cv::Vec3b(255, 0, 255);
color[5] = cv::Vec3b(0, 255, 255);
// Coordinate Mapper
ICoordinateMapper* pCoordinateMapper;
hResult = pSensor->get_CoordinateMapper(&pCoordinateMapper);
if (FAILED(hResult)){
std::cerr << "Error : IKinectSensor::get_CoordinateMapper()" << std::endl;
return -1;
}
while (1){
// Frame
IDepthFrame*pDepthframe;
hResult = pDepthReader->AcquireLatestFrame(&pDepthframe);
if (SUCCEEDED(hResult))
{
pDepthframe->CopyFrameDataToArray(height*width, data);
for (int i = 0; i < width*height; i++)
{
byte intensity = (byte)(data[i] >> 3); //类型的转换,data内的值即为距离
reinterpret_cast<BYTE*>(bufferMat.data)[i] = intensity;
}
}
//更新骨骼帧
IBodyFrame* pBodyFrame = nullptr;
hResult = pBodyReader->AcquireLatestFrame(&pBodyFrame);
if (SUCCEEDED(hResult)){
IBody* pBody[BODY_COUNT] = { 0 };
//更新骨骼数据
hResult = pBodyFrame->GetAndRefreshBodyData(BODY_COUNT, pBody);
if (SUCCEEDED(hResult)){
for (int count = 0; count < BODY_COUNT; count++){
BOOLEAN bTracked = false;
hResult = pBody[count]->get_IsTracked(&bTracked);
if (SUCCEEDED(hResult) && bTracked){
Joint joint[JointType::JointType_Count];
hResult = pBody[count]->GetJoints(JointType::JointType_Count, joint);
if (SUCCEEDED(hResult)){
// Left Hand State
HandState leftHandState = HandState::HandState_Unknown;
hResult = pBody[count]->get_HandLeftState(&leftHandState);
if (SUCCEEDED(hResult)){
DepthSpacePoint depthSpacePoint = { 0 };
hResult = pCoordinateMapper->MapCameraPointToDepthSpace(joint[JointType::JointType_HandLeft].Position,&depthSpacePoint );
if (SUCCEEDED(hResult)){
int x = static_cast<int>(depthSpacePoint.X);
int y = static_cast<int>(depthSpacePoint.Y);
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)){
if (leftHandState == HandState::HandState_Open){
// cv::circle(bufferMat, cv::Point(x, y), 75, cv::Scalar(0, 128, 0), 5, CV_AA);
}
else if (leftHandState == HandState::HandState_Closed){
// cv::circle(bufferMat, cv::Point(x, y), 75, cv::Scalar(0, 0, 128), 5, CV_AA);
}
else if (leftHandState == HandState::HandState_Lasso){
// cv::circle(bufferMat, cv::Point(x, y), 75, cv::Scalar(128, 128, 0), 5, CV_AA);
}
}
}
}
// Right Hand State
HandState rightHandState = HandState::HandState_Unknown;
hResult = pBody[count]->get_HandRightState(&rightHandState);
if (SUCCEEDED(hResult)){
DepthSpacePoint depthSpacePoint = { 0 };
hResult = pCoordinateMapper->MapCameraPointToDepthSpace(joint[JointType::JointType_HandRight].Position, &depthSpacePoint);
if (SUCCEEDED(hResult)){
int x = static_cast<int>(depthSpacePoint.X);
int y = static_cast<int>(depthSpacePoint.Y);
mouse_event(MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE,
(depthSpacePoint.X - 160) * 65536 / 640 * 2, (depthSpacePoint.Y - 120) * 65536 / 480 * 2, 0, 0);
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)){
if (rightHandState == HandState::HandState_Open){
cv::circle(bufferMat, cv::Point(x, y), 50, cv::Scalar(0, 128, 0), 5, CV_AA);
}
else if (rightHandState == HandState::HandState_Closed){
cv::circle(bufferMat, cv::Point(x, y), 50, cv::Scalar(0, 0, 128), 5, CV_AA);
}
else if (rightHandState == HandState::HandState_Lasso){
cv::circle(bufferMat, cv::Point(x, y), 50, cv::Scalar(128, 128, 0), 5, CV_AA);
}
}
}
}
CvPoint skeletonPoint[BODY_COUNT][JointType_Count] = { cvPoint(0, 0) };
// Joint
for (int type = 0; type < JointType::JointType_Count; type++){
DepthSpacePoint depthSpacePoint = { 0 };
pCoordinateMapper->MapCameraPointToDepthSpace(joint[type].Position, &depthSpacePoint);
int x = static_cast<int>(depthSpacePoint.X);
int y = static_cast<int>(depthSpacePoint.Y);
skeletonPoint[count][type].x = x;
skeletonPoint[count][type].y = y;
if ((x >= 0) && (x < width) && (y >= 0) && (y < height)){
cv::circle(bufferMat, cv::Point(x, y), 5, static_cast< cv::Scalar >(color[count]), -1, CV_AA);
}
}
//画骨骼
drawSkeleton(bufferMat, skeletonPoint[count], joint, count);
}
}
}
cv::resize(bufferMat, bodyMat, cv::Size(), 0.5, 0.5);
}
for (int count = 0; count < BODY_COUNT; count++){
SafeRelease(pBody[count]);
}
}
SafeRelease(pDepthframe);
SafeRelease(pBodyFrame);
waitKey(1);
cv::imshow("Body", bodyMat);
}
SafeRelease(pDepthrSource);
SafeRelease(pDepthReader);
SafeRelease(pDescription);
SafeRelease(pBodySource);
// done with body frame reader
SafeRelease(pBodyReader);
SafeRelease(pDescription);
// done with coordinate mapper
SafeRelease(pCoordinateMapper);
if (pSensor){
pSensor->Close();
}
SafeRelease(pSensor);
return 0;
}
void DrawBone(Mat& SkeletonImage, CvPoint po
kinect2.0控制鼠标
5星 · 超过95%的资源 需积分: 50 4 浏览量
2016-08-09
09:26:08
上传
评论 3
收藏 12.94MB ZIP 举报
随便翊
- 粉丝: 3
- 资源: 5
最新资源
- libADLMIDI1-1.5.1-bp155.1.7.aarch64.rpm
- 基于python和resnet50的垃圾分类识别项目源码+全部数据.zip
- C语言进阶学习资料,关于预处理,文件处理,结构体,位移运算的学习 大学生期末复习资料,程序设计课程复习资料
- c++少儿编程专业教师使用资料
- box1234567890
- 少儿编程资料,c++少儿编程专业资料
- libADLMIDI1-1.5.1-bp156.2.6.aarch64.rpm
- java源码 仿360buy京东商城源码 京东JavaWeb项目源代码.rar
- libADLMIDI1-1.5.1-1.2.riscv64.rpm
- 基于ASP的BS架构工艺品展示系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈