===================================================================================
Leap Motion C++环境的配置
先是建一个c++的win32项目
然后配置项目的包含目录和库目录
包含目录中添加
C:\Users\chengk\Documents\LeapDeveloperKit_2.3.0+31542_win\LeapSDK\include
当然,路径要改为你自己的。
然后在库目录中添加:
C:\Users\chengk\Documents\LeapDeveloperKit_2.3.0+31542_win\LeapSDK\lib\x86
接下来级可以开始写代码了:
#include
#include
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"
#include"opencv2/flann.hpp"
#include"opencv2/xfeatures2d.hpp"
#include"opencv2/ml.hpp"
#include"Leap.h"
#pragma comment ( lib, "Leap.lib" )
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
using namespace cv::ml;
using namespace Leap;
class SampleListener : public Listener
{
public:
virtual void onConnect(const Controller&);
virtual void onFrame(const Controller&);
};
void SampleListener::onConnect(const Controller& controller)
{
std::cout << "Connected" << std::endl;
}
void SampleListener::onFrame(const Controller& controller)
{
std::cout << "Frame available" << std::endl;
}
int main()
{
SampleListener listener;
Controller leap;
leap.addListener(listener);
cin.get();
leap.removeListener(listener);
}
这里重写了Listener类,让在Leap Motion连接时和frame可用是输出。
中间需要暂停下,防止还没开始进程就结束了。
==============================================================================
Leap Motion 使用OpenCV获取和显示图像
实现的并不难,就是先设置下可以读取图像,然后在onFrame里读取下图像并显示就可以了
#define _CRT_SECURE_NO_DEPRECATE
#include
#include
#include "opencv2/core.hpp"
#include "opencv2/core/utility.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/calib3d.hpp"
#include "opencv2/imgproc.hpp"
#include"opencv2/flann.hpp"
#include"opencv2/xfeatures2d.hpp"
#include"opencv2/ml.hpp"
#include"Leap.h"
#pragma comment ( lib, "Leap.lib" )
using namespace cv;
using namespace std;
using namespace cv::xfeatures2d;
using namespace cv::ml;
using namespace Leap;
class SampleListener : public Listener
{
public:
virtual void onInit(const Controller&);
virtual void onConnect(const Controller&);
virtual void onDisconnect(const Controller&);
virtual void onExit(const Controller&);
virtual void onFrame(const Controller&);
};
void SampleListener::onInit(const Controller& controller)
{
std::cout << "Initialized" << std::endl;
}
void SampleListener::onConnect(const Controller& controller)
{
std::cout << "Connected" << std::endl;
}
void SampleListener::onDisconnect(const Controller& controller)
{
std::cout << "Disconnected" << std::endl;
}
void SampleListener::onExit(const Controller& controller)
{
std::cout << "Exited" << std::endl;
}
void SampleListener::onFrame(const Controller& controller)
{
const Frame frame = controller.frame();
ImageList images = frame.images();
Mat leftMat;
Mat rightMat;
if (images.count() == 2)
{
leftMat = Mat(images[0].height(), images[0].width(), CV_8UC1, (void *)images[0].data());
rightMat = Mat(images[1].height(), images[1].width(), CV_8UC1, (void *)images[1].data());
imshow("leftMat", leftMat);
imshow("rightMat", rightMat);
waitKey(1);
}
}
int main()
{
SampleListener listener;
Controller leap;
leap.addListener(listener);
leap.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
leap.setPolicy(Leap::Controller::POLICY_IMAGES);
std::cin.get();
leap.removeListener(listener);
return 0;
}
==================================================================================
Leap Motion 用EmguCV 显示图像并对齐手指
使用的为控制台应用程序。主要的代码都在重写的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();
Imagebyte> left = new Imagebyte>(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;
Imagebyte> left = new Imagebyte>(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();
}
}
}