stereo-vision
All Data Structures Namespaces Functions Modules Pages
stereoCamera.h
1 /*
2  * Copyright (C) 2010 RobotCub Consortium, European Commission FP6 Project IST-004370
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 
38 #ifndef _ICUB_STEREOVISION_STEREOCAMERA_H_
39 #define _ICUB_STEREOVISION_STEREOCAMERA_H_
40 
41 #include <iostream>
42 #include <fstream>
43 #include <mutex>
44 #include <string>
45 #include <vector>
46 
47 #include <opencv2/opencv.hpp>
48 
49 #include <iCub/stereoVision/camera.h>
50 #include <iCub/stereoVision/elasWrapper.h>
51 
52 #include <yarp/os/all.h>
53 
54 //using namespace cv::ximgproc;
55 
56 #define LEFT 0
57 #define RIGHT 1
58 
59 using namespace std;
60 using namespace yarp::os;
61 
62 
63 using cv::Mat;
64 using cv::Point2f;
65 using cv::Point3f;
66 using cv::Size;
67 using cv::Ptr;
68 using cv::FileStorage;
69 using cv::DMatch;
70 using cv::FileNode;
71 using cv::FeatureDetector;
72 using cv::DescriptorMatcher;
73 using cv::KeyPoint;
74 using cv::StereoSGBM;
75 using cv::StereoBM;
76 using cv::Rect;
77 using cv::SVD;
78 using cv::Vec3f;
79 using cv::Range;
80 using cv::Scalar;
81 using cv::TermCriteria;
92 {
93 private:
94  // stored Images
95  Mat imleft; // Left Image
96  Mat imleftund; // Undistorted Image
97  Mat imright; // Right Image
98  Mat imrightund; // Undistorted Image Right
99  Mat Disparity; // Disparity Map Image
100  Mat Disparity16; // Disparity 16 Bit Signed
101  Mat imgLeftRect; // Rectified Left Image
102  Mat imgRightRect; // Rectified Right Image
103 
104  // stored Matrices
105  Mat Pleft; // Camera Matrix Left 3x4
106  Mat Pright; // Camera Matrix Right 3x4
107  Mat Kleft; // Intrinsic Parameters Left 3x3
108  Mat Kright; // Intrinsic Parameters Right 3x3
109  Mat DistL; // Distortion Coefficients Left 4x1
110  Mat DistR; // Distortion Coefficients Right 4x1
111  Mat Rinit; // Initial Rotation 3x3
112  Mat Tinit; // Initial Translation 3x3
113  Mat R; // Rotation from Left to Right 3x3
114  Mat T; // Translation from Left to Right 3x1
115  Mat R_exp; // Expected Rotation
116  Mat T_exp; // Expected Trans
117  Mat Pleft_exp; // Expected Camera Matrix left
118  Mat Pright_exp; // Expected Camera Matrix right
119  Mat Q; // Depth Matrix 4x4
120  Mat F; // Fundamental Matrix 3x3
121  Mat E; // Essential Matrix 3x3
122  Mat RLrect; // Rotation from Left Camera to Rectified Left Camera 3x3
123  Mat RRrect; // Rotation from Right Camera to Rectified Right Camera 3x3
124  Mat PLrect; // Rectified Left Camera Matrix
125  Mat PRrect; // Rectified Right Camera Matrix
126  Mat map11; //Mapping from from rectified to original
127  Mat map12; //Mapping from from rectified to original
128  Mat map21; //Mapping from from rectified to original
129  Mat map22; //Mapping from from rectified to original
130  Mat mapxL; //Mapping from Undistorted to Original Left image for the x coordinates
131  Mat mapyL; //Mapping from Undistorted to Original Left image for the y coordinates
132  Mat mapxR; //Mapping from Undistorted to Original Right image for the x coordinates
133  Mat mapyR; //Mapping from Undistorted to Original Right image for the y coordinates
134 
135  Mat MapperL; // pixels mapping from original left camera to rectified left camera
136  Mat MapperR; // pixels mapping from original right camera to rectified right camera
137 
138  vector<Point2f> PointsL; // Match Left
139  vector<Point2f> PointsR; // Match Right
140 
141  vector<Point2f> InliersL; // Inliers Left
142  vector<Point2f> InliersR; // Inliers Right
143 
144  double epipolarTh; //threshold for the constraint x'Fx=0 -> x'Fx<epipolarTh
145 
146  mutex mtx;
147  bool cameraChanged;
148  bool rectify;
149  bool readStringList( const string& filename, vector<string>& l );
150  void runStereoCalib(const vector<string>& imagelist, Size boardSize,float sqsize);
151  double* reprojectionError(Mat& Rot, Mat& Tras);
152  void crossCheckMatching( cv::BFMatcher &descriptorMatcher, const Mat& descriptors1, const Mat& descriptors2, vector<DMatch>& filteredMatches12, double radius);
153  void updatePMatrix();
154  void stereoCalibration(string imagesFilePath, int boardWidth, int boardHeight,float sqsize);
155  void normalizePoints(Mat & K1, Mat & K2, vector<Point2f> & PointsL, vector<Point2f> & PointsR);
156  void getRotation(Mat & q, Mat & R);
157  void printMatrix(Mat &matrix);
158  double reprojectionErrorAvg();
159  void savePoints(string pointsLPath, string pointsRPath, vector<Point2f> PointL, vector<Point2f> PointR);
160 
161  Mat buildRotTras(Mat & R, Mat & T);
162  void buildUndistortRemap();
163  void printStereoIntrinsic();
164  void printExtrinsic();
165  bool loadStereoParameters(yarp::os::ResourceFinder &rf, Mat &KL, Mat &KR, Mat &DistL, Mat &DistR, Mat &Ro, Mat &T);
166  void updateExpectedCameraMatrices();
167 
168 
169 
170  bool use_elas;
171  elasWrapper* elaswrap;
172 
173  // DEBUG
174  int debug_timings[10];
175  int debug_count;
176 
177  cv::Mat right_disp, right_disp16;
178 
179 
180 
181 public:
182 
183  Mat filtered_disp;
184 
189  StereoCamera(bool rectify=true);
190 
191  ~StereoCamera() { }
192 
197  StereoCamera(yarp::os::ResourceFinder &rf,bool rectify=true);
198 
206  StereoCamera(Camera First, Camera Second,bool rectify=true);
207 
213  void initELAS(yarp::os::ResourceFinder &rf);
214 
223  void stereoCalibration(vector<string> imageList, int boardWidth, int boardHeight, float sqsize=1.0);
224 
225 
231  void saveCalibration(string extrinsicFilePath, string intrinsicFilePath);
232 
238  void setImages(const Mat &firstImg, const Mat &secondImg);
239 
247  cv::Mat findMatch(bool visualize=false, double displacement=20.0, double radius=200.0);
248 
256  void computeDisparity(bool best=true, int uniquenessRatio=15, int speckleWindowSize=50,int speckleRange=16, int numberOfDisparities=64, int SADWindowSize=7, int minDisparity=0, int preFilterCap=63, int disp12MaxDiff=0);
257 
261  void undistortImages();
262 
273  void horn(Mat &K1,Mat &K2, vector<Point2f> &Points1,vector<Point2f> &Points2,Mat &Rot,Mat &Tras);
274 
279  void hornRelativeOrientations();
280 
288  Point3f triangulation(Point2f& point1, Point2f& point2);
289 
299  Point3f triangulation(Point2f& point1, Point2f& point2, Mat Camera1, Mat Camera2);
300 
310  Point3f triangulationLS(Point2f& point1, Point2f& point2, Mat Camera1, Mat Camera2);
311 
318  Point3f metricTriangulation(Point2f &point1,double thMeters=10);
319 
327  Point3f metricTriangulation(Point2f &point1, Mat &H, double thMeters=10);
328 
338  Point3f triangulateKnownDisparity(float u, float v, float d, Mat &H);
339 
343  void estimateEssential();
344 
348  bool essentialDecomposition();
349 
364  void chierality( Mat& R1, Mat& R2, Mat& t1, Mat& t2, Mat& R, Mat& t, vector<Point2f> points1, vector<Point2f> points2);
365 
370  const Mat& getImLeft() const;
371 
376  const Mat& getImRight() const;
377 
382  const Mat& getImLeftUnd() const;
383 
388  const Mat& getImRightUnd() const;
389 
394  const Mat& getDisparity() const;
395 
400  const Mat& getDisparity16() const;
401 
406  const Mat& getQ() const;
407 
412  const Mat& getKleft() const;
413 
418  const Mat& getKright() const;
419 
424  const Mat& getFundamental() const;
425 
430  const vector<Point2f>& getMatchLeft() const;
431 
436  const vector<Point2f>& getMatchRight() const;
437 
442  const Mat& getTranslation() const;
443 
448  const Mat& getRotation() const;
449 
454  const Mat& getMapperL() const;
455 
460  const Mat& getMapperR() const;
461 
466  const Mat& getRLrect() const;
467 
472  const Mat& getRRrect() const;
473 
482  void setRotation(Mat& Rot, int mode=0);
483 
492  void setTranslation(Mat& Tras, int mul=0);
493 
501  void setIntrinsics(Mat& K1, Mat& K2, Mat& Dist1, Mat& Dist2);
502 
507  void rectifyImages();
508 
513  const Mat& getLRectified() const;
514 
519  const Mat& getRRectified() const;
520 
528  vector<Point2f> projectPoints3D(string camera, vector<Point3f> &points3D, Mat &H);
529 
535  Mat computeWorldImage(Mat &H);
536 
541  const Mat& getDistCoeffRight() const;
542 
547  const Mat& getDistCoeffLeft() const;
548 
556  Point2f getDistortedPixel(int u, int v, int cam=1);
557 
562  Mat drawMatches();
563 
569  void setMatches(std::vector<cv::Point2f> & pointsL, std::vector<cv::Point2f> & pointsR);
570 
576  void setExpectedPosition(Mat &Rot, Mat &Tran);
577 
584  Mat FfromP(Mat& P1, Mat& P2);
585 
593  Point2f fromRectifiedToOriginal(int u, int v, int camera);
594 
600  cv::Mat remapDisparity(cv::Mat disp);
601 
606  void updateMappings();
607 
608 };
609 
610 #endif // _ICUB_STEREOVISION_STEREOCAMERA_H_
The base class defining a simple camera.
Definition: camera.h:41
The base class defining stereo camera.
Definition: stereoCamera.h:92
Point3f triangulationLS(Point2f &point1, Point2f &point2, Mat Camera1, Mat Camera2)
It performs the least square triangulation (HZ Chap 12.2 Inhomogenous solution).