motion_stabilizing.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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_MOTION_STABILIZING_HPP
  43. #define OPENCV_VIDEOSTAB_MOTION_STABILIZING_HPP
  44. #include <vector>
  45. #include <utility>
  46. #include "opencv2/core.hpp"
  47. #include "opencv2/videostab/global_motion.hpp"
  48. namespace cv
  49. {
  50. namespace videostab
  51. {
  52. //! @addtogroup videostab_motion
  53. //! @{
  54. class CV_EXPORTS IMotionStabilizer
  55. {
  56. public:
  57. virtual ~IMotionStabilizer() {}
  58. //! assumes that [0, size-1) is in or equals to [range.first, range.second)
  59. virtual void stabilize(
  60. int size, const std::vector<Mat> &motions, std::pair<int,int> range,
  61. Mat *stabilizationMotions) = 0;
  62. };
  63. class CV_EXPORTS MotionStabilizationPipeline : public IMotionStabilizer
  64. {
  65. public:
  66. void pushBack(Ptr<IMotionStabilizer> stabilizer) { stabilizers_.push_back(stabilizer); }
  67. bool empty() const { return stabilizers_.empty(); }
  68. virtual void stabilize(
  69. int size, const std::vector<Mat> &motions, std::pair<int,int> range,
  70. Mat *stabilizationMotions) CV_OVERRIDE;
  71. private:
  72. std::vector<Ptr<IMotionStabilizer> > stabilizers_;
  73. };
  74. class CV_EXPORTS MotionFilterBase : public IMotionStabilizer
  75. {
  76. public:
  77. virtual ~MotionFilterBase() {}
  78. virtual Mat stabilize(
  79. int idx, const std::vector<Mat> &motions, std::pair<int,int> range) = 0;
  80. virtual void stabilize(
  81. int size, const std::vector<Mat> &motions, std::pair<int,int> range,
  82. Mat *stabilizationMotions) CV_OVERRIDE;
  83. };
  84. class CV_EXPORTS GaussianMotionFilter : public MotionFilterBase
  85. {
  86. public:
  87. GaussianMotionFilter(int radius = 15, float stdev = -1.f);
  88. void setParams(int radius, float stdev = -1.f);
  89. int radius() const { return radius_; }
  90. float stdev() const { return stdev_; }
  91. virtual Mat stabilize(
  92. int idx, const std::vector<Mat> &motions, std::pair<int,int> range) CV_OVERRIDE;
  93. private:
  94. int radius_;
  95. float stdev_;
  96. std::vector<float> weight_;
  97. };
  98. inline GaussianMotionFilter::GaussianMotionFilter(int _radius, float _stdev) { setParams(_radius, _stdev); }
  99. class CV_EXPORTS LpMotionStabilizer : public IMotionStabilizer
  100. {
  101. public:
  102. LpMotionStabilizer(MotionModel model = MM_SIMILARITY);
  103. void setMotionModel(MotionModel val) { model_ = val; }
  104. MotionModel motionModel() const { return model_; }
  105. void setFrameSize(Size val) { frameSize_ = val; }
  106. Size frameSize() const { return frameSize_; }
  107. void setTrimRatio(float val) { trimRatio_ = val; }
  108. float trimRatio() const { return trimRatio_; }
  109. void setWeight1(float val) { w1_ = val; }
  110. float weight1() const { return w1_; }
  111. void setWeight2(float val) { w2_ = val; }
  112. float weight2() const { return w2_; }
  113. void setWeight3(float val) { w3_ = val; }
  114. float weight3() const { return w3_; }
  115. void setWeight4(float val) { w4_ = val; }
  116. float weight4() const { return w4_; }
  117. virtual void stabilize(
  118. int size, const std::vector<Mat> &motions, std::pair<int,int> range,
  119. Mat *stabilizationMotions) CV_OVERRIDE;
  120. private:
  121. MotionModel model_;
  122. Size frameSize_;
  123. float trimRatio_;
  124. float w1_, w2_, w3_, w4_;
  125. std::vector<double> obj_, collb_, colub_;
  126. std::vector<int> rows_, cols_;
  127. std::vector<double> elems_, rowlb_, rowub_;
  128. void set(int row, int col, double coef)
  129. {
  130. rows_.push_back(row);
  131. cols_.push_back(col);
  132. elems_.push_back(coef);
  133. }
  134. };
  135. CV_EXPORTS Mat ensureInclusionConstraint(const Mat &M, Size size, float trimRatio);
  136. CV_EXPORTS float estimateOptimalTrimRatio(const Mat &M, Size size);
  137. //! @}
  138. } // namespace videostab
  139. } // namespace
  140. #endif