Leap Motion C++ 开发笔记(二) 打开摄像头

来源:互联网 发布:mac 彻底删除office 编辑:程序博客网 时间:2024/05/16 04:29

代码部分

主要使用了SDK中的frame.images()获取图像

遗憾的是,获取的图像仅为8位

摘自官方文档的说明:

If a 32-bit-per-component texture format is not available on your target platform, you can use a separate texture for the x and y lookup values and encode the floating point values into multiple 8-bit color components. You then have to decode the values before using them to look up the raw brightness values.

也就是说Leap Motion并不能像Kinect那样通过像素点得到精确的深度信息值

#include "Leap.h"  #include <iostream>  // OpenCV Header  #include<opencv2/opencv.hpp>  #include "opencv2/core/core.hpp"#include "opencv2/highgui/highgui.hpp"using namespace cv;using namespace std;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&);private:};void SampleListener::onInit(const Controller& controller) {    std::cout << "初始化完成~" << std::endl;}void SampleListener::onConnect(const Controller& controller) {    std::cout << "正在连接" << std::endl;}void SampleListener::onDisconnect(const Controller& controller) {    std::cout << "连接失败" << std::endl;}void SampleListener::onExit(const Controller& controller) {}void SampleListener::onFrame(const Controller& controller) {    const Frame frame = controller.frame();    ImageList images = frame.images();    Mat A;//左侧红外摄像头      Mat B;//右侧      if (images.count() >= 2)    {        A = Mat(images[0].height(), images[0].width(),CV_8UC1, (void *)images[0].data());        B = Mat(images[1].height(), images[1].width(), CV_8UC1, (void *)images[1].data());        imshow("left",A);        imshow("right", B);        waitKey(1);    }}int main(int argc, char** argv) {    SampleListener listener;    Controller controller;    controller.addListener(listener);    controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);    controller.setPolicy(Leap::Controller::POLICY_IMAGES);    std::cout << "输入Enter退出" << std::endl;    std::cin.get();    controller.removeListener(listener);    return 0;}

实现效果

右下图可以看出,左右两个红外摄像头区别还是很明显的

这里写图片描述

原创粉丝点击