Leap Motion 使用OpenCV获取和显示图像

实现的并不难,就是先设置下可以读取图像,然后在onFrame里读取下图像并显示就可以了

#define _CRT_SECURE_NO_DEPRECATE
#include <iostream>
#include <stdio.h>
#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;
}

你可能感兴趣的:(opencv,Leap,motion)