global_motion.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 void setFrameMask(InputArray mask)
  153. {
  154. if (!mask.empty())
  155. CV_Error(Error::StsNotImplemented, "Mask support is not implemented.");
  156. }
  157. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) = 0;
  158. protected:
  159. ImageMotionEstimatorBase(MotionModel model) { setMotionModel(model); }
  160. private:
  161. MotionModel motionModel_;
  162. };
  163. class CV_EXPORTS FromFileMotionReader : public ImageMotionEstimatorBase
  164. {
  165. public:
  166. FromFileMotionReader(const String &path);
  167. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
  168. private:
  169. std::ifstream file_;
  170. };
  171. class CV_EXPORTS ToFileMotionWriter : public ImageMotionEstimatorBase
  172. {
  173. public:
  174. ToFileMotionWriter(const String &path, Ptr<ImageMotionEstimatorBase> estimator);
  175. virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
  176. virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
  177. virtual void setFrameMask(InputArray mask) CV_OVERRIDE { motionEstimator_->setFrameMask(mask); }
  178. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
  179. private:
  180. std::ofstream file_;
  181. Ptr<ImageMotionEstimatorBase> motionEstimator_;
  182. };
  183. /** @brief Describes a global 2D motion estimation method which uses keypoints detection and optical flow for
  184. matching.
  185. */
  186. class CV_EXPORTS KeypointBasedMotionEstimator : public ImageMotionEstimatorBase
  187. {
  188. public:
  189. KeypointBasedMotionEstimator(Ptr<MotionEstimatorBase> estimator);
  190. virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
  191. virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
  192. void setDetector(Ptr<FeatureDetector> val) { detector_ = val; }
  193. Ptr<FeatureDetector> detector() const { return detector_; }
  194. void setOpticalFlowEstimator(Ptr<ISparseOptFlowEstimator> val) { optFlowEstimator_ = val; }
  195. Ptr<ISparseOptFlowEstimator> opticalFlowEstimator() const { return optFlowEstimator_; }
  196. void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
  197. Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
  198. virtual void setFrameMask(InputArray mask) CV_OVERRIDE { mask_ = mask.getMat(); }
  199. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
  200. Mat estimate(InputArray frame0, InputArray frame1, bool *ok = 0);
  201. private:
  202. Ptr<MotionEstimatorBase> motionEstimator_;
  203. Ptr<FeatureDetector> detector_;
  204. Ptr<ISparseOptFlowEstimator> optFlowEstimator_;
  205. Ptr<IOutlierRejector> outlierRejector_;
  206. Mat mask_;
  207. std::vector<uchar> status_;
  208. std::vector<KeyPoint> keypointsPrev_;
  209. std::vector<Point2f> pointsPrev_, points_;
  210. std::vector<Point2f> pointsPrevGood_, pointsGood_;
  211. };
  212. #if defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
  213. class CV_EXPORTS KeypointBasedMotionEstimatorGpu : public ImageMotionEstimatorBase
  214. {
  215. public:
  216. KeypointBasedMotionEstimatorGpu(Ptr<MotionEstimatorBase> estimator);
  217. virtual void setMotionModel(MotionModel val) CV_OVERRIDE { motionEstimator_->setMotionModel(val); }
  218. virtual MotionModel motionModel() const CV_OVERRIDE { return motionEstimator_->motionModel(); }
  219. void setOutlierRejector(Ptr<IOutlierRejector> val) { outlierRejector_ = val; }
  220. Ptr<IOutlierRejector> outlierRejector() const { return outlierRejector_; }
  221. virtual Mat estimate(const Mat &frame0, const Mat &frame1, bool *ok = 0) CV_OVERRIDE;
  222. Mat estimate(const cuda::GpuMat &frame0, const cuda::GpuMat &frame1, bool *ok = 0);
  223. private:
  224. Ptr<MotionEstimatorBase> motionEstimator_;
  225. Ptr<cuda::CornersDetector> detector_;
  226. SparsePyrLkOptFlowEstimatorGpu optFlowEstimator_;
  227. Ptr<IOutlierRejector> outlierRejector_;
  228. cuda::GpuMat frame0_, grayFrame0_, frame1_;
  229. cuda::GpuMat pointsPrev_, points_;
  230. cuda::GpuMat status_;
  231. Mat hostPointsPrev_, hostPoints_;
  232. std::vector<Point2f> hostPointsPrevTmp_, hostPointsTmp_;
  233. std::vector<uchar> rejectionStatus_;
  234. };
  235. #endif // defined(HAVE_OPENCV_CUDAIMGPROC) && defined(HAVE_OPENCV_CUDAOPTFLOW)
  236. /** @brief Computes motion between two frames assuming that all the intermediate motions are known.
  237. @param from Source frame index.
  238. @param to Destination frame index.
  239. @param motions Pair-wise motions. motions[i] denotes motion from the frame i to the frame i+1
  240. @return Motion from the Source frame to the Destination frame.
  241. */
  242. CV_EXPORTS Mat getMotion(int from, int to, const std::vector<Mat> &motions);
  243. //! @}
  244. } // namespace videostab
  245. } // namespace cv
  246. #endif