stabilizer.hpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_STABILIZER_HPP
  43. #define OPENCV_VIDEOSTAB_STABILIZER_HPP
  44. #include <vector>
  45. #include <ctime>
  46. #include "opencv2/core.hpp"
  47. #include "opencv2/imgproc.hpp"
  48. #include "opencv2/videostab/global_motion.hpp"
  49. #include "opencv2/videostab/motion_stabilizing.hpp"
  50. #include "opencv2/videostab/frame_source.hpp"
  51. #include "opencv2/videostab/log.hpp"
  52. #include "opencv2/videostab/inpainting.hpp"
  53. #include "opencv2/videostab/deblurring.hpp"
  54. #include "opencv2/videostab/wobble_suppression.hpp"
  55. namespace cv
  56. {
  57. namespace videostab
  58. {
  59. //! @addtogroup videostab
  60. //! @{
  61. class CV_EXPORTS StabilizerBase
  62. {
  63. public:
  64. virtual ~StabilizerBase() {}
  65. void setLog(Ptr<ILog> ilog) { log_ = ilog; }
  66. Ptr<ILog> log() const { return log_; }
  67. void setRadius(int val) { radius_ = val; }
  68. int radius() const { return radius_; }
  69. void setFrameSource(Ptr<IFrameSource> val) { frameSource_ = val; }
  70. Ptr<IFrameSource> frameSource() const { return frameSource_; }
  71. void setMaskSource(const Ptr<IFrameSource>& val) { maskSource_ = val; }
  72. Ptr<IFrameSource> maskSource() const { return maskSource_; }
  73. void setMotionEstimator(Ptr<ImageMotionEstimatorBase> val) { motionEstimator_ = val; }
  74. Ptr<ImageMotionEstimatorBase> motionEstimator() const { return motionEstimator_; }
  75. void setDeblurer(Ptr<DeblurerBase> val) { deblurer_ = val; }
  76. Ptr<DeblurerBase> deblurrer() const { return deblurer_; }
  77. void setTrimRatio(float val) { trimRatio_ = val; }
  78. float trimRatio() const { return trimRatio_; }
  79. void setCorrectionForInclusion(bool val) { doCorrectionForInclusion_ = val; }
  80. bool doCorrectionForInclusion() const { return doCorrectionForInclusion_; }
  81. void setBorderMode(int val) { borderMode_ = val; }
  82. int borderMode() const { return borderMode_; }
  83. void setInpainter(Ptr<InpainterBase> val) { inpainter_ = val; }
  84. Ptr<InpainterBase> inpainter() const { return inpainter_; }
  85. protected:
  86. StabilizerBase();
  87. void reset();
  88. Mat nextStabilizedFrame();
  89. bool doOneIteration();
  90. virtual void setUp(const Mat &firstFrame);
  91. virtual Mat estimateMotion() = 0;
  92. virtual Mat estimateStabilizationMotion() = 0;
  93. void stabilizeFrame();
  94. virtual Mat postProcessFrame(const Mat &frame);
  95. void logProcessingTime();
  96. Ptr<ILog> log_;
  97. Ptr<IFrameSource> frameSource_;
  98. Ptr<IFrameSource> maskSource_;
  99. Ptr<ImageMotionEstimatorBase> motionEstimator_;
  100. Ptr<DeblurerBase> deblurer_;
  101. Ptr<InpainterBase> inpainter_;
  102. int radius_;
  103. float trimRatio_;
  104. bool doCorrectionForInclusion_;
  105. int borderMode_;
  106. Size frameSize_;
  107. Mat frameMask_;
  108. int curPos_;
  109. int curStabilizedPos_;
  110. bool doDeblurring_;
  111. Mat preProcessedFrame_;
  112. bool doInpainting_;
  113. Mat inpaintingMask_;
  114. Mat finalFrame_;
  115. std::vector<Mat> frames_;
  116. std::vector<Mat> motions_; // motions_[i] is the motion from i-th to i+1-th frame
  117. std::vector<float> blurrinessRates_;
  118. std::vector<Mat> stabilizedFrames_;
  119. std::vector<Mat> stabilizedMasks_;
  120. std::vector<Mat> stabilizationMotions_;
  121. clock_t processingStartTime_;
  122. };
  123. class CV_EXPORTS OnePassStabilizer : public StabilizerBase, public IFrameSource
  124. {
  125. public:
  126. OnePassStabilizer();
  127. void setMotionFilter(Ptr<MotionFilterBase> val) { motionFilter_ = val; }
  128. Ptr<MotionFilterBase> motionFilter() const { return motionFilter_; }
  129. virtual void reset() CV_OVERRIDE;
  130. virtual Mat nextFrame() CV_OVERRIDE { return nextStabilizedFrame(); }
  131. protected:
  132. virtual void setUp(const Mat &firstFrame) CV_OVERRIDE;
  133. virtual Mat estimateMotion() CV_OVERRIDE;
  134. virtual Mat estimateStabilizationMotion() CV_OVERRIDE;
  135. virtual Mat postProcessFrame(const Mat &frame) CV_OVERRIDE;
  136. Ptr<MotionFilterBase> motionFilter_;
  137. };
  138. class CV_EXPORTS TwoPassStabilizer : public StabilizerBase, public IFrameSource
  139. {
  140. public:
  141. TwoPassStabilizer();
  142. void setMotionStabilizer(Ptr<IMotionStabilizer> val) { motionStabilizer_ = val; }
  143. Ptr<IMotionStabilizer> motionStabilizer() const { return motionStabilizer_; }
  144. void setWobbleSuppressor(Ptr<WobbleSuppressorBase> val) { wobbleSuppressor_ = val; }
  145. Ptr<WobbleSuppressorBase> wobbleSuppressor() const { return wobbleSuppressor_; }
  146. void setEstimateTrimRatio(bool val) { mustEstTrimRatio_ = val; }
  147. bool mustEstimateTrimaRatio() const { return mustEstTrimRatio_; }
  148. virtual void reset() CV_OVERRIDE;
  149. virtual Mat nextFrame() CV_OVERRIDE;
  150. protected:
  151. void runPrePassIfNecessary();
  152. virtual void setUp(const Mat &firstFrame) CV_OVERRIDE;
  153. virtual Mat estimateMotion() CV_OVERRIDE;
  154. virtual Mat estimateStabilizationMotion() CV_OVERRIDE;
  155. virtual Mat postProcessFrame(const Mat &frame) CV_OVERRIDE;
  156. Ptr<IMotionStabilizer> motionStabilizer_;
  157. Ptr<WobbleSuppressorBase> wobbleSuppressor_;
  158. bool mustEstTrimRatio_;
  159. int frameCount_;
  160. bool isPrePassDone_;
  161. bool doWobbleSuppression_;
  162. std::vector<Mat> motions2_;
  163. Mat suppressedFrame_;
  164. };
  165. //! @}
  166. } // namespace videostab
  167. } // namespace cv
  168. #endif