使用的爲控制檯應用程序。主要的代碼都在重寫的Listener類裏,代碼如下。
中間需要對座標轉換下。
我只顯示了左邊的那副圖。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Leap;
using Emgu.CV;
using System.Drawing;
using Emgu.CV.Structure;
using Emgu.Util;
using System.Drawing.Imaging;
namespace ConsoleApplication14
{
class asas : Listener
{
Object thislock = new Object();
Image<Gray, byte> left = new Image<Gray, byte>(640, 240);
void safewriteline(string line)
{
lock (thislock)
{
Console.WriteLine(line);
}
}
public override void OnConnect(Controller arg0)
{
safewriteline("Conectted");
}
public override void OnFrame(Controller arg0)
{
Frame frame = arg0.Frame();
ImageList images = frame.Images;
//HandList hands = frame.Hands;
FingerList fingers = frame.Fingers;
Image<Gray, byte> left = new Image<Gray, byte>(640,240);
left.Bytes = images[0].Data;
foreach(var f1 in fingers)
{
Leap.Vector tip = f1.TipPosition;
float h_slope = -(tip.x + 20 * (-1)) / tip.y;
float v_slope = tip.z / tip.y;
Leap.Vector pixel = images[0].Warp(new Leap.Vector(h_slope, v_slope, 0));
CvInvoke.Circle(left, new Point((int)pixel.x, (int)pixel.y), (int)(1/tip.y*1000), new MCvScalar(255), (int)(1/tip.y*1000)*2);
}
CvInvoke.Imshow("asasa",left);
CvInvoke.WaitKey(2);
}
public override void OnInit(Controller arg0)
{
safewriteline("inti");
}
}
}
然後這個是主函數的代碼
namespace ConsoleApplication14
{
class MyApplication
{
static void Main(string[] args)
{
Controller leap = new Controller();
leap.AddListener(new asas());
leap.SetPolicy(Controller.PolicyFlag.POLICY_IMAGES);
Console.ReadKey();
}
}
}
運行圖: