OPENNI学习实践-OPENNI体感开发实战中NITE手势识别样例

#include <stdlib.h>
#include <iostream>
#include <string>

#include <XnCppWrapper.h>
#include <XnOpenNI.h>
#include <XnVFlowRouter.h>
#include <XnVHandPointContext.h>
#include <XnVSessionManager.h>
#include <XnVBroadcaster.h>
#include <XnVPushDetector.h>
#include <XnVSelectableSlider1D.h>
#include <XnVSelectableSlider2D.h>
#include <XnVSteadyDetector.h>
#include <XnVSwipeDetector.h>
#include <XnVCircleDetector.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>

using namespace std;
using namespace xn;
using namespace cv;

DepthGenerator g_DepthGenerator;
Context g_Context;
HandsGenerator g_HandsGenerator;
UserGenerator g_UserGenerator;
XnVFlowRouter* g_pMainFlowRouter;
XnVBroadcaster* m_Broadcaster=NULL;
XnVSessionManager* g_pSessionManager=NULL;
XnVSelectableSlider2D* g_Slider2D_MouseMove=NULL;

XnVPushDetector* m_pPushDetector=NULL;
XnVCircleDetector* m_pCircleDetector=NULL;
XnVSwipeDetector* m_pSwipeDetector=NULL;
XnVSteadyDetector* m_pSteady_MouseClick=NULL;

int X_item=6;
int Y_item=6;
int MouseMove_Xsize=450;
int MouseMove_Ysize=450;
DWORD t1=0;
DWORD t2=0;
int icount=0;
float oldtime=0;
int ScreenRes_X=1920;
int ScreenRes_Y=1080;

void XN_CALLBACK_TYPE SessionStart(const XnPoint3D& ptFocus,void* UserCxt)
{
	printf("SessionStart!!!");
}

void XN_CALLBACK_TYPE SessionEnd(void* UserCxt)
{
	printf("SessionEnd!!!");
}

void XN_CALLBACK_TYPE Slider2D_MouseMove_ValueChange(XnFloat fXValue,XnFloat fYValue,void* cxt)
{
	int CursorPox_X=(int)(fXValue*ScreenRes_X);
	int CursorPox_Y=(int)(ScreenRes_Y-(fYValue*ScreenRes_Y));
	Sleep(10);
	SetCursorPos(CursorPox_X,CursorPox_Y);
}

void XN_CALLBACK_TYPE Steady_OnSteady_MouseClick(XnUInt32 nID,XnFloat fVelocity,void* cxt)
{
	t1=GetTickCount();
	printf("steady!!!");
}

void XN_CALLBACK_TYPE Steady_OnNotSteady_MouseClick(XnUInt32 nID,XnFloat fVelocity,void* cxt)
{
	t1=0;
}

void XN_CALLBACK_TYPE Push_Pushed(XnFloat fVelocity,XnFloat fAngle,void* cxt)
{
	mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,GetMessageExtraInfo());
	mouse_event(MOUSEEVENTF_RIGHTUP,0,0,0,GetMessageExtraInfo());
}

void XN_CALLBACK_TYPE Circle_Cricled(XnFloat fTimes,XnBool bConfident,const XnVCircle *pCircle,void *pUserCxt)
{
	if (icount==2)
	{
		if (oldtime<fTimes)
			printf("顺时针");
		if (oldtime>fTimes)
			printf("逆时针");
		icount=0;
	}
	else
	{
		icount++;
		oldtime=fTimes;
	}
}

static void XN_CALLBACK_TYPE Swipe_SwipeUp(XnFloat fVelocity,XnFloat fAngle,void* cxt)
{
	printf("SwipeUP");
}

static void XN_CALLBACK_TYPE Swipe_SwipeDown(XnFloat fVelocity,XnFloat fAngle,void* cxt)
{
	printf("SwipeDown");
}

static void XN_CALLBACK_TYPE Swipe_SwipeLeft(XnFloat fVelocity,XnFloat fAngle,void* cxt)
{
	printf("SwipeLeft");
}

static void XN_CALLBACK_TYPE Swipe_SwipeRight(XnFloat fVelocity,XnFloat fAngle,void* cxt)
{
	printf("SwipeRight");
}

int main()
{
	ScreenRes_X=GetSystemMetrics(SM_CYSCREEN);
	ScreenRes_Y=GetSystemMetrics(SM_CYSCREEN);

	g_Context.InitFromXmlFile("Sample-Tracking.xml");
	g_Context.FindExistingNode(XN_NODE_TYPE_DEPTH,g_DepthGenerator);
	g_Context.FindExistingNode(XN_NODE_TYPE_HANDS,g_HandsGenerator);

	g_HandsGenerator.SetSmoothing(0.1);

	g_pSessionManager=new XnVSessionManager();
	g_pSessionManager->Initialize(&g_Context,"Click","RaiseHand");
	g_pSessionManager->RegisterSession(NULL,&SessionStart,&SessionEnd);

	g_Slider2D_MouseMove=new XnVSelectableSlider2D(X_item,Y_item,MouseMove_Xsize,MouseMove_Ysize);
	g_Slider2D_MouseMove->RegisterValueChange(NULL,&Slider2D_MouseMove_ValueChange);
	g_pSessionManager->AddListener(g_Slider2D_MouseMove);
	m_pSteady_MouseClick=new XnVSteadyDetector;
	m_pSteady_MouseClick->RegisterSteady(NULL,&Steady_OnSteady_MouseClick);
	m_pSteady_MouseClick->RegisterNotSteady(NULL,&Steady_OnNotSteady_MouseClick);
	g_pSessionManager->AddListener(m_pSteady_MouseClick);

	m_pPushDetector=new XnVPushDetector;
	m_pPushDetector->RegisterPush(NULL,&Push_Pushed);
	g_pSessionManager->AddListener(m_pPushDetector);

	m_pCircleDetector=new XnVCircleDetector;
	m_pCircleDetector->RegisterCircle(NULL,&Circle_Cricled);
	g_pSessionManager->AddListener(m_pCircleDetector);

	m_pSwipeDetector=new XnVSwipeDetector;
	m_pSwipeDetector->RegisterSwipeUp(NULL,&Swipe_SwipeUp);
	m_pSwipeDetector->RegisterSwipeDown(NULL,&Swipe_SwipeDown);
	m_pSwipeDetector->RegisterSwipeLeft(NULL,&Swipe_SwipeLeft);
	m_pSwipeDetector->RegisterSwipeRight(NULL,&Swipe_SwipeRight);
	g_pSessionManager->AddListener(m_pSwipeDetector);

	g_Context.StartGeneratingAll();

	while (true)
	{
		g_Context.WaitAndUpdateAll();
		g_pSessionManager->Update(&g_Context);
	}

	return 0;
}
该样例实现利用手的位置和动作(push)进行鼠标的模拟操作,其中xml文档位于NITE文件夹里。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值