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.hpp"
47 #include "matchers.hpp"
48 #include "util.hpp"
49 #include "camera.hpp"
50 
51 namespace cv {
52 namespace detail {
53 
54 //! @addtogroup stitching_rotation
55 //! @{
56 
57 /** @brief Rotation estimator base class.
58 
59 It takes features of all images, pairwise matches between all images and estimates rotations of all
60 cameras.
61 
62 @note The coordinate system origin is implementation-dependent, but you can always normalize the
63 rotations in respect to the first camera, for instance. :
64  */
65 class CV_EXPORTS Estimator
66 {
67 public:
~Estimator()68     virtual ~Estimator() {}
69 
70     /** @brief Estimates camera parameters.
71 
72     @param features Features of images
73     @param pairwise_matches Pairwise matches of images
74     @param cameras Estimated camera parameters
75     @return True in case of success, false otherwise
76      */
operator ()(const std::vector<ImageFeatures> & features,const std::vector<MatchesInfo> & pairwise_matches,std::vector<CameraParams> & cameras)77     bool operator ()(const std::vector<ImageFeatures> &features,
78                      const std::vector<MatchesInfo> &pairwise_matches,
79                      std::vector<CameraParams> &cameras)
80         { return estimate(features, pairwise_matches, cameras); }
81 
82 protected:
83     /** @brief This method must implement camera parameters estimation logic in order to make the wrapper
84     detail::Estimator::operator()_ work.
85 
86     @param features Features of images
87     @param pairwise_matches Pairwise matches of images
88     @param cameras Estimated camera parameters
89     @return True in case of success, false otherwise
90      */
91     virtual bool estimate(const std::vector<ImageFeatures> &features,
92                           const std::vector<MatchesInfo> &pairwise_matches,
93                           std::vector<CameraParams> &cameras) = 0;
94 };
95 
96 /** @brief Homography based rotation estimator.
97  */
98 class CV_EXPORTS HomographyBasedEstimator : public Estimator
99 {
100 public:
HomographyBasedEstimator(bool is_focals_estimated=false)101     HomographyBasedEstimator(bool is_focals_estimated = false)
102         : is_focals_estimated_(is_focals_estimated) {}
103 
104 private:
105     virtual bool estimate(const std::vector<ImageFeatures> &features,
106                           const std::vector<MatchesInfo> &pairwise_matches,
107                           std::vector<CameraParams> &cameras);
108 
109     bool is_focals_estimated_;
110 };
111 
112 /** @brief Base class for all camera parameters refinement methods.
113  */
114 class CV_EXPORTS BundleAdjusterBase : public Estimator
115 {
116 public:
refinementMask() const117     const Mat refinementMask() const { return refinement_mask_.clone(); }
setRefinementMask(const Mat & mask)118     void setRefinementMask(const Mat &mask)
119     {
120         CV_Assert(mask.type() == CV_8U && mask.size() == Size(3, 3));
121         refinement_mask_ = mask.clone();
122     }
123 
confThresh() const124     double confThresh() const { return conf_thresh_; }
setConfThresh(double conf_thresh)125     void setConfThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
126 
termCriteria()127     TermCriteria termCriteria() { return term_criteria_; }
setTermCriteria(const TermCriteria & term_criteria)128     void setTermCriteria(const TermCriteria& term_criteria) { term_criteria_ = term_criteria; }
129 
130 protected:
131     /** @brief Construct a bundle adjuster base instance.
132 
133     @param num_params_per_cam Number of parameters per camera
134     @param num_errs_per_measurement Number of error terms (components) per match
135      */
BundleAdjusterBase(int num_params_per_cam,int num_errs_per_measurement)136     BundleAdjusterBase(int num_params_per_cam, int num_errs_per_measurement)
137         : num_params_per_cam_(num_params_per_cam),
138           num_errs_per_measurement_(num_errs_per_measurement)
139     {
140         setRefinementMask(Mat::ones(3, 3, CV_8U));
141         setConfThresh(1.);
142         setTermCriteria(TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 1000, DBL_EPSILON));
143     }
144 
145     // Runs bundle adjustment
146     virtual bool estimate(const std::vector<ImageFeatures> &features,
147                           const std::vector<MatchesInfo> &pairwise_matches,
148                           std::vector<CameraParams> &cameras);
149 
150     /** @brief Sets initial camera parameter to refine.
151 
152     @param cameras Camera parameters
153      */
154     virtual void setUpInitialCameraParams(const std::vector<CameraParams> &cameras) = 0;
155     /** @brief Gets the refined camera parameters.
156 
157     @param cameras Refined camera parameters
158      */
159     virtual void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const = 0;
160     /** @brief Calculates error vector.
161 
162     @param err Error column-vector of length total_num_matches \* num_errs_per_measurement
163      */
164     virtual void calcError(Mat &err) = 0;
165     /** @brief Calculates the cost function jacobian.
166 
167     @param jac Jacobian matrix of dimensions
168     (total_num_matches \* num_errs_per_measurement) x (num_images \* num_params_per_cam)
169      */
170     virtual void calcJacobian(Mat &jac) = 0;
171 
172     // 3x3 8U mask, where 0 means don't refine respective parameter, != 0 means refine
173     Mat refinement_mask_;
174 
175     int num_images_;
176     int total_num_matches_;
177 
178     int num_params_per_cam_;
179     int num_errs_per_measurement_;
180 
181     const ImageFeatures *features_;
182     const MatchesInfo *pairwise_matches_;
183 
184     // Threshold to filter out poorly matched image pairs
185     double conf_thresh_;
186 
187     //Levenberg–Marquardt algorithm termination criteria
188     TermCriteria term_criteria_;
189 
190     // Camera parameters matrix (CV_64F)
191     Mat cam_params_;
192 
193     // Connected images pairs
194     std::vector<std::pair<int,int> > edges_;
195 };
196 
197 
198 /** @brief Implementation of the camera parameters refinement algorithm which minimizes sum of the reprojection
199 error squares
200 
201 It can estimate focal length, aspect ratio, principal point.
202 You can affect only on them via the refinement mask.
203  */
204 class CV_EXPORTS BundleAdjusterReproj : public BundleAdjusterBase
205 {
206 public:
BundleAdjusterReproj()207     BundleAdjusterReproj() : BundleAdjusterBase(7, 2) {}
208 
209 private:
210     void setUpInitialCameraParams(const std::vector<CameraParams> &cameras);
211     void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const;
212     void calcError(Mat &err);
213     void calcJacobian(Mat &jac);
214 
215     Mat err1_, err2_;
216 };
217 
218 
219 /** @brief Implementation of the camera parameters refinement algorithm which minimizes sum of the distances
220 between the rays passing through the camera center and a feature. :
221 
222 It can estimate focal length. It ignores the refinement mask for now.
223  */
224 class CV_EXPORTS BundleAdjusterRay : public BundleAdjusterBase
225 {
226 public:
BundleAdjusterRay()227     BundleAdjusterRay() : BundleAdjusterBase(4, 3) {}
228 
229 private:
230     void setUpInitialCameraParams(const std::vector<CameraParams> &cameras);
231     void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const;
232     void calcError(Mat &err);
233     void calcJacobian(Mat &jac);
234 
235     Mat err1_, err2_;
236 };
237 
238 
239 enum WaveCorrectKind
240 {
241     WAVE_CORRECT_HORIZ,
242     WAVE_CORRECT_VERT
243 };
244 
245 /** @brief Tries to make panorama more horizontal (or vertical).
246 
247 @param rmats Camera rotation matrices.
248 @param kind Correction kind, see detail::WaveCorrectKind.
249  */
250 void CV_EXPORTS waveCorrect(std::vector<Mat> &rmats, WaveCorrectKind kind);
251 
252 
253 //////////////////////////////////////////////////////////////////////////////
254 // Auxiliary functions
255 
256 // Returns matches graph representation in DOT language
257 String CV_EXPORTS matchesGraphAsString(std::vector<String> &pathes, std::vector<MatchesInfo> &pairwise_matches,
258                                             float conf_threshold);
259 
260 std::vector<int> CV_EXPORTS leaveBiggestComponent(
261         std::vector<ImageFeatures> &features,
262         std::vector<MatchesInfo> &pairwise_matches,
263         float conf_threshold);
264 
265 void CV_EXPORTS findMaxSpanningTree(
266         int num_images, const std::vector<MatchesInfo> &pairwise_matches,
267         Graph &span_tree, std::vector<int> &centers);
268 
269 //! @} stitching_rotation
270 
271 } // namespace detail
272 } // namespace cv
273 
274 #endif // __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
275