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

相关文章

使用Python绘制3D堆叠条形图全解析

《使用Python绘制3D堆叠条形图全解析》在数据可视化的工具箱里,3D图表总能带来眼前一亮的效果,本文就来和大家聊聊如何使用Python实现绘制3D堆叠条形图,感兴趣的小伙伴可以了解下... 目录为什么选择 3D 堆叠条形图代码实现:从数据到 3D 世界的搭建核心代码逐行解析细节优化应用场景:3D 堆叠图

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

Python如何将OpenCV摄像头视频流通过浏览器播放

《Python如何将OpenCV摄像头视频流通过浏览器播放》:本文主要介绍Python如何将OpenCV摄像头视频流通过浏览器播放的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完... 目录方法1:使用Flask + MJPEG流实现代码使用方法优点缺点方法2:使用WebSocket传输视

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部