motion_estimators.hpp
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
44 #define __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
45 
46 #include "opencv2/core/core.hpp"
47 #include "matchers.hpp"
48 #include "util.hpp"
49 #include "camera.hpp"
50 
51 namespace cv {
52 namespace detail {
53 
54 class CV_EXPORTS Estimator
55 {
56 public:
57  virtual ~Estimator() {}
58 
59  void operator ()(const std::vector<ImageFeatures> &features, const std::vector<MatchesInfo> &pairwise_matches,
60  std::vector<CameraParams> &cameras)
61  { estimate(features, pairwise_matches, cameras); }
62 
63 protected:
64  virtual void estimate(const std::vector<ImageFeatures> &features, const std::vector<MatchesInfo> &pairwise_matches,
65  std::vector<CameraParams> &cameras) = 0;
66 };
67 
68 
69 class CV_EXPORTS HomographyBasedEstimator : public Estimator
70 {
71 public:
72  HomographyBasedEstimator(bool is_focals_estimated = false)
73  : is_focals_estimated_(is_focals_estimated) {}
74 
75 private:
76  void estimate(const std::vector<ImageFeatures> &features, const std::vector<MatchesInfo> &pairwise_matches,
77  std::vector<CameraParams> &cameras);
78 
79  bool is_focals_estimated_;
80 };
81 
82 
83 class CV_EXPORTS BundleAdjusterBase : public Estimator
84 {
85 public:
86  const Mat refinementMask() const { return refinement_mask_.clone(); }
87  void setRefinementMask(const Mat &mask)
88  {
89  CV_Assert(mask.type() == CV_8U && mask.size() == Size(3, 3));
90  refinement_mask_ = mask.clone();
91  }
92 
93  double confThresh() const { return conf_thresh_; }
94  void setConfThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
95 
96  CvTermCriteria termCriteria() { return term_criteria_; }
97  void setTermCriteria(const CvTermCriteria& term_criteria) { term_criteria_ = term_criteria; }
98 
99 protected:
100  BundleAdjusterBase(int num_params_per_cam, int num_errs_per_measurement)
101  : num_params_per_cam_(num_params_per_cam),
102  num_errs_per_measurement_(num_errs_per_measurement)
103  {
104  setRefinementMask(Mat::ones(3, 3, CV_8U));
105  setConfThresh(1.);
106  setTermCriteria(cvTermCriteria(CV_TERMCRIT_EPS + CV_TERMCRIT_ITER, 1000, DBL_EPSILON));
107  }
108 
109  // Runs bundle adjustment
110  virtual void estimate(const std::vector<ImageFeatures> &features,
111  const std::vector<MatchesInfo> &pairwise_matches,
112  std::vector<CameraParams> &cameras);
113 
114  virtual void setUpInitialCameraParams(const std::vector<CameraParams> &cameras) = 0;
115  virtual void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const = 0;
116  virtual void calcError(Mat &err) = 0;
117  virtual void calcJacobian(Mat &jac) = 0;
118 
119  // 3x3 8U mask, where 0 means don't refine respective parameter, != 0 means refine
121 
124 
127 
130 
131  // Threshold to filter out poorly matched image pairs
132  double conf_thresh_;
133 
134  //Levenberg–Marquardt algorithm termination criteria
136 
137  // Camera parameters matrix (CV_64F)
139 
140  // Connected images pairs
141  std::vector<std::pair<int,int> > edges_;
142 };
143 
144 
145 // Minimizes reprojection error.
146 // It can estimate focal length, aspect ratio, principal point.
147 // You can affect only on them via the refinement mask.
148 class CV_EXPORTS BundleAdjusterReproj : public BundleAdjusterBase
149 {
150 public:
152 
153 private:
154  void setUpInitialCameraParams(const std::vector<CameraParams> &cameras);
155  void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const;
156  void calcError(Mat &err);
157  void calcJacobian(Mat &jac);
158 
159  Mat err1_, err2_;
160 };
161 
162 
163 // Minimizes sun of ray-to-ray distances.
164 // It can estimate focal length. It ignores the refinement mask for now.
165 class CV_EXPORTS BundleAdjusterRay : public BundleAdjusterBase
166 {
167 public:
169 
170 private:
171  void setUpInitialCameraParams(const std::vector<CameraParams> &cameras);
172  void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const;
173  void calcError(Mat &err);
174  void calcJacobian(Mat &jac);
175 
176  Mat err1_, err2_;
177 };
178 
179 
181 {
184 };
185 
186 void CV_EXPORTS waveCorrect(std::vector<Mat> &rmats, WaveCorrectKind kind);
187 
188 
190 // Auxiliary functions
191 
192 // Returns matches graph representation in DOT language
193 std::string CV_EXPORTS matchesGraphAsString(std::vector<std::string> &pathes, std::vector<MatchesInfo> &pairwise_matches,
194  float conf_threshold);
195 
196 std::vector<int> CV_EXPORTS leaveBiggestComponent(std::vector<ImageFeatures> &features, std::vector<MatchesInfo> &pairwise_matches,
197  float conf_threshold);
198 
199 void CV_EXPORTS findMaxSpanningTree(int num_images, const std::vector<MatchesInfo> &pairwise_matches,
200  Graph &span_tree, std::vector<int> &centers);
201 
202 } // namespace detail
203 } // namespace cv
204 
205 #endif // __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
static MatExpr ones(int rows, int cols, int type)
BundleAdjusterReproj()
Definition: motion_estimators.hpp:151
Definition: motion_estimators.hpp:54
void setConfThresh(double conf_thresh)
Definition: motion_estimators.hpp:94
HomographyBasedEstimator(bool is_focals_estimated=false)
Definition: motion_estimators.hpp:72
Mat clone() const
returns deep copy of the matrix, i.e. the data is copied
Definition: mat.hpp:332
std::vector< std::pair< int, int > > edges_
Definition: motion_estimators.hpp:141
Size2i Size
Definition: core.hpp:896
Mat cam_params_
Definition: motion_estimators.hpp:138
const ImageFeatures * features_
Definition: motion_estimators.hpp:128
Definition: motion_estimators.hpp:83
double confThresh() const
Definition: motion_estimators.hpp:93
void CV_EXPORTS findMaxSpanningTree(int num_images, const std::vector< MatchesInfo > &pairwise_matches, Graph &span_tree, std::vector< int > &centers)
Definition: matchers.hpp:59
std::vector< int > CV_EXPORTS leaveBiggestComponent(std::vector< ImageFeatures > &features, std::vector< MatchesInfo > &pairwise_matches, float conf_threshold)
CV_INLINE CvTermCriteria cvTermCriteria(int type, int max_iter, double epsilon)
Definition: types_c.h:1007
int type() const
returns element type, similar to CV_MAT_TYPE(cvmat->type)
Definition: mat.hpp:399
int num_params_per_cam_
Definition: motion_estimators.hpp:125
Definition: matchers.hpp:131
Definition: motion_estimators.hpp:183
int num_images_
Definition: motion_estimators.hpp:122
virtual ~Estimator()
Definition: motion_estimators.hpp:57
CvTermCriteria term_criteria_
Definition: motion_estimators.hpp:135
double conf_thresh_
Definition: motion_estimators.hpp:132
Definition: motion_estimators.hpp:69
Definition: motion_estimators.hpp:182
void setTermCriteria(const CvTermCriteria &term_criteria)
Definition: motion_estimators.hpp:97
The Core Functionality.
BundleAdjusterRay()
Definition: motion_estimators.hpp:168
The n-dimensional matrix class.
Definition: core.hpp:1688
Definition: motion_estimators.hpp:165
CvTermCriteria termCriteria()
Definition: motion_estimators.hpp:96
Definition: types_c.h:997
const Mat refinementMask() const
Definition: motion_estimators.hpp:86
BundleAdjusterBase(int num_params_per_cam, int num_errs_per_measurement)
Definition: motion_estimators.hpp:100
Definition: motion_estimators.hpp:148
int num_errs_per_measurement_
Definition: motion_estimators.hpp:126
const MatchesInfo * pairwise_matches_
Definition: motion_estimators.hpp:129
MSize size
Definition: core.hpp:2006
void CV_EXPORTS waveCorrect(std::vector< Mat > &rmats, WaveCorrectKind kind)
GLenum GLint GLuint mask
Definition: tracking.hpp:132
int total_num_matches_
Definition: motion_estimators.hpp:123
Mat refinement_mask_
Definition: motion_estimators.hpp:120
void setRefinementMask(const Mat &mask)
Definition: motion_estimators.hpp:87
WaveCorrectKind
Definition: motion_estimators.hpp:180
std::string CV_EXPORTS matchesGraphAsString(std::vector< std::string > &pathes, std::vector< MatchesInfo > &pairwise_matches, float conf_threshold)