应网友要求,我使用OpenCV对LeapMotion的图像数据进行了最基本的显示。注意的是,LeapMotion对于Image的数据有2个,因为LeapMotion和人一样有2只眼睛。
代码可以从我的Github下载(点击右侧的Download ZIP可一次性下载当前页面所有程序),“ShowImageByOpenCV”对应了本文章对应的代码。
VS2013 + LeapMotion2.2.0 + OpenCV2.4.10,可以直接编译运行(VS2010和VS2012未验证过,转换后应该也能运行),而不必安装Leap的SDK或OpenCV的SDK。
运行前,请确保你在LeapMotion控制面板中设置了,程序可以接收到LeapMotion的图像数据才行。注意这里要2部,缺一部就无法成功!
最终运行效果截图
信息在LeapMotion眼中都是变形的,如图我手中拿着一个矩形的盒子,显示出来不是矩形。
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
/******************************************************************************\ * Copyright (C) 2012-2014 Leap Motion, Inc. All rights reserved. * * Leap Motion proprietary and confidential. Not for distribution. * * Use subject to the terms of the Leap Motion SDK Agreement available at * * https://developer.leapmotion.com/sdk_agreement, or another agreement * * between Leap Motion and you, your company or other organization. * \******************************************************************************/ #include <iostream> #include <string.h> #include "Leap.h" #include "opencv2/opencv.hpp" using namespace Leap; using namespace cv; 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&); private: }; 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) { // Note: not dispatched when running in a debugger. std::cout << "Disconnected" << std::endl; } void SampleListener::onExit(const Controller& controller) { std::cout << "Exited" << std::endl; } void SampleListener::onFrame(const Controller& controller) { // Get the most recent frame and report some basic information 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); } } #include <windows.h> int main(int argc, char** argv) { // Create a sample listener and controller SampleListener listener; Controller controller; // Have the sample listener receive events from the controller controller.addListener(listener); controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES); controller.setPolicy(Leap::Controller::POLICY_IMAGES); Sleep(1000); std::cout << "is background policy set success : " << controller.isPolicySet(Leap::Controller::POLICY_BACKGROUND_FRAMES)<<std::endl; std::cout << "is image policy set success : " << controller.isPolicySet(Leap::Controller::POLICY_IMAGES)<<std::endl; // Keep this process running until Enter is pressed std::cout << "Press Enter to quit..." << std::endl; std::cin.get(); // Remove the sample listener when done controller.removeListener(listener); return 0; } |