1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler/*M///////////////////////////////////////////////////////////////////////////////////////
2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  By downloading, copying, installing or using the software you agree to this license.
6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  If you do not agree to this license, do not download, install,
7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//  copy or use the software.
8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//                           License Agreement
11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//                For Open Source Computer Vision Library
12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Third party copyrights are property of their respective owners.
16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Redistribution and use in source and binary forms, with or without modification,
18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// are permitted provided that the following conditions are met:
19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * Redistribution's of source code must retain the above copyright notice,
21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     this list of conditions and the following disclaimer.
22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * Redistribution's in binary form must reproduce the above copyright notice,
24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     this list of conditions and the following disclaimer in the documentation
25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     and/or other materials provided with the distribution.
26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//   * The name of the copyright holders may not be used to endorse or promote products
28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//     derived from this software without specific prior written permission.
29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
30793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// This software is provided by the copyright holders and contributors "as is" and
31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// any express or implied warranties, including, but not limited to, the implied
32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// warranties of merchantability and fitness for a particular purpose are disclaimed.
33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// In no event shall the Intel Corporation or contributors be liable for any direct,
34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// indirect, incidental, special, exemplary, or consequential damages
35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// (including, but not limited to, procurement of substitute goods or services;
36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// loss of use, data, or profits; or business interruption) however caused
37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// and on any theory of liability, whether in contract, strict liability,
38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// or tort (including negligence or otherwise) arising in any way out of
39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// the use of this software, even if advised of the possibility of such damage.
40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//M*/
42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#ifndef __OPENCV_VIDEOSTAB_INPAINTINT_HPP__
44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define __OPENCV_VIDEOSTAB_INPAINTINT_HPP__
45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
46793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include <vector>
47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "opencv2/core.hpp"
48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "opencv2/videostab/optical_flow.hpp"
49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "opencv2/videostab/fast_marching.hpp"
50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "opencv2/videostab/global_motion.hpp"
51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "opencv2/photo.hpp"
52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslernamespace cv
54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslernamespace videostab
56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//! @addtogroup videostab
59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//! @{
60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerclass CV_EXPORTS InpainterBase
62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerpublic:
64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    InpainterBase()
65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        : radius_(0), motionModel_(MM_UNKNOWN), frames_(0), motions_(0),
66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler          stabilizedFrames_(0), stabilizationMotions_(0) {}
67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
68793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual ~InpainterBase() {}
69793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setRadius(int val) { radius_ = val; }
71793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual int radius() const { return radius_; }
72793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
73793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
74793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual MotionModel motionModel() const { return motionModel_; }
75793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
76793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void inpaint(int idx, Mat &frame, Mat &mask) = 0;
77793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
78793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
79793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    // data from stabilizer
80793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
81793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setFrames(const std::vector<Mat> &val) { frames_ = &val; }
82793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual const std::vector<Mat>& frames() const { return *frames_; }
83793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
84793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setMotions(const std::vector<Mat> &val) { motions_ = &val; }
85793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual const std::vector<Mat>& motions() const { return *motions_; }
86793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
87793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setStabilizedFrames(const std::vector<Mat> &val) { stabilizedFrames_ = &val; }
88793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual const std::vector<Mat>& stabilizedFrames() const { return *stabilizedFrames_; }
89793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
90793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setStabilizationMotions(const std::vector<Mat> &val) { stabilizationMotions_ = &val; }
91793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual const std::vector<Mat>& stabilizationMotions() const { return *stabilizationMotions_; }
92793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
93793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerprotected:
94793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int radius_;
95793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    MotionModel motionModel_;
96793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const std::vector<Mat> *frames_;
97793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const std::vector<Mat> *motions_;
98793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const std::vector<Mat> *stabilizedFrames_;
99793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    const std::vector<Mat> *stabilizationMotions_;
100793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
101793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
102793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerclass CV_EXPORTS NullInpainter : public InpainterBase
103793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
104793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerpublic:
105793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void inpaint(int /*idx*/, Mat &/*frame*/, Mat &/*mask*/) {}
106793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
107793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
108793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerclass CV_EXPORTS InpaintingPipeline : public InpainterBase
109793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
110793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerpublic:
111793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    void pushBack(Ptr<InpainterBase> inpainter) { inpainters_.push_back(inpainter); }
112793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    bool empty() const { return inpainters_.empty(); }
113793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
114793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setRadius(int val);
115793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setMotionModel(MotionModel val);
116793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setFrames(const std::vector<Mat> &val);
117793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setMotions(const std::vector<Mat> &val);
118793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setStabilizedFrames(const std::vector<Mat> &val);
119793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void setStabilizationMotions(const std::vector<Mat> &val);
120793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
121793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void inpaint(int idx, Mat &frame, Mat &mask);
122793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
123793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerprivate:
124793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    std::vector<Ptr<InpainterBase> > inpainters_;
125793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
126793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
127793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerclass CV_EXPORTS ConsistentMosaicInpainter : public InpainterBase
128793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
129793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerpublic:
130793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ConsistentMosaicInpainter();
131793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
132793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    void setStdevThresh(float val) { stdevThresh_ = val; }
133793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    float stdevThresh() const { return stdevThresh_; }
134793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
135793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void inpaint(int idx, Mat &frame, Mat &mask);
136793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
137793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerprivate:
138793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    float stdevThresh_;
139793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
140793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
141793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerclass CV_EXPORTS MotionInpainter : public InpainterBase
142793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
143793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerpublic:
144793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    MotionInpainter();
145793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
146793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    void setOptFlowEstimator(Ptr<IDenseOptFlowEstimator> val) { optFlowEstimator_ = val; }
147793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Ptr<IDenseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
148793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
149793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    void setFlowErrorThreshold(float val) { flowErrorThreshold_ = val; }
150793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    float flowErrorThreshold() const { return flowErrorThreshold_; }
151793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
152793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    void setDistThreshold(float val) { distThresh_ = val; }
153793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    float distThresh() const { return distThresh_; }
154793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
155793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    void setBorderMode(int val) { borderMode_ = val; }
156793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int borderMode() const { return borderMode_; }
157793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
158793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void inpaint(int idx, Mat &frame, Mat &mask);
159793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
160793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerprivate:
161793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    FastMarchingMethod fmm_;
162793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Ptr<IDenseOptFlowEstimator> optFlowEstimator_;
163793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    float flowErrorThreshold_;
164793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    float distThresh_;
165793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int borderMode_;
166793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
167793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Mat frame1_, transformedFrame1_;
168793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Mat_<uchar> grayFrame_, transformedGrayFrame1_;
169793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Mat_<uchar> mask1_, transformedMask1_;
170793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Mat_<float> flowX_, flowY_, flowErrors_;
171793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Mat_<uchar> flowMask_;
172793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
173793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
174793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerclass CV_EXPORTS ColorAverageInpainter : public InpainterBase
175793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
176793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerpublic:
177793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void inpaint(int idx, Mat &frame, Mat &mask);
178793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
179793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerprivate:
180793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    FastMarchingMethod fmm_;
181793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
182793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
183793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerclass CV_EXPORTS ColorInpainter : public InpainterBase
184793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{
185793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerpublic:
186793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    ColorInpainter(int method = INPAINT_TELEA, double radius = 2.);
187793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
188793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    virtual void inpaint(int idx, Mat &frame, Mat &mask);
189793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
190793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerprivate:
191793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    int method_;
192793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    double radius_;
193793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler    Mat invMask_;
194793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler};
195793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
196793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerinline ColorInpainter::ColorInpainter(int _method, double _radius)
197793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        : method_(_method), radius_(_radius) {}
198793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
199793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerCV_EXPORTS void calcFlowMask(
200793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        const Mat &flowX, const Mat &flowY, const Mat &errors, float maxError,
201793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        const Mat &mask0, const Mat &mask1, Mat &flowMask);
202793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
203793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerCV_EXPORTS void completeFrameAccordingToFlow(
204793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        const Mat &flowMask, const Mat &flowX, const Mat &flowY, const Mat &frame1, const Mat &mask1,
205793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler        float distThresh, Mat& frame0, Mat &mask0);
206793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
207793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//! @}
208793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
209793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} // namespace videostab
210793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} // namespace cv
211793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
212793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
213