1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11
// For Open Source Computer Vision Library
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.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
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.
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.
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.
43
#ifndef __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
44
#define __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__
46
#include "opencv2/core.hpp"
47
#include "matchers.hpp"
54
//! @addtogroup stitching_rotation
57
/** @brief Rotation estimator base class.
59
It takes features of all images, pairwise matches between all images and estimates rotations of all
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. :
65
class CV_EXPORTS Estimator
68
virtual ~Estimator() {}
70
/** @brief Estimates camera parameters.
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
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); }
83
/** @brief This method must implement camera parameters estimation logic in order to make the wrapper
84
detail::Estimator::operator()_ work.
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
91
virtual bool estimate(const std::vector<ImageFeatures> &features,
92
const std::vector<MatchesInfo> &pairwise_matches,
93
std::vector<CameraParams> &cameras) = 0;
96
/** @brief Homography based rotation estimator.
98
class CV_EXPORTS HomographyBasedEstimator : public Estimator
101
HomographyBasedEstimator(bool is_focals_estimated = false)
102
: is_focals_estimated_(is_focals_estimated) {}
105
virtual bool estimate(const std::vector<ImageFeatures> &features,
106
const std::vector<MatchesInfo> &pairwise_matches,
107
std::vector<CameraParams> &cameras);
109
bool is_focals_estimated_;
112
/** @brief Base class for all camera parameters refinement methods.
114
class CV_EXPORTS BundleAdjusterBase : public Estimator
117
const Mat refinementMask() const { return refinement_mask_.clone(); }
118
void setRefinementMask(const Mat &mask)
120
CV_Assert(mask.type() == CV_8U && mask.size() == Size(3, 3));
121
refinement_mask_ = mask.clone();
124
double confThresh() const { return conf_thresh_; }
125
void setConfThresh(double conf_thresh) { conf_thresh_ = conf_thresh; }
127
TermCriteria termCriteria() { return term_criteria_; }
128
void setTermCriteria(const TermCriteria& term_criteria) { term_criteria_ = term_criteria; }
131
/** @brief Construct a bundle adjuster base instance.
133
@param num_params_per_cam Number of parameters per camera
134
@param num_errs_per_measurement Number of error terms (components) per match
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)
140
setRefinementMask(Mat::ones(3, 3, CV_8U));
142
setTermCriteria(TermCriteria(TermCriteria::EPS + TermCriteria::COUNT, 1000, DBL_EPSILON));
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);
150
/** @brief Sets initial camera parameter to refine.
152
@param cameras Camera parameters
154
virtual void setUpInitialCameraParams(const std::vector<CameraParams> &cameras) = 0;
155
/** @brief Gets the refined camera parameters.
157
@param cameras Refined camera parameters
159
virtual void obtainRefinedCameraParams(std::vector<CameraParams> &cameras) const = 0;
160
/** @brief Calculates error vector.
162
@param err Error column-vector of length total_num_matches \* num_errs_per_measurement
164
virtual void calcError(Mat &err) = 0;
165
/** @brief Calculates the cost function jacobian.
167
@param jac Jacobian matrix of dimensions
168
(total_num_matches \* num_errs_per_measurement) x (num_images \* num_params_per_cam)
170
virtual void calcJacobian(Mat &jac) = 0;
172
// 3x3 8U mask, where 0 means don't refine respective parameter, != 0 means refine
173
Mat refinement_mask_;
176
int total_num_matches_;
178
int num_params_per_cam_;
179
int num_errs_per_measurement_;
181
const ImageFeatures *features_;
182
const MatchesInfo *pairwise_matches_;
184
// Threshold to filter out poorly matched image pairs
187
//Levenberg–Marquardt algorithm termination criteria
188
TermCriteria term_criteria_;
190
// Camera parameters matrix (CV_64F)
193
// Connected images pairs
194
std::vector<std::pair<int,int> > edges_;
198
/** @brief Implementation of the camera parameters refinement algorithm which minimizes sum of the reprojection
201
It can estimate focal length, aspect ratio, principal point.
202
You can affect only on them via the refinement mask.
204
class CV_EXPORTS BundleAdjusterReproj : public BundleAdjusterBase
207
BundleAdjusterReproj() : BundleAdjusterBase(7, 2) {}
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);
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. :
222
It can estimate focal length. It ignores the refinement mask for now.
224
class CV_EXPORTS BundleAdjusterRay : public BundleAdjusterBase
227
BundleAdjusterRay() : BundleAdjusterBase(4, 3) {}
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);
245
/** @brief Tries to make panorama more horizontal (or vertical).
247
@param rmats Camera rotation matrices.
248
@param kind Correction kind, see detail::WaveCorrectKind.
250
void CV_EXPORTS waveCorrect(std::vector<Mat> &rmats, WaveCorrectKind kind);
253
//////////////////////////////////////////////////////////////////////////////
254
// Auxiliary functions
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);
260
std::vector<int> CV_EXPORTS leaveBiggestComponent(
261
std::vector<ImageFeatures> &features,
262
std::vector<MatchesInfo> &pairwise_matches,
263
float conf_threshold);
265
void CV_EXPORTS findMaxSpanningTree(
266
int num_images, const std::vector<MatchesInfo> &pairwise_matches,
267
Graph &span_tree, std::vector<int> ¢ers);
269
//! @} stitching_rotation
271
} // namespace detail
274
#endif // __OPENCV_STITCHING_MOTION_ESTIMATORS_HPP__