Added distance threshold argument into videostab sample
[profile/ivi/opencv.git] / modules / videostab / include / opencv2 / videostab / inpainting.hpp
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/core.hpp"
48 #include "opencv2/videostab/optical_flow.hpp"
49 #include "opencv2/videostab/fast_marching.hpp"
50 #include "opencv2/photo/photo.hpp"
51
52 namespace cv
53 {
54 namespace videostab
55 {
56
57 class CV_EXPORTS IInpainter
58 {
59 public:
60     IInpainter()
61         : radius_(0), frames_(0), motions_(0),
62           stabilizedFrames_(0), stabilizationMotions_(0) {}
63
64     virtual ~IInpainter() {}
65
66     virtual void setRadius(int val) { radius_ = val; }
67     int radius() const { return radius_; }
68
69     virtual void setFrames(const std::vector<Mat> &val) { frames_ = &val; }
70     const std::vector<Mat>& frames() const { return *frames_; }
71
72     virtual void setMotions(const std::vector<Mat> &val) { motions_ = &val; }
73     const std::vector<Mat>& motions() const { return *motions_; }
74
75     virtual void setStabilizedFrames(const std::vector<Mat> &val) { stabilizedFrames_ = &val; }
76     const std::vector<Mat>& stabilizedFrames() const { return *stabilizedFrames_; }
77
78     virtual void setStabilizationMotions(const std::vector<Mat> &val) { stabilizationMotions_ = &val; }
79     const std::vector<Mat>& stabilizationMotions() const { return *stabilizationMotions_; }
80
81     virtual void inpaint(int idx, Mat &frame, Mat &mask) = 0;
82
83 protected:
84     int radius_;
85     const std::vector<Mat> *frames_;
86     const std::vector<Mat> *motions_;
87     const std::vector<Mat> *stabilizedFrames_;
88     const std::vector<Mat> *stabilizationMotions_;
89 };
90
91 class CV_EXPORTS NullInpainter : public IInpainter
92 {
93 public:
94     virtual void inpaint(int /*idx*/, Mat &/*frame*/, Mat &/*mask*/) {}
95 };
96
97 class CV_EXPORTS InpaintingPipeline : public IInpainter
98 {
99 public:
100     void pushBack(Ptr<IInpainter> inpainter) { inpainters_.push_back(inpainter); }
101     bool empty() const { return inpainters_.empty(); }
102
103     virtual void setRadius(int val);
104     virtual void setFrames(const std::vector<Mat> &val);
105     virtual void setMotions(const std::vector<Mat> &val);
106     virtual void setStabilizedFrames(const std::vector<Mat> &val);
107     virtual void setStabilizationMotions(const std::vector<Mat> &val);
108
109     virtual void inpaint(int idx, Mat &frame, Mat &mask);
110
111 private:
112     std::vector<Ptr<IInpainter> > inpainters_;
113 };
114
115 class CV_EXPORTS ConsistentMosaicInpainter : public IInpainter
116 {
117 public:
118     ConsistentMosaicInpainter();
119
120     void setStdevThresh(float val) { stdevThresh_ = val; }
121     float stdevThresh() const { return stdevThresh_; }
122
123     virtual void inpaint(int idx, Mat &frame, Mat &mask);
124
125 private:
126     float stdevThresh_;
127 };
128
129 class CV_EXPORTS MotionInpainter : public IInpainter
130 {
131 public:
132     MotionInpainter();
133
134     void setOptFlowEstimator(Ptr<IDenseOptFlowEstimator> val) { optFlowEstimator_ = val; }
135     Ptr<IDenseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
136
137     void setFlowErrorThreshold(float val) { flowErrorThreshold_ = val; }
138     float flowErrorThreshold() const { return flowErrorThreshold_; }
139
140     void setDistThreshold(float val) { distThresh_ = val; }
141     float distThresh() const { return distThresh_; }
142
143     void setBorderMode(int val) { borderMode_ = val; }
144     int borderMode() const { return borderMode_; }
145
146     virtual void inpaint(int idx, Mat &frame, Mat &mask);
147
148 private:
149     FastMarchingMethod fmm_;
150     Ptr<IDenseOptFlowEstimator> optFlowEstimator_;
151     float flowErrorThreshold_;
152     float distThresh_;
153     int borderMode_;
154
155     Mat frame1_, transformedFrame1_;
156     Mat_<uchar> grayFrame_, transformedGrayFrame1_;
157     Mat_<uchar> mask1_, transformedMask1_;
158     Mat_<float> flowX_, flowY_, flowErrors_;
159     Mat_<uchar> flowMask_;
160 };
161
162 class CV_EXPORTS ColorAverageInpainter : public IInpainter
163 {
164 public:
165     virtual void inpaint(int idx, Mat &frame, Mat &mask);
166
167 private:
168     FastMarchingMethod fmm_;
169 };
170
171 class CV_EXPORTS ColorInpainter : public IInpainter
172 {
173 public:
174     ColorInpainter(int method = INPAINT_TELEA, double radius = 2.)
175         : method_(method), radius_(radius) {}
176
177     virtual void inpaint(int idx, Mat &frame, Mat &mask);
178
179 private:
180     int method_;
181     double radius_;
182     Mat invMask_;
183 };
184
185 CV_EXPORTS void calcFlowMask(
186         const Mat &flowX, const Mat &flowY, const Mat &errors, float maxError,
187         const Mat &mask0, const Mat &mask1, Mat &flowMask);
188
189 CV_EXPORTS void completeFrameAccordingToFlow(
190         const Mat &flowMask, const Mat &flowX, const Mat &flowY, const Mat &frame1, const Mat &mask1,
191         float distThresh, Mat& frame0, Mat &mask0);
192
193 } // namespace videostab
194 } // namespace cv
195
196 #endif