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