include/opencv2/videostab/inpainting.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_INPAINTINT_HPP__
00044 #define __OPENCV_VIDEOSTAB_INPAINTINT_HPP__
00045 
00046 #include <vector>
00047 #include "opencv2/core/core.hpp"
00048 #include "opencv2/videostab/optical_flow.hpp"
00049 #include "opencv2/videostab/fast_marching.hpp"
00050 #include "opencv2/photo/photo.hpp"
00051 
00052 namespace cv
00053 {
00054 namespace videostab
00055 {
00056 
00057 class CV_EXPORTS InpainterBase
00058 {
00059 public:
00060     InpainterBase()
00061         : radius_(0), frames_(0), motions_(0),
00062           stabilizedFrames_(0), stabilizationMotions_(0) {}
00063 
00064     virtual ~InpainterBase() {}
00065 
00066     virtual void setRadius(int val) { radius_ = val; }
00067     virtual int radius() const { return radius_; }
00068 
00069     virtual void setFrames(const std::vector<Mat> &val) { frames_ = &val; }
00070     virtual const std::vector<Mat>& frames() const { return *frames_; }
00071 
00072     virtual void setMotions(const std::vector<Mat> &val) { motions_ = &val; }
00073     virtual const std::vector<Mat>& motions() const { return *motions_; }
00074 
00075     virtual void setStabilizedFrames(const std::vector<Mat> &val) { stabilizedFrames_ = &val; }
00076     virtual const std::vector<Mat>& stabilizedFrames() const { return *stabilizedFrames_; }
00077 
00078     virtual void setStabilizationMotions(const std::vector<Mat> &val) { stabilizationMotions_ = &val; }
00079     virtual const std::vector<Mat>& stabilizationMotions() const { return *stabilizationMotions_; }
00080 
00081     virtual void update() {}
00082 
00083     virtual void inpaint(int idx, Mat &frame, Mat &mask) = 0;
00084 
00085 protected:
00086     int radius_;
00087     const std::vector<Mat> *frames_;
00088     const std::vector<Mat> *motions_;
00089     const std::vector<Mat> *stabilizedFrames_;
00090     const std::vector<Mat> *stabilizationMotions_;
00091 };
00092 
00093 class CV_EXPORTS NullInpainter : public InpainterBase
00094 {
00095 public:
00096     virtual void inpaint(int /*idx*/, Mat &/*frame*/, Mat &/*mask*/) {}
00097 };
00098 
00099 class CV_EXPORTS InpaintingPipeline : public InpainterBase
00100 {
00101 public:
00102     void pushBack(Ptr<InpainterBase> inpainter) { inpainters_.push_back(inpainter); }
00103     bool empty() const { return inpainters_.empty(); }
00104 
00105     virtual void setRadius(int val);
00106     virtual void setFrames(const std::vector<Mat> &val);
00107     virtual void setMotions(const std::vector<Mat> &val);
00108     virtual void setStabilizedFrames(const std::vector<Mat> &val);
00109     virtual void setStabilizationMotions(const std::vector<Mat> &val);
00110 
00111     virtual void update();
00112 
00113     virtual void inpaint(int idx, Mat &frame, Mat &mask);
00114 
00115 private:
00116     std::vector<Ptr<InpainterBase> > inpainters_;
00117 };
00118 
00119 class CV_EXPORTS ConsistentMosaicInpainter : public InpainterBase
00120 {
00121 public:
00122     ConsistentMosaicInpainter();
00123 
00124     void setStdevThresh(float val) { stdevThresh_ = val; }
00125     float stdevThresh() const { return stdevThresh_; }
00126 
00127     virtual void inpaint(int idx, Mat &frame, Mat &mask);
00128 
00129 private:
00130     float stdevThresh_;
00131 };
00132 
00133 class CV_EXPORTS MotionInpainter : public InpainterBase
00134 {
00135 public:
00136     MotionInpainter();
00137 
00138     void setOptFlowEstimator(Ptr<IDenseOptFlowEstimator> val) { optFlowEstimator_ = val; }
00139     Ptr<IDenseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
00140 
00141     void setFlowErrorThreshold(float val) { flowErrorThreshold_ = val; }
00142     float flowErrorThreshold() const { return flowErrorThreshold_; }
00143 
00144     void setDistThreshold(float val) { distThresh_ = val; }
00145     float distThresh() const { return distThresh_; }
00146 
00147     void setBorderMode(int val) { borderMode_ = val; }
00148     int borderMode() const { return borderMode_; }
00149 
00150     virtual void inpaint(int idx, Mat &frame, Mat &mask);
00151 
00152 private:
00153     FastMarchingMethod fmm_;
00154     Ptr<IDenseOptFlowEstimator> optFlowEstimator_;
00155     float flowErrorThreshold_;
00156     float distThresh_;
00157     int borderMode_;
00158 
00159     Mat frame1_, transformedFrame1_;
00160     Mat_<uchar> grayFrame_, transformedGrayFrame1_;
00161     Mat_<uchar> mask1_, transformedMask1_;
00162     Mat_<float> flowX_, flowY_, flowErrors_;
00163     Mat_<uchar> flowMask_;
00164 };
00165 
00166 class CV_EXPORTS ColorAverageInpainter : public InpainterBase
00167 {
00168 public:
00169     virtual void inpaint(int idx, Mat &frame, Mat &mask);
00170 
00171 private:
00172     FastMarchingMethod fmm_;
00173 };
00174 
00175 class CV_EXPORTS ColorInpainter : public InpainterBase
00176 {
00177 public:
00178     ColorInpainter(int method = INPAINT_TELEA, double _radius = 2.)
00179         : method_(method), radius_(_radius) {}
00180 
00181     virtual void inpaint(int idx, Mat &frame, Mat &mask);
00182 
00183 private:
00184     int method_;
00185     double radius_;
00186     Mat invMask_;
00187 };
00188 
00189 CV_EXPORTS void calcFlowMask(
00190         const Mat &flowX, const Mat &flowY, const Mat &errors, float maxError,
00191         const Mat &mask0, const Mat &mask1, Mat &flowMask);
00192 
00193 CV_EXPORTS void completeFrameAccordingToFlow(
00194         const Mat &flowMask, const Mat &flowX, const Mat &flowY, const Mat &frame1, const Mat &mask1,
00195         float distThresh, Mat& frame0, Mat &mask0);
00196 
00197 } // namespace videostab
00198 } // namespace cv
00199 
00200 #endif