刚刚入手Leap motion
写了一个控制CS的demo
代码是改DEMO的。很粗糙
方法很简单
1.获取每帧获得指尖位置.
2.然后计算两帧直接的位移。
3.然后设定鼠标位置 + 位移 。
4.捕捉开枪动作
差帧速率判断判断开枪动作。
主要代码
#include "opencv2/opencv.hpp"
#include <iostream>
#include "windows.h"
#include <stdio.h>
using namespace cv;
using namespace std;
Mat show(100,400,CV_8SC3);
int counts=0;
float speed=0;
float origin=0;
Point2f MousePos;
Point2f MousePosPast;
Point2f MouseMove;
POINT currentMousePos;
void InitS()
{
show.setTo(100);
}
Point last;
int ms = 0 ;
int ic = 0 ;
void MouseControl(float x,float y){
int with= GetSystemMetrics(SM_CXFULLSCREEN);
int heigh= GetSystemMetrics(SM_CYFULLSCREEN);
Point2f ploc(x+100,-y+300);
/*
counts++;
if(counts == 400)
{
show.setTo(255);
counts=0;
}
*/
//line(show,Point(counts,50-((int)speed*3)),Point(counts,200),Scalar(255,0,255),1);
//if(ploc.x>40&&ploc.x<200&&ploc.y>20&&ploc.y<130)
{
GetCursorPos(¤tMousePos);
ploc.x = ploc.x-40;
ploc.y = ploc.y-20;
//size(160*120)
speed = origin-y;
if (speed>8 && speed<12)
{
mouse_event (MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0 );
}
MousePos.y =-y+200;
MousePos.x =x;
MouseMove.x = MousePos.x- MousePosPast.x;
MouseMove.y= MousePos.y- MousePosPast.y;
cout<<"speed to gun: "<<speed<<endl;
SetCursorPos(currentMousePos.x+(int)(MouseMove.x*50),currentMousePos.y+(int)(MouseMove*50).y);
origin = y;
}
MousePosPast.y = MousePos.y;
MousePosPast.x = MousePos.x;
/*
imshow("debug",show);
cvWaitKey(1);
*/}
演示视频地址:
http://v.youku.com/v_show/id_XNjAzMzYyOTY0.html?qq-pf-to=pcqq.group