motion_estimators.hpp 12 KB

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