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