stereo-vision
All Data Structures Namespaces Functions Modules Pages
disparityThread.h
1 /*
2  * Copyright (C) 2011 Department of Robotics Brain and Cognitive Sciences - Istituto Italiano di Tecnologia
3  * Authors: Vadim Tikhanoff
4  * email: vadim.tikhanoff@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txtd
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #include <mutex>
20 
21 #include <iCub/stereoVision/stereoCamera.h>
22 #include <yarp/dev/PolyDriver.h>
23 #include <iCub/iKin/iKinFwd.h>
24 #include <yarp/dev/GazeControl.h>
25 
26 #define LEFT 0
27 #define RIGHT 1
28 
29 #ifdef USING_GPU
30  #include <iCub/stereoVision/utils.h>
31 #endif
32 
33 using namespace std;
34 using namespace yarp::os;
35 using namespace yarp::sig;
36 using namespace yarp::dev;
37 using namespace yarp::math;
38 using namespace iCub::ctrl;
39 using namespace iCub::iKin;
40 
47 class DisparityThread : public PeriodicThread
48 {
49 private:
50  StereoCamera *stereo;
51  bool done;
52  bool work;
53  bool init;
54  bool success;
55  bool useCalibrated;
56  bool useHorn;
57  bool updateCamera;
58  bool updateOnce;
59 
60  bool useBestDisp;
61  int uniquenessRatio;
62  int speckleWindowSize;
63  int speckleRange;
64  int numberOfDisparities;
65  int SADWindowSize;
66  int minDisparity;
67  int preFilterCap;
68  int disp12MaxDiff;
69  int widthInit;
70 
71  #ifdef USING_GPU
72  /* pointer to the utilities class */
73  Utilities *utils;
74  #endif
75 
76  yarp::sig::Vector QL;
77  yarp::sig::Vector QR;
78 
79  Matrix yarp_initLeft,yarp_initRight;
80  Matrix yarp_H0;
81  mutex mutexDisp;
82  PolyDriver gazeCtrl;
83  IGazeControl* igaze;
84 
85  iCubEye *LeyeKin;
86  iCubEye *ReyeKin;
87  yarp::dev::PolyDriver polyHead;
88  yarp::dev::IEncoders *posHead;
89  yarp::dev::IControlLimits *HctrlLim;
90  int nHeadAxis;
91 
92  yarp::dev::PolyDriver polyTorso;
93  yarp::dev::IEncoders *posTorso;
94  yarp::dev::IControlLimits *TctrlLim;
95 
96  yarp::sig::Vector eyes0,eyes;
97  Mat HL_root,HR_root;
98  Mat R0,T0;
99 
100  string moduleName;
101  string robotName;
102 
103  Mat buildRotTras(Mat &R, Mat &T);
104  bool loadStereoParameters(yarp::os::ResourceFinder &rf, Mat &KL, Mat &KR, Mat &DistL, Mat &DistR, Mat &Ro, Mat &To);
105  Matrix getCameraHGazeCtrl(int camera);
106  Matrix getCameraH(yarp::sig::Vector &head_angles,yarp::sig::Vector &torso_angles, iCubEye *eyeKin, int camera);
107  void printMatrixYarp(Matrix &A);
108  void convert(Matrix& matrix, Mat& mat);
109  void convert(Mat& mat, Matrix& matrix);
110  void updateViaGazeCtrl(const bool update);
111  void updateViaKinematics(const yarp::sig::Vector& deyes);
112  bool loadExtrinsics(yarp::os::ResourceFinder& rf, Mat& Ro, Mat& To, yarp::sig::Vector& eyes);
113 
114 public:
115  DisparityThread(const string &name, yarp::os::ResourceFinder &rf, bool useHorn=true, bool updateCamera=false, bool rectify=true);
116  ~DisparityThread() { };
117 
118  void setImages(const Mat &left, const Mat &right);
119  void getDisparity(Mat &Disp);
120  Point3f get3DPointMatch(double u1, double v1, double u2, double v2, string drive);
121  void getDisparityFloat(Mat &Disp);
122  void getQMat(Mat &Q);
123  void getMapper(Mat &Mapper);
124  void getRectMatrix(Mat &RL);
125  void triangulate(Point2f &pixel,Point3f &point) ;
126  bool checkDone();
127  void getRootTransformation(Mat & Trans,int eye=LEFT);
128  bool isOpen();
129  void setDispParameters(bool _useBestDisp, int _uniquenessRatio, int _speckleWindowSize,int _speckleRange, int _numberOfDisparities, int _SADWindowSize, int _minDisparity, int _preFilterCap, int _disp12MaxDiff);
130 
131  void updateCamerasOnce();
132  void startUpdate();
133  void stopUpdate();
134 
135  bool threadInit();
136  void threadRelease();
137  void run();
138  void onStop();
139 };
140 
141 
The class defining the disparity computation.
The base class defining stereo camera.
Definition: stereoCamera.h:92