stereo-vision
All Data Structures Namespaces Functions Modules Pages
SFM.h
1 /*
2  * Copyright (C) 2015 RobotCub Consortium
3  * Author: Sean Ryan Fanello, Giulia Pasquale
4  * email: sean.fanello@iit.it giulia.pasquale@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.txt
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 
232 #include <cstdio>
233 #include <mutex>
234 #include <condition_variable>
235 #include <string>
236 #include <iostream>
237 #include <fstream>
238 #include <set>
239 #include <deque>
240 
241 #include <yarp/os/all.h>
242 #include <yarp/dev/all.h>
243 #include <yarp/sig/all.h>
244 #include <yarp/math/Math.h>
245 
246 #include <iCub/ctrl/math.h>
247 #include <iCub/iKin/iKinFwd.h>
248 #include <iCub/stereoVision/stereoCamera.h>
249 
250 #include "fastBilateral.hpp"
251 
252 #ifdef USING_GPU
253  #include <iCub/stereoVision/utils.h>
254 #endif
255 
256 #define LEFT 0
257 #define RIGHT 1
258 
259 using namespace std;
260 using namespace yarp::os;
261 using namespace yarp::dev;
262 using namespace yarp::sig;
263 using namespace yarp::math;
264 using namespace iCub::ctrl;
265 using namespace iCub::iKin;
266 
267 
268 class SFM: public yarp::os::RFModule
269 {
270  StereoCamera* stereo;
271  Mat outputDm;
272  Mat leftMat, rightMat;
273 
274 #ifdef USING_GPU
275  /* pointer to the utilities class */
276  Utilities *utils;
277 #endif
278 
279  yarp::os::Port rpc;
280  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > leftImgPort;
281  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > rightImgPort;
282  BufferedPort<ImageOf<PixelRgbFloat> > worldCartPort;
283  BufferedPort<ImageOf<PixelRgbFloat> > worldCylPort;
284  Port handlerPort;
285 
286  BufferedPort<ImageOf<PixelMono> > outDisp;
287  BufferedPort<ImageOf<PixelBgr> > outMatch;
288 
289  BufferedPort<ImageOf<PixelRgb> > outLeftRectImgPort;
290  BufferedPort<ImageOf<PixelRgb> > outRightRectImgPort;
291 
292  int numberOfTrials;
293  string camCalibFile;
294  bool useBestDisp;
295  int uniquenessRatio;
296  int speckleWindowSize;
297  int speckleRange;
298  int numberOfDisparities;
299  int SADWindowSize;
300  int minDisparity;
301  int preFilterCap;
302  int disp12MaxDiff;
303  bool doSFM;
304  bool calibUpdated;
305  double sigmaColorBLF;
306  double sigmaSpaceBLF;
307  bool doBLF;
308  mutex mutexRecalibration;
309  mutex mutexDisp;
310  mutex mtx_calibEndEvent;
311  condition_variable cv_calibEndEvent;
312 
313  PolyDriver headCtrl,gazeCtrl;
314  IEncoders* iencs;
315  IGazeControl* igaze;
316  yarp::sig::Vector eyes0,eyes;
317  int nHeadAxes;
318  Mat HL_root;
319  Mat HR_root;
320  Mat R0,T0;
321 
322  bool loadIntrinsics(yarp::os::ResourceFinder &rf, Mat &KL, Mat &KR, Mat &DistL, Mat &DistR);
323  Mat buildRotTras(const Mat& R, const Mat& T);
324  Matrix getCameraHGazeCtrl(int camera);
325  void convert(Matrix& matrix, Mat& mat);
326  void convert(Mat& mat, Matrix& matrix);
327  void fillWorld3D(ImageOf<PixelRgbFloat> &worldCartImg, ImageOf<PixelRgbFloat> &worldCylImg);
328  void floodFill(const Point &seed,const Point3f &p0, const double dist, set<int> &visited, Bottle &res);
329  bool loadExtrinsics(yarp::os::ResourceFinder& rf, Mat& Ro, Mat& To, yarp::sig::Vector& eyes);
330  bool updateExtrinsics(Mat& Rot, Mat& Tr, yarp::sig::Vector& eyes, const string& groupname);
331  void updateViaGazeCtrl(const bool update);
332  void updateViaKinematics(const yarp::sig::Vector& deyes);
333  bool init;
334 
335 public:
336 
337  bool configure(ResourceFinder &rf);
338  bool close();
339  bool updateModule();
340  double getPeriod();
341  bool interruptModule();
342  bool respond(const Bottle& command, Bottle& reply);
343 
344  void setDispParameters(bool _useBestDisp, int _uniquenessRatio, int _speckleWindowSize,
345  int _speckleRange, int _numberOfDisparities, int _SADWindowSize,
346  int _minDisparity, int _preFilterCap, int _disp12MaxDiff);
347  Point3f get3DPoints(int u, int v, const string &drive="LEFT");
348  Point3f get3DPointsAndDisp(int u, int v, int &uR, int &vR, const string &drive);
349 
350  Point3f get3DPointMatch(double u1, double v1, double u2, double v2, const string &drive="LEFT");
351  Point2f projectPoint(const string &camera, double x, double y, double z);
352 };
The base class defining stereo camera.
Definition: stereoCamera.h:92