stabilizer.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 setMotionEstimator(Ptr<ImageMotionEstimatorBase> val) { motionEstimator_ = val; }
  72. Ptr<ImageMotionEstimatorBase> motionEstimator() const { return motionEstimator_; }
  73. void setDeblurer(Ptr<DeblurerBase> val) { deblurer_ = val; }
  74. Ptr<DeblurerBase> deblurrer() const { return deblurer_; }
  75. void setTrimRatio(float val) { trimRatio_ = val; }
  76. float trimRatio() const { return trimRatio_; }
  77. void setCorrectionForInclusion(bool val) { doCorrectionForInclusion_ = val; }
  78. bool doCorrectionForInclusion() const { return doCorrectionForInclusion_; }
  79. void setBorderMode(int val) { borderMode_ = val; }
  80. int borderMode() const { return borderMode_; }
  81. void setInpainter(Ptr<InpainterBase> val) { inpainter_ = val; }
  82. Ptr<InpainterBase> inpainter() const { return inpainter_; }
  83. protected:
  84. StabilizerBase();
  85. void reset();
  86. Mat nextStabilizedFrame();
  87. bool doOneIteration();
  88. virtual void setUp(const Mat &firstFrame);
  89. virtual Mat estimateMotion() = 0;
  90. virtual Mat estimateStabilizationMotion() = 0;
  91. void stabilizeFrame();
  92. virtual Mat postProcessFrame(const Mat &frame);
  93. void logProcessingTime();
  94. Ptr<ILog> log_;
  95. Ptr<IFrameSource> frameSource_;
  96. Ptr<ImageMotionEstimatorBase> motionEstimator_;
  97. Ptr<DeblurerBase> deblurer_;
  98. Ptr<InpainterBase> inpainter_;
  99. int radius_;
  100. float trimRatio_;
  101. bool doCorrectionForInclusion_;
  102. int borderMode_;
  103. Size frameSize_;
  104. Mat frameMask_;
  105. int curPos_;
  106. int curStabilizedPos_;
  107. bool doDeblurring_;
  108. Mat preProcessedFrame_;
  109. bool doInpainting_;
  110. Mat inpaintingMask_;
  111. Mat finalFrame_;
  112. std::vector<Mat> frames_;
  113. std::vector<Mat> motions_; // motions_[i] is the motion from i-th to i+1-th frame
  114. std::vector<float> blurrinessRates_;
  115. std::vector<Mat> stabilizedFrames_;
  116. std::vector<Mat> stabilizedMasks_;
  117. std::vector<Mat> stabilizationMotions_;
  118. clock_t processingStartTime_;
  119. };
  120. class CV_EXPORTS OnePassStabilizer : public StabilizerBase, public IFrameSource
  121. {
  122. public:
  123. OnePassStabilizer();
  124. void setMotionFilter(Ptr<MotionFilterBase> val) { motionFilter_ = val; }
  125. Ptr<MotionFilterBase> motionFilter() const { return motionFilter_; }
  126. virtual void reset() CV_OVERRIDE;
  127. virtual Mat nextFrame() CV_OVERRIDE { return nextStabilizedFrame(); }
  128. protected:
  129. virtual void setUp(const Mat &firstFrame) CV_OVERRIDE;
  130. virtual Mat estimateMotion() CV_OVERRIDE;
  131. virtual Mat estimateStabilizationMotion() CV_OVERRIDE;
  132. virtual Mat postProcessFrame(const Mat &frame) CV_OVERRIDE;
  133. Ptr<MotionFilterBase> motionFilter_;
  134. };
  135. class CV_EXPORTS TwoPassStabilizer : public StabilizerBase, public IFrameSource
  136. {
  137. public:
  138. TwoPassStabilizer();
  139. void setMotionStabilizer(Ptr<IMotionStabilizer> val) { motionStabilizer_ = val; }
  140. Ptr<IMotionStabilizer> motionStabilizer() const { return motionStabilizer_; }
  141. void setWobbleSuppressor(Ptr<WobbleSuppressorBase> val) { wobbleSuppressor_ = val; }
  142. Ptr<WobbleSuppressorBase> wobbleSuppressor() const { return wobbleSuppressor_; }
  143. void setEstimateTrimRatio(bool val) { mustEstTrimRatio_ = val; }
  144. bool mustEstimateTrimaRatio() const { return mustEstTrimRatio_; }
  145. virtual void reset() CV_OVERRIDE;
  146. virtual Mat nextFrame() CV_OVERRIDE;
  147. protected:
  148. void runPrePassIfNecessary();
  149. virtual void setUp(const Mat &firstFrame) CV_OVERRIDE;
  150. virtual Mat estimateMotion() CV_OVERRIDE;
  151. virtual Mat estimateStabilizationMotion() CV_OVERRIDE;
  152. virtual Mat postProcessFrame(const Mat &frame) CV_OVERRIDE;
  153. Ptr<IMotionStabilizer> motionStabilizer_;
  154. Ptr<WobbleSuppressorBase> wobbleSuppressor_;
  155. bool mustEstTrimRatio_;
  156. int frameCount_;
  157. bool isPrePassDone_;
  158. bool doWobbleSuppression_;
  159. std::vector<Mat> motions2_;
  160. Mat suppressedFrame_;
  161. };
  162. //! @}
  163. } // namespace videostab
  164. } // namespace cv
  165. #endif