当前位置:   article > 正文

LeapMotion开发(二)--Opencv绘制手掌位置_leapmotion手指坐标

leapmotion手指坐标

利用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部分函数部分:

 

  

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

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

 

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

 

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


整体代码如下:

  1. #include <iostream>
  2. #include <cstring>
  3. #include "Leap.h"
  4. #include<opencv2\highgui.hpp>
  5. #include<opencv2\imgproc\imgproc.hpp>
  6. #include<opencv2\core\core.hpp>
  7. using namespace cv;
  8. using namespace Leap;
  9. using namespace std;
  10. Vector palm;
  11. class SampleListener : public Listener {
  12. public:
  13. virtual void onInit(const Controller&);
  14. virtual void onConnect(const Controller&);
  15. virtual void onDisconnect(const Controller&);
  16. virtual void onExit(const Controller&);
  17. virtual void onFrame(const Controller&);
  18. virtual void onFocusGained(const Controller&);
  19. virtual void onFocusLost(const Controller&);
  20. virtual void onDeviceChange(const Controller&);
  21. virtual void onServiceConnect(const Controller&);
  22. virtual void onServiceDisconnect(const Controller&);
  23. virtual void onServiceChange(const Controller&);
  24. virtual void onDeviceFailure(const Controller&);
  25. virtual void onLogMessage(const Controller&, MessageSeverity severity, int64_t timestamp, const char* msg);
  26. };
  27. const std::string fingerNames[] = { "Thumb", "Index", "Middle", "Ring", "Pinky" };
  28. const std::string boneNames[] = { "Metacarpal", "Proximal", "Middle", "Distal" };
  29. void SampleListener::onInit(const Controller& controller) {
  30. std::cout << "Initialized" << std::endl;
  31. }
  32. void SampleListener::onConnect(const Controller& controller) {
  33. std::cout << "Connected" << std::endl;
  34. }
  35. void SampleListener::onDisconnect(const Controller& controller) {
  36. // Note: not dispatched when running in a debugger.
  37. std::cout << "Disconnected" << std::endl;
  38. }
  39. void SampleListener::onExit(const Controller& controller) {
  40. std::cout << "Exited" << std::endl;
  41. }
  42. void SampleListener::onFrame(const Controller& controller) {
  43. // Get the most recent frame and report some basic information
  44. const Frame frame = controller.frame();
  45. std::cout << "Frame id: " << frame.id()
  46. << ", timestamp: " << frame.timestamp()
  47. << ", hands: " << frame.hands().count()
  48. << ", extended fingers: " << frame.fingers().extended().count() << std::endl;
  49. HandList hands = frame.hands();
  50. for (HandList::const_iterator hl = hands.begin(); hl != hands.end(); ++hl) {
  51. // Get the first hand
  52. const Hand hand = *hl;
  53. std::string handType = hand.isLeft() ? "Left hand" : "Right hand";
  54. std::cout << std::string(2, ' ') << handType << ", id: " << hand.id()
  55. << ", palm position: " << hand.palmPosition() << std::endl;
  56. palm = hand.palmPosition();
  57. // Get the hand's normal vector and direction
  58. const Vector normal = hand.palmNormal();
  59. const Vector direction = hand.direction();
  60. // Calculate the hand's pitch, roll, and yaw angles
  61. std::cout << std::string(2, ' ') << "pitch: " << direction.pitch() * RAD_TO_DEG << " degrees, "
  62. << "roll: " << normal.roll() * RAD_TO_DEG << " degrees, "
  63. << "yaw: " << direction.yaw() * RAD_TO_DEG << " degrees" << std::endl;
  64. // Get the Arm bone
  65. Arm arm = hand.arm();
  66. /*
  67. std::cout << std::string(2, ' ') << "Arm direction: " << arm.direction()
  68. << " wrist position: " << arm.wristPosition()
  69. << " elbow position: " << arm.elbowPosition() << std::endl;
  70. */
  71. // Get fingers
  72. const FingerList fingers = hand.fingers();
  73. for (FingerList::const_iterator fl = fingers.begin(); fl != fingers.end(); ++fl) {
  74. const Finger finger = *fl;
  75. /*
  76. std::cout << std::string(4, ' ') << fingerNames[finger.type()]
  77. << " finger, id: " << finger.id()
  78. << ", length: " << finger.length()
  79. << "mm, width: " << finger.width() << std::endl;
  80. */
  81. // Get finger bones
  82. for (int b = 0; b < 4; ++b) {
  83. Bone::Type boneType = static_cast<Bone::Type>(b);
  84. Bone bone = finger.bone(boneType);
  85. /*
  86. std::cout << std::string(6, ' ') << boneNames[boneType]
  87. << " bone, start: " << bone.prevJoint()
  88. << ", end: " << bone.nextJoint()
  89. << ", direction: " << bone.direction() << std::endl;
  90. */
  91. }
  92. }
  93. }
  94. if (!frame.hands().isEmpty()) {
  95. std::cout << std::endl;
  96. }
  97. }
  98. void SampleListener::onFocusGained(const Controller& controller) {
  99. std::cout << "Focus Gained" << std::endl;
  100. }
  101. void SampleListener::onFocusLost(const Controller& controller) {
  102. std::cout << "Focus Lost" << std::endl;
  103. }
  104. void SampleListener::onDeviceChange(const Controller& controller) {
  105. std::cout << "Device Changed" << std::endl;
  106. const DeviceList devices = controller.devices();
  107. for (int i = 0; i < devices.count(); ++i) {
  108. std::cout << "id: " << devices[i].toString() << std::endl;
  109. std::cout << " isStreaming: " << (devices[i].isStreaming() ? "true" : "false") << std::endl;
  110. std::cout << " isSmudged:" << (devices[i].isSmudged() ? "true" : "false") << std::endl;
  111. std::cout << " isLightingBad:" << (devices[i].isLightingBad() ? "true" : "false") << std::endl;
  112. }
  113. }
  114. void SampleListener::onServiceConnect(const Controller& controller) {
  115. std::cout << "Service Connected" << std::endl;
  116. }
  117. void SampleListener::onServiceDisconnect(const Controller& controller) {
  118. std::cout << "Service Disconnected" << std::endl;
  119. }
  120. void SampleListener::onServiceChange(const Controller& controller) {
  121. std::cout << "Service Changed" << std::endl;
  122. }
  123. void SampleListener::onDeviceFailure(const Controller& controller) {
  124. std::cout << "Device Error" << std::endl;
  125. const Leap::FailedDeviceList devices = controller.failedDevices();
  126. for (FailedDeviceList::const_iterator dl = devices.begin(); dl != devices.end(); ++dl) {
  127. const FailedDevice device = *dl;
  128. std::cout << " PNP ID:" << device.pnpId();
  129. std::cout << " Failure type:" << device.failure();
  130. }
  131. }
  132. void SampleListener::onLogMessage(const Controller&, MessageSeverity s, int64_t t, const char* msg) {
  133. switch (s) {
  134. case Leap::MESSAGE_CRITICAL:
  135. std::cout << "[Critical]";
  136. break;
  137. case Leap::MESSAGE_WARNING:
  138. std::cout << "[Warning]";
  139. break;
  140. case Leap::MESSAGE_INFORMATION:
  141. std::cout << "[Info]";
  142. break;
  143. case Leap::MESSAGE_UNKNOWN:
  144. std::cout << "[Unknown]";
  145. }
  146. std::cout << "[" << t << "] ";
  147. std::cout << msg << std::endl;
  148. }
  149. int main(int argc, char** argv) {
  150. // Create a sample listener and controller
  151. SampleListener listener;
  152. Controller controller;
  153. // Have the sample listener receive events from the controller
  154. controller.addListener(listener);
  155. if (argc > 1 && strcmp(argv[1], "--bg") == 0)
  156. controller.setPolicy(Leap::Controller::POLICY_BACKGROUND_FRAMES);
  157. controller.setPolicy(Leap::Controller::POLICY_ALLOW_PAUSE_RESUME);
  158. // Keep this process running until Enter is pressed
  159. std::cout << "Press Enter to quit, or enter 'p' to pause or unpause the service..." << std::endl;
  160. bool paused = false;
  161. //圆心
  162. Point center = Point(255, 255);
  163. //半径
  164. int r = 100;
  165. //承载图像
  166. //参数为:承载的图像、圆心、半径、颜色、粗细、线型
  167. while (true) {
  168. Mat picture(500, 500, CV_8UC3, Scalar(255, 255, 255));
  169. center = Point(palm[0] + 320, palm[2] + 240);
  170. r = palm[1] / 2;
  171. circle(picture, center, r, Scalar(0, 0, 0));
  172. imshow("控制画图", picture);
  173. waitKey(30);
  174. }
  175. // Remove the sample listener when done
  176. controller.removeListener(listener);
  177. return 0;
  178. }


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




最后祝大家编程愉快~

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/128017
推荐阅读
相关标签
  

闽ICP备14008679号