1.检测视频流中已存在的手的个数
/*
Created by Jinhua Zhao,2017.09.24.
Contact:3210513029@qq.com
vs2017+Opencv3.30+realsense SR300
*/
#include <pxcsensemanager.h>
#include <pxcsession.h>
#include <pxchandconfiguration.h>
#include <pxccursordata.h>
#include <pxchandmodule.h>
#include <pxchanddata.h>
#include <pxcprojection.h>
#include <pxcpowerstate.h>
#include <pxchandcursormodule.h>
#include <pxccursorconfiguration.h>
#include <utilities\pxcsmoother.h>
#include <opencv2\opencv.hpp>
#include <Windows.h>
using namespace std;
using namespace cv;
int main() {
PXCSenseManager* sm = PXCSenseManager::CreateInstance();
sm->EnableHand();
PXCHandModule* handMoudle = sm->QueryHand();
PXCHandConfiguration* handConfig = handMoudle->CreateActiveConfiguration();
handConfig->SetTrackingMode(PXCHandData::TrackingModeType::TRACKING_MODE_FULL_HAND);
handConfig->EnableStabilizer(true);
handConfig->EnableTrackedJoints(true);
handConfig->EnableNormalizedJoints(true);
handConfig->EnableSegmentationImage(true);
handConfig->ApplyChanges();
sm->Init();
PXCHandData* handData = handMoudle->CreateOutput();
PXCHandData::IHand* ihand[3];
pxcUID handId[3];
while (sm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)
{
handData->Update();
int nHands = handData->QueryNumberOfHands();
cout << nHands << endl;
sm->ReleaseFrame();
}
handData->Release();
handConfig->Release();
sm->Close();
sm->Release();
return 0;
}
2.用食指控制鼠标运动,其中需要根据要求的手指以及具体应用场景进行更改!
/*
Created by Jinhua Zhao,2017.09.24.
Contact:3210513029@qq.com
vs2017+Opencv3.30+realsense SR300
*/
#include <pxcsensemanager.h>
#include <pxcsession.h>
#include <pxchandconfiguration.h>
#include <pxccursordata.h>
#include <pxchandmodule.h>
#include <pxchanddata.h>
#include <pxcprojection.h>
#include <pxcpowerstate.h>
#include <pxchandcursormodule.h>
#include <pxccursorconfiguration.h>
#include <utilities\pxcsmoother.h>
#include <opencv2\opencv.hpp>
#include <Windows.h>
using namespace std;
using namespace cv;
int main() {
PXCSenseManager* sm = PXCSenseManager::CreateInstance();
sm->EnableHand();
PXCHandModule* handMoudle = sm->QueryHand();
PXCHandConfiguration* handConfig = handMoudle->CreateActiveConfiguration();
handConfig->SetTrackingMode(PXCHandData::TrackingModeType::TRACKING_MODE_FULL_HAND);
handConfig->EnableStabilizer(true);
handConfig->EnableTrackedJoints(true);
handConfig->EnableNormalizedJoints(true);
handConfig->EnableSegmentationImage(true);
handConfig->ApplyChanges();
sm->Init();
PXCHandData* handData = handMoudle->CreateOutput();
PXCHandData::IHand* ihand[3];
PXCHandData::JointData jointdata;
PXCHandData::FingerData fingerdata;
PXCHandData::JointType jointtype=PXCHandData::JointType::JOINT_CENTER;
PXCHandData::FingerType fingertye = PXCHandData::FingerType::FINGER_INDEX;
pxcUID handId[3];
int nHands;
time_t time1, time2;
for (size_t i = 0; i < 3; i++) {
ihand[i] = nullptr;
handId[i] = 0;
}
float before_x, before_y, before_z;
float now_x, now_y, now_z;
float delta_x, delta_y, delta_z;
float tip;
while (sm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)
{
handData->Update();
nHands = handData->QueryNumberOfHands();//Query the numbers of the hands.
if (nHands == 0){ sm->ReleaseFrame(); continue;}
for (size_t i = 0; i < nHands; i++) //Dealing with the every piece of data of the hand.
{
handData->QueryHandId(PXCHandData::ACCESS_ORDER_NEAR_TO_FAR, i, handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]);
handData->QueryHandDataById(handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN], ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]);
auto side = ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]->QueryBodySide();
ihand[side] = ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN];
handId[side] = handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN];
ihand[i]->QueryTrackedJoint(jointtype, jointdata);
ihand[i]->QueryFingerData(fingertye, fingerdata);//The tip of index finger.
before_x = 1000 * jointdata.positionWorld.x;
before_y = 1000 * jointdata.positionWorld.y;
before_z = 1000 * jointdata.positionWorld.z;
delta_x = now_x - before_x;
delta_y = now_y - before_y;
delta_z = now_z - before_z;
now_x = before_x;
now_y = before_y;
now_z = before_z;
tip = fingerdata.foldedness;
cout << tip << endl;
if (tip<10)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, delta_x, delta_y, 0, 0);
Sleep(500);
}
else
mouse_event(MOUSEEVENTF_MOVE|MOUSEEVENTF_LEFTUP|MOUSEEVENTF_RIGHTUP, delta_x, delta_y, 0, 0);
cout << "x=" << delta_x << endl;
cout << "y=" << delta_y << endl;
cout << "z=" << delta_z << endl;
}
sm->ReleaseFrame();
}
handData->Release();
handConfig->Release();
sm->Close();
sm->Release();
return 0;
}
3.中指控制左鼠标,食指控制右鼠标(测试用的左手,实际左右手都行)
/*
Created by Jinhua Zhao,2017.09.24.
Contact:3210513029@qq.com
vs2017+Opencv3.30+realsense SR300
*/
#include <pxcsensemanager.h>
#include <pxcsession.h>
#include <pxchandconfiguration.h>
#include <pxccursordata.h>
#include <pxchandmodule.h>
#include <pxchanddata.h>
#include <pxcprojection.h>
#include <pxcpowerstate.h>
#include <pxchandcursormodule.h>
#include <pxccursorconfiguration.h>
#include <utilities\pxcsmoother.h>
#include <opencv2\opencv.hpp>
#include <Windows.h>
using namespace std;
using namespace cv;
int main() {
PXCSenseManager* sm = PXCSenseManager::CreateInstance();
sm->EnableHand();
PXCHandModule* handMoudle = sm->QueryHand();
PXCHandConfiguration* handConfig = handMoudle->CreateActiveConfiguration();
handConfig->SetTrackingMode(PXCHandData::TrackingModeType::TRACKING_MODE_FULL_HAND);
handConfig->EnableStabilizer(true);
handConfig->EnableTrackedJoints(true);
handConfig->EnableNormalizedJoints(true);
handConfig->EnableSegmentationImage(true);
handConfig->ApplyChanges();
sm->Init();
PXCHandData* handData = handMoudle->CreateOutput();
PXCHandData::IHand* ihand[3];
PXCHandData::JointData jointdata;
PXCHandData::FingerData fingerdata;
PXCHandData::FingerData fingerdata1;
PXCHandData::JointType jointtype=PXCHandData::JointType::JOINT_CENTER;
PXCHandData::FingerType fingertye = PXCHandData::FingerType::FINGER_INDEX;
PXCHandData::FingerType fingertye1 = PXCHandData::FingerType::FINGER_MIDDLE;
pxcUID handId[3];
int nHands;
time_t time1, time2;
for (size_t i = 0; i < 3; i++) {
ihand[i] = nullptr;
handId[i] = 0;
}
float before_x, before_y, before_z;
float now_x, now_y, now_z;
float delta_x, delta_y, delta_z;
float tip, tip1;
while (sm->AcquireFrame(true) >= PXC_STATUS_NO_ERROR)
{
handData->Update();
nHands = handData->QueryNumberOfHands();//Query the numbers of the hands.
if (nHands == 0){
sm->ReleaseFrame();
SetCursorPos(960,540);
continue;
}
for (size_t i = 0; i < nHands; i++) //Dealing with the every piece of data of the hand.
{
handData->QueryHandId(PXCHandData::ACCESS_ORDER_NEAR_TO_FAR, i, handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]);
handData->QueryHandDataById(handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN], ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]);
auto side = ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN]->QueryBodySide();
ihand[side] = ihand[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN];
handId[side] = handId[PXCHandData::BodySideType::BODY_SIDE_UNKNOWN];
ihand[i]->QueryTrackedJoint(jointtype, jointdata);
ihand[i]->QueryFingerData(fingertye, fingerdata);//The tip of index finger.
ihand[i]->QueryFingerData(fingertye1, fingerdata1);//The tip of index finger.
before_x = 3000 * jointdata.positionWorld.x;
before_y = 3000 * jointdata.positionWorld.y;
//before_z = 1000 * jointdata.positionWorld.z;
delta_x = now_x - before_x;
delta_y = now_y - before_y;
//delta_z = now_z - before_z;
now_x = before_x;
now_y = before_y;
//now_z = before_z;
tip = fingerdata.foldedness;
tip1 = fingerdata1.foldedness;
cout << tip << endl;
cout << tip1 << endl;
if (tip1<5)
{
if (tip>90)
{
mouse_event(MOUSEEVENTF_LEFTDOWN, delta_x, delta_y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, delta_x, delta_y, 0, 0);
Sleep(100);
}
mouse_event(MOUSEEVENTF_RIGHTDOWN, delta_x, delta_y, 0, 0);
mouse_event(MOUSEEVENTF_RIGHTUP, delta_x, delta_y, 0, 0);
Sleep(100);
}
mouse_event(MOUSEEVENTF_MOVE, delta_x, delta_y, 0, 0);
cout << "x=" << delta_x << endl;
cout << "y=" << delta_y << endl;
//cout << "z=" << delta_z << endl;
}
sm->ReleaseFrame();
}
handData->Release();
handConfig->Release();
sm->Close();
sm->Release();
return 0;
}