LeapMotion开发(二)--Opencv绘制手掌位置

2024-01-15 16:30

本文主要是介绍LeapMotion开发(二)--Opencv绘制手掌位置,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

利用Leapmotion提取的掌心的三维坐标在空白画布上画出与手掌位置相对应的一个圆圈。

 

友情链接:

Opencv环境配置:http://blog.csdn.net/zmdsjtu/article/details/52235056

LeapMotion配置:http://blog.csdn.net/zmdsjtu/article/details/52514270

 

实现功能描述:在官方示例Sample.cpp的基础上提取手掌有效坐标,配置opencv环境利用画圆的函数将三维坐标对应于圆的横纵坐标以及半径。

 

首先介绍传参部分:

       

 palm=hand.palmPosition();

 

palm为vector。

这是本文最为重要的一行代码,其他诸如opencv显示都是集成的,类似的其他各种数据可以从示例程序的cout里寻找,这里的hand.palmPosition()返回的是一个vector,可以进行一切你想要的操作。

 

 

 

Opencv部分函数部分:

 

  

  //圆心 Point center = Point(255, 255);//半径 int r = 100;//承载图像 Mat picture(500, 500, CV_8UC3, Scalar(255, 255, 255));center= Point(palm[0]+320, palm[2]+240);r= palm[1] / 2;circle(picture,center, r, Scalar(0, 0,0));waitKey(30);
 

waitKey()务必加上。。。血的教训

 

其中palm[0],palm[1],palm[2]对应这手掌的三围坐标

 

为了观观赏性把左右挥动加上320作为图像里的横纵,前后加上240为纵坐标,上下毫米数除以2作为半径大小。


整体代码如下:

#include <iostream>
#include <cstring>
#include "Leap.h"
#include<opencv2\highgui.hpp>
#include<opencv2\imgproc\imgproc.hpp>
#include<opencv2\core\core.hpp>
using namespace cv;
using namespace Leap;
using namespace std;Vector palm;
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&);virtual void onFocusGained(const Controller&);virtual void onFocusLost(const Controller&);virtual void onDeviceChange(const Controller&);virtual void onServiceConnect(const Controller&);virtual void onServiceDisconnect(const Controller&);virtual void onServiceChange(const Controller&);virtual void onDeviceFailure(const Controller&);virtual void onLogMessage(const Controller&, MessageSeverity severity, int64_t timestamp, const char* msg);
};const std::string fingerNames[] = { "Thumb", "Index", "Middle", "Ring", "Pinky" };
const std::string boneNames[] = { "Metacarpal", "Proximal", "Middle", "Distal" };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 informationconst Frame frame = controller.frame();std::cout << "Frame id: " << frame.id()<< ", timestamp: " << frame.timestamp()<< ", hands: " << frame.hands().count()<< ", extended fingers: " << frame.fingers().extended().count() << std::endl;HandList hands = frame.hands();for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {// Get the first handconst Hand hand = *hl;std::string handType = hand.isLeft() ? "Left hand" : "Right hand";std::cout << std::string(2, ' ') << handType << ", id: " << hand.id()<< ", palm position: " << hand.palmPosition() << std::endl;palm = hand.palmPosition();// Get the hand's normal vector and directionconst Vector normal = hand.palmNormal();const Vector direction = hand.direction();// Calculate the hand's pitch, roll, and yaw anglesstd::cout << std::string(2, ' ') << "pitch: " << direction.pitch() * RAD_TO_DEG << " degrees, "<< "roll: " << normal.roll() * RAD_TO_DEG << " degrees, "<< "yaw: " << direction.yaw() * RAD_TO_DEG << " degrees" << std::endl;// Get the Arm boneArm arm = hand.arm();/*std::cout << std::string(2, ' ') <<  "Arm direction: " << arm.direction()<< " wrist position: " << arm.wristPosition()<< " elbow position: " << arm.elbowPosition() << std::endl;*/// Get fingersconst FingerList fingers = hand.fingers();for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {const Finger finger = *fl;/*std::cout << std::string(4, ' ') <<  fingerNames[finger.type()]<< " finger, id: " << finger.id()<< ", length: " << finger.length()<< "mm, width: " << finger.width() << std::endl;*/// Get finger bonesfor (int b = 0; b < 4; ++b) {Bone::Type boneType = static_cast<Bone::Type>(b);Bone bone = finger.bone(boneType);/*std::cout << std::string(6, ' ') <<  boneNames[boneType]<< " bone, start: " << bone.prevJoint()<< ", end: " << bone.nextJoint()<< ", direction: " << bone.direction() << std::endl;*/}}}if (!frame.hands().isEmpty()) {std::cout << std::endl;}}void SampleListener::onFocusGained(const Controller& controller) {std::cout << "Focus Gained" << std::endl;
}void SampleListener::onFocusLost(const Controller& controller) {std::cout << "Focus Lost" << std::endl;
}void SampleListener::onDeviceChange(const Controller& controller) {std::cout << "Device Changed" << std::endl;const DeviceList devices = controller.devices();for (int i = 0; i < devices.count(); ++i) {std::cout << "id: " << devices[i].toString() << std::endl;std::cout << "  isStreaming: " << (devices[i].isStreaming() ? "true" : "false") << std::endl;std::cout << "  isSmudged:" << (devices[i].isSmudged() ? "true" : "false") << std::endl;std::cout << "  isLightingBad:" << (devices[i].isLightingBad() ? "true" : "false") << std::endl;}
}void SampleListener::onServiceConnect(const Controller& controller) {std::cout << "Service Connected" << std::endl;
}void SampleListener::onServiceDisconnect(const Controller& controller) {std::cout << "Service Disconnected" << std::endl;
}void SampleListener::onServiceChange(const Controller& controller) {std::cout << "Service Changed" << std::endl;
}void SampleListener::onDeviceFailure(const Controller& controller) {std::cout << "Device Error" << std::endl;const Leap::FailedDeviceList devices = controller.failedDevices();for (FailedDeviceList::const_iterator dl = devices.begin(); dl != devices.end(); ++dl) {const FailedDevice device = *dl;std::cout << "  PNP ID:" << device.pnpId();std::cout << "    Failure type:" << device.failure();}
}void SampleListener::onLogMessage(const Controller&, MessageSeverity s, int64_t t, const char* msg) {switch (s) {case Leap::MESSAGE_CRITICAL:std::cout << "[Critical]";break;case Leap::MESSAGE_WARNING:std::cout << "[Warning]";break;case Leap::MESSAGE_INFORMATION:std::cout << "[Info]";break;case Leap::MESSAGE_UNKNOWN:std::cout << "[Unknown]";}std::cout << "[" << t << "] ";std::cout << msg << std::endl;
}int main(int argc, char** argv) {// Create a sample listener and controllerSampleListener listener;Controller controller;// Have the sample listener receive events from the controllercontroller.addListener(listener);if (argc > 1 && strcmp(argv[1], "--bg") == 0)controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);controller.setPolicy(Leap::Controller::POLICY_ALLOW_PAUSE_RESUME);// Keep this process running until Enter is pressedstd::cout << "Press Enter to quit, or enter 'p' to pause or unpause the service..." << std::endl;bool paused = false;//圆心  Point center = Point(255, 255);//半径  int r = 100;//承载图像  //参数为:承载的图像、圆心、半径、颜色、粗细、线型  while (true) {Mat picture(500, 500, CV_8UC3, Scalar(255, 255, 255));center = Point(palm[0] + 320, palm[2] + 240);r = palm[1] / 2;circle(picture, center, r, Scalar(0, 0, 0));imshow("控制画图", picture);waitKey(30);}// Remove the sample listener when donecontroller.removeListener(listener);return 0;
}


结果如图所示:(为了看上去效果好一点加了一段调用摄像头的代码)




最后祝大家编程愉快~

这篇关于LeapMotion开发(二)--Opencv绘制手掌位置的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/609510

相关文章

Spring Security基于数据库的ABAC属性权限模型实战开发教程

《SpringSecurity基于数据库的ABAC属性权限模型实战开发教程》:本文主要介绍SpringSecurity基于数据库的ABAC属性权限模型实战开发教程,本文给大家介绍的非常详细,对大... 目录1. 前言2. 权限决策依据RBACABAC综合对比3. 数据库表结构说明4. 实战开始5. MyBA

使用Python开发一个简单的本地图片服务器

《使用Python开发一个简单的本地图片服务器》本文介绍了如何结合wxPython构建的图形用户界面GUI和Python内建的Web服务器功能,在本地网络中搭建一个私人的,即开即用的网页相册,文中的示... 目录项目目标核心技术栈代码深度解析完整代码工作流程主要功能与优势潜在改进与思考运行结果总结你是否曾经

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

Python基于wxPython和FFmpeg开发一个视频标签工具

《Python基于wxPython和FFmpeg开发一个视频标签工具》在当今数字媒体时代,视频内容的管理和标记变得越来越重要,无论是研究人员需要对实验视频进行时间点标记,还是个人用户希望对家庭视频进行... 目录引言1. 应用概述2. 技术栈分析2.1 核心库和模块2.2 wxpython作为GUI选择的优

opencv图像处理之指纹验证的实现

《opencv图像处理之指纹验证的实现》本文主要介绍了opencv图像处理之指纹验证的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录一、简介二、具体案例实现1. 图像显示函数2. 指纹验证函数3. 主函数4、运行结果三、总结一、

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤

利用Python开发Markdown表格结构转换为Excel工具

《利用Python开发Markdown表格结构转换为Excel工具》在数据管理和文档编写过程中,我们经常使用Markdown来记录表格数据,但它没有Excel使用方便,所以本文将使用Python编写一... 目录1.完整代码2. 项目概述3. 代码解析3.1 依赖库3.2 GUI 设计3.3 解析 Mark

利用Go语言开发文件操作工具轻松处理所有文件

《利用Go语言开发文件操作工具轻松处理所有文件》在后端开发中,文件操作是一个非常常见但又容易出错的场景,本文小编要向大家介绍一个强大的Go语言文件操作工具库,它能帮你轻松处理各种文件操作场景... 目录为什么需要这个工具?核心功能详解1. 文件/目录存javascript在性检查2. 批量创建目录3. 文件

基于Python开发批量提取Excel图片的小工具

《基于Python开发批量提取Excel图片的小工具》这篇文章主要为大家详细介绍了如何使用Python中的openpyxl库开发一个小工具,可以实现批量提取Excel图片,有需要的小伙伴可以参考一下... 目前有一个需求,就是批量读取当前目录下所有文件夹里的Excel文件,去获取出Excel文件中的图片,并

基于Python开发PDF转PNG的可视化工具

《基于Python开发PDF转PNG的可视化工具》在数字文档处理领域,PDF到图像格式的转换是常见需求,本文介绍如何利用Python的PyMuPDF库和Tkinter框架开发一个带图形界面的PDF转P... 目录一、引言二、功能特性三、技术架构1. 技术栈组成2. 系统架构javascript设计3.效果图