include/opencv2/videostab/stabilizer.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other materials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_VIDEOSTAB_STABILIZER_HPP__
00044 #define __OPENCV_VIDEOSTAB_STABILIZER_HPP__
00045 
00046 #include <vector>
00047 #include "opencv2/core/core.hpp"
00048 #include "opencv2/imgproc/imgproc.hpp"
00049 #include "opencv2/videostab/global_motion.hpp"
00050 #include "opencv2/videostab/motion_stabilizing.hpp"
00051 #include "opencv2/videostab/frame_source.hpp"
00052 #include "opencv2/videostab/log.hpp"
00053 #include "opencv2/videostab/inpainting.hpp"
00054 #include "opencv2/videostab/deblurring.hpp"
00055 
00056 namespace cv
00057 {
00058 namespace videostab
00059 {
00060 
00061 class CV_EXPORTS StabilizerBase
00062 {
00063 public:
00064     virtual ~StabilizerBase() {}
00065 
00066     void setLog(Ptr<ILog> _log) { log_ = _log; }
00067     Ptr<ILog> log() const { return log_; }
00068 
00069     void setRadius(int val) { radius_ = val; }
00070     int radius() const { return radius_; }
00071 
00072     void setFrameSource(Ptr<IFrameSource> val) { frameSource_ = val; }
00073     Ptr<IFrameSource> frameSource() const { return frameSource_; }
00074 
00075     void setMotionEstimator(Ptr<IGlobalMotionEstimator> val) { motionEstimator_ = val; }
00076     Ptr<IGlobalMotionEstimator> motionEstimator() const { return motionEstimator_; }
00077 
00078     void setDeblurer(Ptr<DeblurerBase> val) { deblurer_ = val; }
00079     Ptr<DeblurerBase> deblurrer() const { return deblurer_; }
00080 
00081     void setTrimRatio(float val) { trimRatio_ = val; }
00082     float trimRatio() const { return trimRatio_; }
00083 
00084     void setCorrectionForInclusion(bool val) { doCorrectionForInclusion_ = val; }
00085     bool doCorrectionForInclusion() const { return doCorrectionForInclusion_; }
00086 
00087     void setBorderMode(int val) { borderMode_ = val; }
00088     int borderMode() const { return borderMode_; }
00089 
00090     void setInpainter(Ptr<InpainterBase> val) { inpainter_ = val; }
00091     Ptr<InpainterBase> inpainter() const { return inpainter_; }
00092 
00093 protected:
00094     StabilizerBase();
00095 
00096     void setUp(int cacheSize, const Mat &frame);
00097     Mat nextStabilizedFrame();
00098     bool doOneIteration();
00099     void stabilizeFrame(const Mat &stabilizationMotion);
00100 
00101     virtual void setUp(Mat &firstFrame) = 0;
00102     virtual void stabilizeFrame() = 0;
00103     virtual void estimateMotion() = 0;
00104 
00105     Ptr<ILog> log_;
00106     Ptr<IFrameSource> frameSource_;
00107     Ptr<IGlobalMotionEstimator> motionEstimator_;
00108     Ptr<DeblurerBase> deblurer_;
00109     Ptr<InpainterBase> inpainter_;
00110     int radius_;
00111     float trimRatio_;
00112     bool doCorrectionForInclusion_;
00113     int borderMode_;
00114 
00115     Size frameSize_;
00116     Mat frameMask_;
00117     int curPos_;
00118     int curStabilizedPos_;
00119     bool doDeblurring_;
00120     Mat preProcessedFrame_;
00121     bool doInpainting_;
00122     Mat inpaintingMask_;
00123     std::vector<Mat> frames_;
00124     std::vector<Mat> motions_; // motions_[i] is the motion from i-th to i+1-th frame
00125     std::vector<float> blurrinessRates_;
00126     std::vector<Mat> stabilizedFrames_;
00127     std::vector<Mat> stabilizedMasks_;
00128     std::vector<Mat> stabilizationMotions_;
00129 };
00130 
00131 class CV_EXPORTS OnePassStabilizer : public StabilizerBase, public IFrameSource
00132 {
00133 public:
00134     OnePassStabilizer();
00135 
00136     void setMotionFilter(Ptr<MotionFilterBase> val) { motionFilter_ = val; }
00137     Ptr<MotionFilterBase> motionFilter() const { return motionFilter_; }
00138 
00139     virtual void reset() { resetImpl(); }
00140     virtual Mat nextFrame() { return nextStabilizedFrame(); }
00141 
00142 private:
00143     void resetImpl();
00144 
00145     virtual void setUp(Mat &firstFrame);
00146     virtual void estimateMotion();
00147     virtual void stabilizeFrame();
00148 
00149     Ptr<MotionFilterBase> motionFilter_;
00150 };
00151 
00152 class CV_EXPORTS TwoPassStabilizer : public StabilizerBase, public IFrameSource
00153 {
00154 public:
00155     TwoPassStabilizer();
00156 
00157     void setMotionStabilizer(Ptr<IMotionStabilizer> val) { motionStabilizer_ = val; }
00158     Ptr<IMotionStabilizer> motionStabilizer() const { return motionStabilizer_; }
00159 
00160     void setEstimateTrimRatio(bool val) { mustEstTrimRatio_ = val; }
00161     bool mustEstimateTrimaRatio() const { return mustEstTrimRatio_; }
00162 
00163     virtual void reset() { resetImpl(); }
00164     virtual Mat nextFrame();
00165 
00166     // available after pre-pass, before it's empty
00167     std::vector<Mat> motions() const;
00168 
00169 private:
00170     void resetImpl();
00171     void runPrePassIfNecessary();
00172 
00173     virtual void setUp(Mat &firstFrame);
00174     virtual void estimateMotion() { /* do nothing as motion was estimation in pre-pass */ }
00175     virtual void stabilizeFrame();
00176 
00177     Ptr<IMotionStabilizer> motionStabilizer_;
00178     bool mustEstTrimRatio_;
00179 
00180     int frameCount_;
00181     bool isPrePassDone_;
00182 };
00183 
00184 } // namespace videostab
00185 } // namespace cv
00186 
00187 #endif