motion_estimators.hpp 13 KB

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