stereo-vision
All Data Structures Namespaces Functions Modules Pages
DispModule.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 <string>
234 #include <iostream>
235 #include <fstream>
236 #include <set>
237 #include <deque>
238 #include <mutex>
239 
240 #include <yarp/os/all.h>
241 #include <yarp/dev/all.h>
242 #include <yarp/sig/all.h>
243 #include <yarp/math/Math.h>
244 
245 #include <iCub/ctrl/math.h>
246 #include <iCub/iKin/iKinFwd.h>
247 #include <iCub/stereoVision/stereoCamera.h>
248 
249 
250 #ifdef USE_GUI
251  #include "cvgui.h"
252 #endif
253 
254 #include "common.h"
255 #include "StereoMatcher.h"
256 
257 #ifdef USING_GPU
258  #include <iCub/stereoVision/utils.h>
259 #endif
260 
261 #define LEFT 0
262 #define RIGHT 1
263 
264 using namespace std;
265 using namespace yarp::os;
266 using namespace yarp::dev;
267 using namespace yarp::sig;
268 using namespace yarp::math;
269 using namespace iCub::ctrl;
270 using namespace iCub::iKin;
271 
272 using namespace cv::ximgproc;
273 
274 class DispModule: public yarp::os::RFModule
275 {
276  StereoCamera* stereo;
277  Mat outputDm, outputDepth;
278  Mat leftMat, rightMat;
279 
280 #ifdef USING_GPU
281  /* pointer to the utilities class */
282  Utilities *utils;
283 #endif
284 
285  // input YARP ports
286  yarp::os::Port rpc;
287  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > leftImgPort;
288  yarp::os::BufferedPort<yarp::sig::ImageOf<yarp::sig::PixelRgb> > rightImgPort;
289  Port handlerPort;
290 
291  // output YARP ports
292  BufferedPort<ImageOf<PixelFloat> > outDepth;
293  BufferedPort<ImageOf<PixelMono> > outDisp;
294 
295  // number of trials to consider when recalibrating the system
296  int numberOfTrials;
297 
298  // path of the camCalib configuration file
299  string camCalibFile;
300 
301  // stereo matching and calibration auxiliary parameters
302  bool useBestDisp;
303  bool runRecalibration;
304  bool calibUpdated;
305  bool debugWindow;
306  bool doBLF;
307 
308  // structures meant to hold the stereo parameters
309  Params stereo_parameters, original_parameters;
310 
311  // mutex and other handles needed by the module
312  std::mutex mutexRecalibration;
313  Event calibEndEvent;
314  std::mutex mutexDisp;
315 
316  ResourceFinder localCalibration;
317 
318  // objects to interface with the robot's state
319  PolyDriver headCtrl,gazeCtrl;
320  IEncoders* iencs;
321  IGazeControl* igaze;
322  yarp::sig::Vector eyes0,eyes;
323  int nHeadAxes;
324  Mat HL_root;
325  Mat HR_root;
326  Mat R0,T0;
327 
328  // objects used in the refinement of the disparity map
329  cv::Mat old_d, old_de;
330  cv::Mat orig;
331 
332  bool init;
333 
334  // parameters for the CUDA implementation of SGBM
335  SGM_PARAMS cuda_params, params_right;
336 
337  // object handling the stereo matching algorithms
338  StereoMatcherNew * matcher;
339 
350  bool loadIntrinsics(yarp::os::ResourceFinder &rf, Mat &KL, Mat &KR, Mat &DistL, Mat &DistR);
351 
352 
362  bool loadConfigurationFile(yarp::os::ResourceFinder& rf, Mat& Ro, Mat& To, yarp::sig::Vector& eyes);
363 
373  // bool loadStereoParameters(yarp::os::ResourceFinder& rf, Mat& Ro, Mat& To, yarp::sig::Vector& eyes);
374 
382  Mat buildRotTras(const Mat& R, const Mat& T);
383 
384  Matrix getCameraHGazeCtrl(int camera);
385 
392  void convert(Matrix& matrix, Mat& mat);
393 
400  void convert(Mat& mat, Matrix& matrix);
401 
411  bool loadExtrinsics(yarp::os::ResourceFinder& rf, Mat& Ro, Mat& To, yarp::sig::Vector& eyes);
412 
422  bool updateExtrinsics(Mat& Rot, Mat& Tr, yarp::sig::Vector& eyes, const string& groupname);
423 
433  bool updateConfigurationFile(Mat& Rot, Mat& Tr, yarp::sig::Vector& eyes, const string& groupname);
434 
439  void updateViaGazeCtrl(const bool update);
440 
446  void updateViaKinematics(const yarp::sig::Vector& deyes);
447 
452  void initializeStereoParams();
453 
462  Mat depthFromDisparity(Mat disp, Mat Q, Mat R);
463 
468  void handleGuiUpdate();
469 
474  void recalibrate();
475 
476 #ifdef USE_GUI
477  GUI gui;
478 #endif
479 
480 public:
481 
482  // module methods
483 
484  bool configure(ResourceFinder &rf);
485  bool close();
486  bool updateModule();
487  double getPeriod();
488  bool interruptModule();
489  bool respond(const Bottle& command, Bottle& reply);
490 
499  void setDispParameters(bool _useBestDisp, int _uniquenessRatio, int _speckleWindowSize,
500  int _speckleRange, int _numberOfDisparities, int _SADWindowSize,
501  int _minDisparity, int _preFilterCap, int _disp12MaxDiff);
502 
511  Point3f get3DPoints(int u, int v, const string &drive="LEFT");
512 
522  Point2f projectPoint(const string &camera, double x, double y, double z);
523 
524  DispModule();
525  ~DispModule();
526 
531  void printParameters();
532 
540  cv::Mat refineDisparity(cv::Mat old_disp, cv::Mat new_disp, int th);
541 
542 };
The base class defining stereo camera.
Definition: stereoCamera.h:92