global_motion.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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-2011, 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_VIDEOSTAB_GLOBAL_MOTION_HPP
  43. #define OPENCV_VIDEOSTAB_GLOBAL_MOTION_HPP
  44. #include <vector>
  45. #include <fstream>
  46. #include "opencv2/core.hpp"
  47. #include "opencv2/features2d.hpp"
  48. #include "opencv2/opencv_modules.hpp"
  49. #include "opencv2/videostab/optical_flow.hpp"
  50. #include "opencv2/videostab/motion_core.hpp"
  51. #include "opencv2/videostab/outlier_rejection.hpp"
  52. #ifdef HAVE_OPENCV_CUDAIMGPROC
  53. # include "opencv2/cudaimgproc.hpp"
  54. #endif
  55. namespace cv
  56. {
  57. namespace videostab
  58. {
  59. //! @addtogroup videostab_motion
  60. //! @{
  61. /** @brief Estimates best global motion between two 2D point clouds in the least-squares sense.
  62. @note Works in-place and changes input point arrays.
  63. @param points0 Source set of 2D points (32F).
  64. @param points1 Destination set of 2D points (32F).
  65. @param model Motion model (up to MM_AFFINE).
  66. @param rmse Final root-mean-square error.
  67. @return 3x3 2D transformation matrix (32F).
  68. */
  69. CV_EXPORTS Mat estimateGlobalMotionLeastSquares(
  70. InputOutputArray points0, InputOutputArray points1, int model = MM_AFFINE,
  71. float *rmse = 0);
  72. /** @brief Estimates best global motion between two 2D point clouds robustly (using RANSAC method).
  73. @param points0 Source set of 2D points (32F).
  74. @param points1 Destination set of 2D points (32F).
  75. @param model Motion model. See cv::videostab::MotionModel.
  76. @param params RANSAC method parameters. See videostab::RansacParams.
  77. @param rmse Final root-mean-square error.
  78. @param ninliers Final number of inliers.
  79. */
  80. CV_EXPORTS Mat estimateGlobalMotionRansac(
  81. InputArray points0, InputArray points1, int model = MM_AFFINE,
  82. const RansacParams &params = RansacParams::default2dMotion(MM_AFFINE),
  83. float *rmse = 0, int *ninliers = 0);
  84. /** @brief Base class for all global motion estimation methods.
  85. */
  86. class CV_EXPORTS MotionEstimatorBase
  87. {
  88. public:
  89. virtual ~MotionEstimatorBase() {}
  90. /** @brief Sets motion model.
  91. @param val Motion model. See cv::videostab::MotionModel.
  92. */
  93. virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
  94. /**
  95. @return Motion model. See cv::videostab::MotionModel.
  96. */
  97. virtual MotionModel motionModel() const { return motionModel_; }
  98. /** @brief Estimates global motion between two 2D point clouds.
  99. @param points0 Source set of 2D points (32F).
  100. @param points1 Destination set of 2D points (32F).
  101. @param ok Indicates whether motion was estimated successfully.
  102. @return 3x3 2D transformation matrix (32F).
  103. */
  104. virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) = 0;
  105. protected:
  106. MotionEstimatorBase(MotionModel model) { setMotionModel(model); }
  107. private:
  108. MotionModel motionModel_;
  109. };
  110. /** @brief Describes a robust RANSAC-based global 2D motion estimation method which minimizes L2 error.
  111. */
  112. class CV_EXPORTS MotionEstimatorRansacL2 : public MotionEstimatorBase
  113. {
  114. public:
  115. MotionEstimatorRansacL2(MotionModel model = MM_AFFINE);
  116. void setRansacParams(const RansacParams &val) { ransacParams_ = val; }
  117. RansacParams ransacParams() const { return ransacParams_; }
  118. void setMinInlierRatio(float val) { minInlierRatio_ = val; }
  119. float minInlierRatio() const { return minInlierRatio_; }
  120. virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) CV_OVERRIDE;
  121. private:
  122. RansacParams ransacParams_;
  123. float minInlierRatio_;
  124. };
  125. /** @brief Describes a global 2D motion estimation method which minimizes L1 error.
  126. @note To be able to use this method you must build OpenCV with CLP library support. :
  127. */
  128. class CV_EXPORTS MotionEstimatorL1 : public MotionEstimatorBase
  129. {
  130. public:
  131. MotionEstimatorL1(MotionModel model = MM_AFFINE);
  132. virtual Mat estimate(InputArray points0, InputArray points1, bool *ok = 0) CV_OVERRIDE;
  133. private:
  134. std::vector<double> obj_, collb_, colub_;
  135. std::vector<double> elems_, rowlb_, rowub_;
  136. std::vector<int> rows_, cols_;
  137. void set(int row, int col, double coef)
  138. {
  139. rows_.push_back(row);
  140. cols_.push_back(col);
  141. elems_.push_back(coef);
  142. }
  143. };
  144. /** @brief Base class for global 2D motion estimation methods which take frames as input.
  145. */
  146. class CV_EXPORTS ImageMotionEstimatorBase
  147. {
  148. public:
  149. virtual ~ImageMotionEstimatorBase() {}
  150. virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
  151. virtual MotionModel motionModel() const { return motionModel_; }
  152. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
  153. protected:
  154. ImageMotionEstimatorBase(MotionModel model) { setMotionModel(model); }
  155. private:
  156. MotionModel motionModel_;
  157. };
  158. class CV_EXPORTS FromFileMotionReader : public ImageMotionEstimatorBase
  159. {
  160. public:
  161. FromFileMotionReader(const String &path);
  162. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
  163. private:
  164. std::ifstream file_;
  165. };
  166. class CV_EXPORTS ToFileMotionWriter : public ImageMotionEstimatorBase
  167. {
  168. public:
  169. ToFileMotionWriter(const String &path, Ptr<ImageMotionEstimatorBase> estimator);
  170. virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
  171. virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
  172. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
  173. private:
  174. std::ofstream file_;
  175. Ptr<ImageMotionEstimatorBase> motionEstimator_;
  176. };
  177. /** @brief Describes a global 2D motion estimation method which uses keypoints detection and optical flow for
  178. matching.
  179. */
  180. class CV_EXPORTS KeypointBasedMotionEstimator : public ImageMotionEstimatorBase
  181. {
  182. public:
  183. KeypointBasedMotionEstimator(Ptr<MotionEstimatorBase> estimator);
  184. virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
  185. virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
  186. void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
  187. Ptr<FeatureDetector> detector() const { return detector_; }
  188. void setOpticalFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
  189. Ptr<ISparseOptFlowEstimator> opticalFlowEstimator() const { return optFlowEstimator_; }
  190. void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
  191. Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
  192. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
  193. Mat estimate(InputArray frame0, InputArray frame1, bool *ok = 0);
  194. private:
  195. Ptr<MotionEstimatorBase> motionEstimator_;
  196. Ptr<FeatureDetector> detector_;
  197. Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
  198. Ptr<IOutlierRejector> outlierRejector_;
  199. std::vector<uchar> status_;
  200. std::vector<KeyPoint> keypointsPrev_;
  201. std::vector<Point2f> pointsPrev_, points_;
  202. std::vector<Point2f> pointsPrevGood_, pointsGood_;
  203. };
  204. #if defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
  205. class CV_EXPORTS KeypointBasedMotionEstimatorGpu : public ImageMotionEstimatorBase
  206. {
  207. public:
  208. KeypointBasedMotionEstimatorGpu(Ptr<MotionEstimatorBase> estimator);
  209. virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
  210. virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
  211. void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
  212. Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
  213. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
  214. Mat estimate(const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, bool *ok = 0);
  215. private:
  216. Ptr<MotionEstimatorBase> motionEstimator_;
  217. Ptr<cuda::CornersDetector> detector_;
  218. SparsePyrLkOptFlowEstimatorGpu optFlowEstimator_;
  219. Ptr<IOutlierRejector> outlierRejector_;
  220. cuda::GpuMat frame0_, grayFrame0_, frame1_;
  221. cuda::GpuMat pointsPrev_, points_;
  222. cuda::GpuMat status_;
  223. Mat hostPointsPrev_, hostPoints_;
  224. std::vector<Point2f> hostPointsPrevTmp_, hostPointsTmp_;
  225. std::vector<uchar> rejectionStatus_;
  226. };
  227. #endif // defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
  228. /** @brief Computes motion between two frames assuming that all the intermediate motions are known.
  229. @param from Source frame index.
  230. @param to Destination frame index.
  231. @param motions Pair-wise motions. motions[i] denotes motion from the frame i to the frame i+1
  232. @return Motion from the Source frame to the Destination frame.
  233. */
  234. CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
  235. //! @}
  236. } // namespace videostab
  237. } // namespace cv
  238. #endif