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_INPAINTINT_HPP
44 #define OPENCV_VIDEOSTAB_INPAINTINT_HPP
45 
46 #include <vector>
47 #include "opencv2/core.hpp"
48 #include "opencv2/videostab/optical_flow.hpp"
49 #include "opencv2/videostab/fast_marching.hpp"
50 #include "opencv2/videostab/global_motion.hpp"
51 #include "opencv2/photo.hpp"
52 
53 namespace cv
54 {
55 namespace videostab
56 {
57 
58 //! @addtogroup videostab
59 //! @{
60 
61 class CV_EXPORTS InpainterBase
62 {
63 public:
InpainterBase()64     InpainterBase()
65         : radius_(0), motionModel_(MM_UNKNOWN), frames_(0), motions_(0),
66           stabilizedFrames_(0), stabilizationMotions_(0) {}
67 
~InpainterBase()68     virtual ~InpainterBase() {}
69 
setRadius(int val)70     virtual void setRadius(int val) { radius_ = val; }
radius() const71     virtual int radius() const { return radius_; }
72 
setMotionModel(MotionModel val)73     virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
motionModel() const74     virtual MotionModel motionModel() const { return motionModel_; }
75 
76     virtual void inpaint(int idx, Mat &frame, Mat &mask) = 0;
77 
78 
79     // data from stabilizer
80 
setFrames(const std::vector<Mat> & val)81     virtual void setFrames(const std::vector<Mat> &val) { frames_ = &val; }
frames() const82     virtual const std::vector<Mat>& frames() const { return *frames_; }
83 
setMotions(const std::vector<Mat> & val)84     virtual void setMotions(const std::vector<Mat> &val) { motions_ = &val; }
motions() const85     virtual const std::vector<Mat>& motions() const { return *motions_; }
86 
setStabilizedFrames(const std::vector<Mat> & val)87     virtual void setStabilizedFrames(const std::vector<Mat> &val) { stabilizedFrames_ = &val; }
stabilizedFrames() const88     virtual const std::vector<Mat>& stabilizedFrames() const { return *stabilizedFrames_; }
89 
setStabilizationMotions(const std::vector<Mat> & val)90     virtual void setStabilizationMotions(const std::vector<Mat> &val) { stabilizationMotions_ = &val; }
stabilizationMotions() const91     virtual const std::vector<Mat>& stabilizationMotions() const { return *stabilizationMotions_; }
92 
93 protected:
94     int radius_;
95     MotionModel motionModel_;
96     const std::vector<Mat> *frames_;
97     const std::vector<Mat> *motions_;
98     const std::vector<Mat> *stabilizedFrames_;
99     const std::vector<Mat> *stabilizationMotions_;
100 };
101 
102 class CV_EXPORTS NullInpainter : public InpainterBase
103 {
104 public:
inpaint(int,Mat &,Mat &)105     virtual void inpaint(int /*idx*/, Mat &/*frame*/, Mat &/*mask*/) CV_OVERRIDE {}
106 };
107 
108 class CV_EXPORTS InpaintingPipeline : public InpainterBase
109 {
110 public:
pushBack(Ptr<InpainterBase> inpainter)111     void pushBack(Ptr<InpainterBase> inpainter) { inpainters_.push_back(inpainter); }
empty() const112     bool empty() const { return inpainters_.empty(); }
113 
114     virtual void setRadius(int val) CV_OVERRIDE;
115     virtual void setMotionModel(MotionModel val) CV_OVERRIDE;
116     virtual void setFrames(const std::vector<Mat> &val) CV_OVERRIDE;
117     virtual void setMotions(const std::vector<Mat> &val) CV_OVERRIDE;
118     virtual void setStabilizedFrames(const std::vector<Mat> &val) CV_OVERRIDE;
119     virtual void setStabilizationMotions(const std::vector<Mat> &val) CV_OVERRIDE;
120 
121     virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
122 
123 private:
124     std::vector<Ptr<InpainterBase> > inpainters_;
125 };
126 
127 class CV_EXPORTS ConsistentMosaicInpainter : public InpainterBase
128 {
129 public:
130     ConsistentMosaicInpainter();
131 
setStdevThresh(float val)132     void setStdevThresh(float val) { stdevThresh_ = val; }
stdevThresh() const133     float stdevThresh() const { return stdevThresh_; }
134 
135     virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
136 
137 private:
138     float stdevThresh_;
139 };
140 
141 class CV_EXPORTS MotionInpainter : public InpainterBase
142 {
143 public:
144     MotionInpainter();
145 
setOptFlowEstimator(Ptr<IDenseOptFlowEstimator> val)146     void setOptFlowEstimator(Ptr<IDenseOptFlowEstimator> val) { optFlowEstimator_ = val; }
optFlowEstimator() const147     Ptr<IDenseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
148 
setFlowErrorThreshold(float val)149     void setFlowErrorThreshold(float val) { flowErrorThreshold_ = val; }
flowErrorThreshold() const150     float flowErrorThreshold() const { return flowErrorThreshold_; }
151 
setDistThreshold(float val)152     void setDistThreshold(float val) { distThresh_ = val; }
distThresh() const153     float distThresh() const { return distThresh_; }
154 
setBorderMode(int val)155     void setBorderMode(int val) { borderMode_ = val; }
borderMode() const156     int borderMode() const { return borderMode_; }
157 
158     virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
159 
160 private:
161     FastMarchingMethod fmm_;
162     Ptr<IDenseOptFlowEstimator> optFlowEstimator_;
163     float flowErrorThreshold_;
164     float distThresh_;
165     int borderMode_;
166 
167     Mat frame1_, transformedFrame1_;
168     Mat_<uchar> grayFrame_, transformedGrayFrame1_;
169     Mat_<uchar> mask1_, transformedMask1_;
170     Mat_<float> flowX_, flowY_, flowErrors_;
171     Mat_<uchar> flowMask_;
172 };
173 
174 class CV_EXPORTS ColorAverageInpainter : public InpainterBase
175 {
176 public:
177     virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
178 
179 private:
180     FastMarchingMethod fmm_;
181 };
182 
183 class CV_EXPORTS ColorInpainter : public InpainterBase
184 {
185 public:
186     ColorInpainter(int method = INPAINT_TELEA, double radius = 2.);
187 
188     virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
189 
190 private:
191     int method_;
192     double radius_;
193     Mat invMask_;
194 };
195 
ColorInpainter(int _method,double _radius)196 inline ColorInpainter::ColorInpainter(int _method, double _radius)
197         : method_(_method), radius_(_radius) {}
198 
199 CV_EXPORTS void calcFlowMask(
200         const Mat &flowX, const Mat &flowY, const Mat &errors, float maxError,
201         const Mat &mask0, const Mat &mask1, Mat &flowMask);
202 
203 CV_EXPORTS void completeFrameAccordingToFlow(
204         const Mat &flowMask, const Mat &flowX, const Mat &flowY, const Mat &frame1, const Mat &mask1,
205         float distThresh, Mat& frame0, Mat &mask0);
206 
207 //! @}
208 
209 } // namespace videostab
210 } // namespace cv
211 
212 #endif
213