stabilizer.hpp
Go to the documentation of this file.
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"
55 
56 namespace cv
57 {
58 namespace videostab
59 {
60 
61 class CV_EXPORTS StabilizerBase
62 {
63 public:
64  virtual ~StabilizerBase() {}
65 
66  void setLog(Ptr<ILog> _log) { log_ = _log; }
67  Ptr<ILog> log() const { return log_; }
68 
69  void setRadius(int val) { radius_ = val; }
70  int radius() const { return radius_; }
71 
72  void setFrameSource(Ptr<IFrameSource> val) { frameSource_ = val; }
73  Ptr<IFrameSource> frameSource() const { return frameSource_; }
74 
75  void setMotionEstimator(Ptr<IGlobalMotionEstimator> val) { motionEstimator_ = val; }
76  Ptr<IGlobalMotionEstimator> motionEstimator() const { return motionEstimator_; }
77 
78  void setDeblurer(Ptr<DeblurerBase> val) { deblurer_ = val; }
79  Ptr<DeblurerBase> deblurrer() const { return deblurer_; }
80 
81  void setTrimRatio(float val) { trimRatio_ = val; }
82  float trimRatio() const { return trimRatio_; }
83 
84  void setCorrectionForInclusion(bool val) { doCorrectionForInclusion_ = val; }
85  bool doCorrectionForInclusion() const { return doCorrectionForInclusion_; }
86 
87  void setBorderMode(int val) { borderMode_ = val; }
88  int borderMode() const { return borderMode_; }
89 
90  void setInpainter(Ptr<InpainterBase> val) { inpainter_ = val; }
91  Ptr<InpainterBase> inpainter() const { return inpainter_; }
92 
93 protected:
95 
96  void setUp(int cacheSize, const Mat &frame);
97  Mat nextStabilizedFrame();
98  bool doOneIteration();
99  void stabilizeFrame(const Mat &stabilizationMotion);
100 
101  virtual void setUp(Mat &firstFrame) = 0;
102  virtual void stabilizeFrame() = 0;
103  virtual void estimateMotion() = 0;
104 
110  int radius_;
111  float trimRatio_;
114 
117  int curPos_;
123  std::vector<Mat> frames_;
124  std::vector<Mat> motions_; // motions_[i] is the motion from i-th to i+1-th frame
125  std::vector<float> blurrinessRates_;
126  std::vector<Mat> stabilizedFrames_;
127  std::vector<Mat> stabilizedMasks_;
128  std::vector<Mat> stabilizationMotions_;
129 };
130 
131 class CV_EXPORTS OnePassStabilizer : public StabilizerBase, public IFrameSource
132 {
133 public:
135 
136  void setMotionFilter(Ptr<MotionFilterBase> val) { motionFilter_ = val; }
137  Ptr<MotionFilterBase> motionFilter() const { return motionFilter_; }
138 
139  virtual void reset() { resetImpl(); }
140  virtual Mat nextFrame() { return nextStabilizedFrame(); }
141 
142 private:
143  void resetImpl();
144 
145  virtual void setUp(Mat &firstFrame);
146  virtual void estimateMotion();
147  virtual void stabilizeFrame();
148 
149  Ptr<MotionFilterBase> motionFilter_;
150 };
151 
152 class CV_EXPORTS TwoPassStabilizer : public StabilizerBase, public IFrameSource
153 {
154 public:
156 
157  void setMotionStabilizer(Ptr<IMotionStabilizer> val) { motionStabilizer_ = val; }
158  Ptr<IMotionStabilizer> motionStabilizer() const { return motionStabilizer_; }
159 
160  void setEstimateTrimRatio(bool val) { mustEstTrimRatio_ = val; }
161  bool mustEstimateTrimaRatio() const { return mustEstTrimRatio_; }
162 
163  virtual void reset() { resetImpl(); }
164  virtual Mat nextFrame();
165 
166  // available after pre-pass, before it's empty
167  std::vector<Mat> motions() const;
168 
169 private:
170  void resetImpl();
171  void runPrePassIfNecessary();
172 
173  virtual void setUp(Mat &firstFrame);
174  virtual void estimateMotion() { /* do nothing as motion was estimation in pre-pass */ }
175  virtual void stabilizeFrame();
176 
177  Ptr<IMotionStabilizer> motionStabilizer_;
178  bool mustEstTrimRatio_;
179 
180  int frameCount_;
181  bool isPrePassDone_;
182 };
183 
184 } // namespace videostab
185 } // namespace cv
186 
187 #endif
void setFrameSource(Ptr< IFrameSource > val)
Definition: stabilizer.hpp:72
void setInpainter(Ptr< InpainterBase > val)
Definition: stabilizer.hpp:90
int radius_
Definition: stabilizer.hpp:110
std::vector< Mat > stabilizedMasks_
Definition: stabilizer.hpp:127
virtual void reset()
Definition: stabilizer.hpp:163
void setRadius(int val)
Definition: stabilizer.hpp:69
Matrix44< T > nextFrame(const Matrix44< T > &prevMatrix, const Vec3< T > &prevPoint, const Vec3< T > &curPoint, Vec3< T > &prevTangent, Vec3< T > &curTangent)
Definition: stabilizer.hpp:131
bool doInpainting_
Definition: stabilizer.hpp:121
void setMotionEstimator(Ptr< IGlobalMotionEstimator > val)
Definition: stabilizer.hpp:75
Ptr< IMotionStabilizer > motionStabilizer() const
Definition: stabilizer.hpp:158
bool doDeblurring_
Definition: stabilizer.hpp:119
Ptr< InpainterBase > inpainter_
Definition: stabilizer.hpp:109
Definition: frame_source.hpp:56
bool mustEstimateTrimaRatio() const
Definition: stabilizer.hpp:161
float trimRatio_
Definition: stabilizer.hpp:111
Ptr< IGlobalMotionEstimator > motionEstimator_
Definition: stabilizer.hpp:107
void setEstimateTrimRatio(bool val)
Definition: stabilizer.hpp:160
Ptr< DeblurerBase > deblurrer() const
Definition: stabilizer.hpp:79
Mat frameMask_
Definition: stabilizer.hpp:116
std::vector< Mat > stabilizedFrames_
Definition: stabilizer.hpp:126
The 2D size class.
Definition: core.hpp:81
std::vector< Mat > stabilizationMotions_
Definition: stabilizer.hpp:128
void setTrimRatio(float val)
Definition: stabilizer.hpp:81
void setCorrectionForInclusion(bool val)
Definition: stabilizer.hpp:84
Mat preProcessedFrame_
Definition: stabilizer.hpp:120
Definition: stabilizer.hpp:61
GLuint GLfloat * val
Ptr< ILog > log_
Definition: stabilizer.hpp:105
void setMotionStabilizer(Ptr< IMotionStabilizer > val)
Definition: stabilizer.hpp:157
virtual Mat nextFrame()
Definition: stabilizer.hpp:140
int radius() const
Definition: stabilizer.hpp:70
Mat inpaintingMask_
Definition: stabilizer.hpp:122
int borderMode_
Definition: stabilizer.hpp:113
Ptr< DeblurerBase > deblurer_
Definition: stabilizer.hpp:108
Ptr< InpainterBase > inpainter() const
Definition: stabilizer.hpp:91
The Image Processing.
virtual void reset()
Definition: stabilizer.hpp:139
Ptr< IFrameSource > frameSource_
Definition: stabilizer.hpp:106
void setMotionFilter(Ptr< MotionFilterBase > val)
Definition: stabilizer.hpp:136
std::vector< Mat > frames_
Definition: stabilizer.hpp:123
std::vector< Mat > motions_
Definition: stabilizer.hpp:124
void setBorderMode(int val)
Definition: stabilizer.hpp:87
The Core Functionality.
Ptr< IFrameSource > frameSource() const
Definition: stabilizer.hpp:73
The n-dimensional matrix class.
Definition: core.hpp:1688
std::vector< float > blurrinessRates_
Definition: stabilizer.hpp:125
Ptr< ILog > log() const
Definition: stabilizer.hpp:67
void void * frame
Definition: core_c.h:1459
Ptr< MotionFilterBase > motionFilter() const
Definition: stabilizer.hpp:137
float trimRatio() const
Definition: stabilizer.hpp:82
bool doCorrectionForInclusion_
Definition: stabilizer.hpp:112
void setLog(Ptr< ILog > _log)
Definition: stabilizer.hpp:66
int curStabilizedPos_
Definition: stabilizer.hpp:118
Ptr< IGlobalMotionEstimator > motionEstimator() const
Definition: stabilizer.hpp:76
Smart pointer to dynamically allocated objects.
Definition: core.hpp:1268
Size frameSize_
Definition: stabilizer.hpp:115
int curPos_
Definition: stabilizer.hpp:117
virtual ~StabilizerBase()
Definition: stabilizer.hpp:64
Definition: stabilizer.hpp:152
void setDeblurer(Ptr< DeblurerBase > val)
Definition: stabilizer.hpp:78
bool doCorrectionForInclusion() const
Definition: stabilizer.hpp:85
int borderMode() const
Definition: stabilizer.hpp:88