Refactored videostab module, added base class for woobble suppression
[profile/ivi/opencv.git] / modules / videostab / include / opencv2 / videostab / stabilizer.hpp
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 "opencv2/core/core.hpp"
48 #include "opencv2/imgproc/imgproc.hpp"
49 #include "opencv2/videostab/global_motion.hpp"
50 #include "opencv2/videostab/motion_stabilizing.hpp"
51 #include "opencv2/videostab/frame_source.hpp"
52 #include "opencv2/videostab/log.hpp"
53 #include "opencv2/videostab/inpainting.hpp"
54 #include "opencv2/videostab/deblurring.hpp"
55 #include "opencv2/videostab/wobble_suppression.hpp"
56
57 namespace cv
58 {
59 namespace videostab
60 {
61
62 class CV_EXPORTS StabilizerBase
63 {
64 public:
65     virtual ~StabilizerBase() {}
66
67     void setLog(Ptr<ILog> log) { log_ = log; }
68     Ptr<ILog> log() const { return log_; }
69
70     void setRadius(int val) { radius_ = val; }
71     int radius() const { return radius_; }
72
73     void setFrameSource(Ptr<IFrameSource> val) { frameSource_ = val; }
74     Ptr<IFrameSource> frameSource() const { return frameSource_; }
75
76     void setMotionEstimator(Ptr<GlobalMotionEstimatorBase> val) { motionEstimator_ = val; }
77     Ptr<GlobalMotionEstimatorBase> motionEstimator() const { return motionEstimator_; }
78
79     void setDeblurer(Ptr<DeblurerBase> val) { deblurer_ = val; }
80     Ptr<DeblurerBase> deblurrer() const { return deblurer_; }
81
82     void setTrimRatio(float val) { trimRatio_ = val; }
83     float trimRatio() const { return trimRatio_; }
84
85     void setCorrectionForInclusion(bool val) { doCorrectionForInclusion_ = val; }
86     bool doCorrectionForInclusion() const { return doCorrectionForInclusion_; }
87
88     void setBorderMode(int val) { borderMode_ = val; }
89     int borderMode() const { return borderMode_; }
90
91     void setInpainter(Ptr<InpainterBase> val) { inpainter_ = val; }
92     Ptr<InpainterBase> inpainter() const { return inpainter_; }
93
94 protected:
95     StabilizerBase();
96
97     void reset();
98     Mat nextStabilizedFrame();
99     bool doOneIteration();
100     virtual void setUp(const Mat &firstFrame);
101     virtual Mat estimateMotion() = 0;
102     virtual Mat estimateStabilizationMotion() = 0;
103     void stabilizeFrame();
104     virtual Mat postProcessFrame(const Mat &frame);
105
106     Ptr<ILog> log_;
107     Ptr<IFrameSource> frameSource_;
108     Ptr<GlobalMotionEstimatorBase> motionEstimator_;
109     Ptr<DeblurerBase> deblurer_;
110     Ptr<InpainterBase> inpainter_;
111     int radius_;
112     float trimRatio_;
113     bool doCorrectionForInclusion_;
114     int borderMode_;
115
116     Size frameSize_;
117     Mat frameMask_;
118     int curPos_;
119     int curStabilizedPos_;
120     bool doDeblurring_;
121     Mat preProcessedFrame_;
122     bool doInpainting_;
123     Mat inpaintingMask_;
124     Mat finalFrame_;
125     std::vector<Mat> frames_;
126     std::vector<Mat> motions_; // motions_[i] is the motion from i-th to i+1-th frame
127     std::vector<float> blurrinessRates_;
128     std::vector<Mat> stabilizedFrames_;
129     std::vector<Mat> stabilizedMasks_;
130     std::vector<Mat> stabilizationMotions_;
131 };
132
133 class CV_EXPORTS OnePassStabilizer : public StabilizerBase, public IFrameSource
134 {
135 public:
136     OnePassStabilizer();
137
138     void setMotionFilter(Ptr<MotionFilterBase> val) { motionFilter_ = val; }
139     Ptr<MotionFilterBase> motionFilter() const { return motionFilter_; }
140
141     virtual void reset();
142     virtual Mat nextFrame() { return nextStabilizedFrame(); }
143
144 private:
145     virtual void setUp(const Mat &firstFrame);
146     virtual Mat estimateMotion();
147     virtual Mat estimateStabilizationMotion();
148     virtual Mat postProcessFrame(const Mat &frame);
149
150     Ptr<MotionFilterBase> motionFilter_;
151 };
152
153 class CV_EXPORTS TwoPassStabilizer : public StabilizerBase, public IFrameSource
154 {
155 public:
156     TwoPassStabilizer();
157
158     void setMotionStabilizer(Ptr<IMotionStabilizer> val) { motionStabilizer_ = val; }
159     Ptr<IMotionStabilizer> motionStabilizer() const { return motionStabilizer_; }    
160
161     void setWobbleSuppressor(Ptr<WobbleSuppressorBase> val) { wobbleSuppressor_ = val; }
162     Ptr<WobbleSuppressorBase> wobbleSuppressor() const { return wobbleSuppressor_; }
163
164     void setEstimateTrimRatio(bool val) { mustEstTrimRatio_ = val; }
165     bool mustEstimateTrimaRatio() const { return mustEstTrimRatio_; }
166
167     virtual void reset();
168     virtual Mat nextFrame();
169
170     // available after pre-pass, before it's empty
171     std::vector<Mat> motions() const;
172
173 private:
174     void runPrePassIfNecessary();
175
176     virtual void setUp(const Mat &firstFrame);
177     virtual Mat estimateMotion();
178     virtual Mat estimateStabilizationMotion();
179     virtual Mat postProcessFrame(const Mat &frame);
180
181     Ptr<IMotionStabilizer> motionStabilizer_;
182     Ptr<WobbleSuppressorBase> wobbleSuppressor_;
183     bool mustEstTrimRatio_;
184
185     int frameCount_;
186     bool isPrePassDone_;
187     Mat suppressedFrame_;
188 };
189
190 } // namespace videostab
191 } // namespace cv
192
193 #endif