OnlineBodySchemaAdaptation  2.0
handPoseEstimationModule.h
Go to the documentation of this file.
1 // -*- mode:C++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 /*
3  * Copyright: (C) 2017 VisLab, Institute for Systems and Robotics,
4  * Instituto Superior Técnico, Universidade de Lisboa, Lisbon, Portugal
5  * Author: Pedro Vicente <pvicente@isr.tecnico.ulisboa.pt>
6  * CopyPolicy: Released under the terms of the GNU GPL v3.0.
7  *
8  */
9 
23 #ifndef _HPESTIMATION_
24 #define _HPESTIMATION_
25 
26 #include <iostream>
27 #include <iomanip>
28 #include <stdio.h>
29 #include <string.h>
30 #include <time.h>
31 #include <deque>
32 
33 #include <yarp/os/RFModule.h>
34 #include <yarp/os/Bottle.h>
35 #include <yarp/os/Time.h>
36 #include <yarp/os/Log.h>
37 #include <yarp/os/LogStream.h>
38 #include <yarp/os/BufferedPort.h>
39 #include <yarp/sig/Image.h>
40 #include <yarp/sig/Vector.h>
41 
42 //#include <opencv/cv.h>
43 #include <opencv2/core/core.hpp>
44 #include <opencv2/imgproc/imgproc.hpp>
45 #include <opencv2/highgui/highgui.hpp>
46 
47 #include "handPoseEstimation_IDL.h"
48 
53 class handPoseEstimationModule : public yarp::os::RFModule, public handPoseEstimation_IDL
54 {
55  private:
56  // module parameters
57  std::string moduleName;
58  std::string arm;
59  //port Names
60  std::string handlerPortName;
61  std::string imageInputPortRName;
62  std::string imageInputPortLName;
63  std::string armPortName;
64  std::string headPortName;
65  std::string likelihoodPortName;
66  std::string LRimageOutputPortName;
67  std::string headOutPortName;
68  std::string particlesOutPortName;
69  std::string offsetsPortName;
70  bool closing;
71 
72  // Variables
73  int iteration;
74  double initialMean,initialStdDev;
75  double initialArtificialNoiseStdDev, artifNoiseStdDev;
76  double lowerBoundNoise, upperBoundNoise, increasedMultiplier, decreasedMultiplier;
77  double minimumLikelihood;
78  double KDEStdDev;
79  int maxWeightIndex;
80  int minimumIteration;
81  bool stopped, paused;
82  yarp::os::Bottle lastBestOffset;
83  yarp::sig::Vector encodersArm, encodersHead;
84  //yarp::sig::ImageOf<yarp::sig::PixelRgb> *imageR, *imageL;
85  cv::Mat imageR, imageL;
86  cv::Mat imageProcR, imageProcL;
87  cv::Mat concatenatedImage;
88  // SMC (Sequential Monte Carlo) related variables
89  int nParticles;
90  CvMat* particles;
91  CvMat* newParticles;
92 
93  CvMat* particles1; // Beta 1
94  CvMat* particles2; // Beta 2
95  CvMat* particles3; // Beta 3
96  CvMat* particles4; // Beta 4
97  CvMat* particles5; // Beta 5
98  CvMat* particles6; // Beta 6
99  CvMat* particles7; // Beta 7
100  CvMat* particles8; // Likelihood
101  CvMat* particles1to7; // BETA = [ Beta1, ... , Beta 7]
102  CvMat* newParticles1to7;
103  CvMat* noise;
104  // resampling-related stuff
105  CvMat* cumWeight;
106  CvRNG rngState;
107  /*** Input Ports ***/
108  yarp::os::RpcServer handlerPort;
109  yarp::os::BufferedPort< yarp::sig::ImageOf<yarp::sig::PixelBgr> > imageInputPortR; // Right Image
110  yarp::os::BufferedPort< yarp::sig::ImageOf<yarp::sig::PixelBgr> > imageInputPortL; // Left Image
111  yarp::os::BufferedPort< yarp::os::Bottle > armPort;
112  yarp::os::BufferedPort< yarp::os::Bottle > headPort;
113  yarp::os::BufferedPort< yarp::os::Bottle > likelihoodPort;
114  /*** Output Ports ***/
115  yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelBgr> > LRimageOutputPort;
116  yarp::os::BufferedPort< yarp::os::Bottle > headOutPort;
117  yarp::os::BufferedPort< yarp::os::Bottle > particlesOutPort;
118  yarp::os::BufferedPort< yarp::os::Bottle > offsetsPort;
119  yarp::os::BufferedPort< yarp::os::Bottle > fingers_port;
120  yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelBgr> > outputPortImage;
121  yarp::os::BufferedPort< yarp::sig::ImageOf< yarp::sig::PixelBgr> > outputPortImage2;
122 
123  protected:
128  cv::Mat processImages(cv::Mat inputImage);
133  bool initializeSMCVariables();
138  bool initSMC();
143  bool runSMCIteration();
147  void mergeAndFlipImages();
156  bool readArmJoints();
161  bool readHeadJoints();
166  bool systematic_resampling(CvMat* oldParticlesState, CvMat* oldParticlesWeights, CvMat* newParticlesState, CvMat* cumWeight,float sum2);
167  public:
168  virtual bool configure(yarp::os::ResourceFinder &rf);
169  virtual bool interruptModule();
170  virtual bool close();
171  virtual bool updateModule();
172  virtual double getPeriod();
173 
174  // IDL functions
175 
176  bool attach(yarp::os::RpcServer &source);
177  bool start();
178  bool stop();
179  bool pause();
180  bool resume();
181  yarp::os::Bottle lastOffsets();
182  bool quit();
183 };
184 
185 #endif
bool stop()
Stop the hand pose estimation.
void mergeAndFlipImages()
merge the left and right images.
bool resume()
Resume the hand pose estimation.
bool systematic_resampling(CvMat *oldParticlesState, CvMat *oldParticlesWeights, CvMat *newParticlesState, CvMat *cumWeight, float sum2)
perform the systematic resampling step of the SMC algorithm
bool runSMCIteration()
Run one iteration of the Sequential Monte Carlo Parameters estimation.
Class handPoseEstimationModule.
bool readArmJoints()
Read the arm joints.
bool initializeSMCVariables()
Initialize the variables structs needed for the SMC to run.
bool readHeadJoints()
Read the head joints.
bool initSMC()
Initialize the variables values for the SMC.
bool start()
Start (re-start) the hand pose estimation.
cv::Mat processImages(cv::Mat inputImage)
The function applies a canny edge detector and a distance transform.
bool pause()
Pause the hand pose estimation.
yarp::os::Bottle lastOffsets()
Ask for the last estimated angular Offsets on the arm (7DoF)
handPoseEstimation_IDL IDL Interface to handPoseEstimation-module services.
void kernelDensityEstimation()
Perform the Kernel Density estimation with a multivariate gaussian kernel.