inpainting.hpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. #ifndef OPENCV_VIDEOSTAB_INPAINTINT_HPP
  43. #define OPENCV_VIDEOSTAB_INPAINTINT_HPP
  44. #include <vector>
  45. #include "opencv2/core.hpp"
  46. #include "opencv2/videostab/optical_flow.hpp"
  47. #include "opencv2/videostab/fast_marching.hpp"
  48. #include "opencv2/videostab/global_motion.hpp"
  49. #include "opencv2/photo.hpp"
  50. namespace cv
  51. {
  52. namespace videostab
  53. {
  54. //! @addtogroup videostab
  55. //! @{
  56. class CV_EXPORTS InpainterBase
  57. {
  58. public:
  59. InpainterBase()
  60. : radius_(0), motionModel_(MM_UNKNOWN), frames_(0), motions_(0),
  61. stabilizedFrames_(0), stabilizationMotions_(0) {}
  62. virtual ~InpainterBase() {}
  63. virtual void setRadius(int val) { radius_ = val; }
  64. virtual int radius() const { return radius_; }
  65. virtual void setMotionModel(MotionModel val) { motionModel_ = val; }
  66. virtual MotionModel motionModel() const { return motionModel_; }
  67. virtual void inpaint(int idx, Mat &frame, Mat &mask) = 0;
  68. // data from stabilizer
  69. virtual void setFrames(const std::vector<Mat> &val) { frames_ = &val; }
  70. virtual const std::vector<Mat>& frames() const { return *frames_; }
  71. virtual void setMotions(const std::vector<Mat> &val) { motions_ = &val; }
  72. virtual const std::vector<Mat>& motions() const { return *motions_; }
  73. virtual void setStabilizedFrames(const std::vector<Mat> &val) { stabilizedFrames_ = &val; }
  74. virtual const std::vector<Mat>& stabilizedFrames() const { return *stabilizedFrames_; }
  75. virtual void setStabilizationMotions(const std::vector<Mat> &val) { stabilizationMotions_ = &val; }
  76. virtual const std::vector<Mat>& stabilizationMotions() const { return *stabilizationMotions_; }
  77. protected:
  78. int radius_;
  79. MotionModel motionModel_;
  80. const std::vector<Mat> *frames_;
  81. const std::vector<Mat> *motions_;
  82. const std::vector<Mat> *stabilizedFrames_;
  83. const std::vector<Mat> *stabilizationMotions_;
  84. };
  85. class CV_EXPORTS NullInpainter : public InpainterBase
  86. {
  87. public:
  88. virtual void inpaint(int /*idx*/, Mat &/*frame*/, Mat &/*mask*/) CV_OVERRIDE {}
  89. };
  90. class CV_EXPORTS InpaintingPipeline : public InpainterBase
  91. {
  92. public:
  93. void pushBack(Ptr<InpainterBase> inpainter) { inpainters_.push_back(inpainter); }
  94. bool empty() const { return inpainters_.empty(); }
  95. virtual void setRadius(int val) CV_OVERRIDE;
  96. virtual void setMotionModel(MotionModel val) CV_OVERRIDE;
  97. virtual void setFrames(const std::vector<Mat> &val) CV_OVERRIDE;
  98. virtual void setMotions(const std::vector<Mat> &val) CV_OVERRIDE;
  99. virtual void setStabilizedFrames(const std::vector<Mat> &val) CV_OVERRIDE;
  100. virtual void setStabilizationMotions(const std::vector<Mat> &val) CV_OVERRIDE;
  101. virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
  102. private:
  103. std::vector<Ptr<InpainterBase> > inpainters_;
  104. };
  105. class CV_EXPORTS ConsistentMosaicInpainter : public InpainterBase
  106. {
  107. public:
  108. ConsistentMosaicInpainter();
  109. void setStdevThresh(float val) { stdevThresh_ = val; }
  110. float stdevThresh() const { return stdevThresh_; }
  111. virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
  112. private:
  113. float stdevThresh_;
  114. };
  115. class CV_EXPORTS MotionInpainter : public InpainterBase
  116. {
  117. public:
  118. MotionInpainter();
  119. void setOptFlowEstimator(Ptr<IDenseOptFlowEstimator> val) { optFlowEstimator_ = val; }
  120. Ptr<IDenseOptFlowEstimator> optFlowEstimator() const { return optFlowEstimator_; }
  121. void setFlowErrorThreshold(float val) { flowErrorThreshold_ = val; }
  122. float flowErrorThreshold() const { return flowErrorThreshold_; }
  123. void setDistThreshold(float val) { distThresh_ = val; }
  124. float distThresh() const { return distThresh_; }
  125. void setBorderMode(int val) { borderMode_ = val; }
  126. int borderMode() const { return borderMode_; }
  127. virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
  128. private:
  129. FastMarchingMethod fmm_;
  130. Ptr<IDenseOptFlowEstimator> optFlowEstimator_;
  131. float flowErrorThreshold_;
  132. float distThresh_;
  133. int borderMode_;
  134. Mat frame1_, transformedFrame1_;
  135. Mat_<uchar> grayFrame_, transformedGrayFrame1_;
  136. Mat_<uchar> mask1_, transformedMask1_;
  137. Mat_<float> flowX_, flowY_, flowErrors_;
  138. Mat_<uchar> flowMask_;
  139. };
  140. class CV_EXPORTS ColorAverageInpainter : public InpainterBase
  141. {
  142. public:
  143. virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
  144. private:
  145. FastMarchingMethod fmm_;
  146. };
  147. class CV_EXPORTS ColorInpainter : public InpainterBase
  148. {
  149. public:
  150. ColorInpainter(int method = INPAINT_TELEA, double radius = 2.);
  151. virtual void inpaint(int idx, Mat &frame, Mat &mask) CV_OVERRIDE;
  152. private:
  153. int method_;
  154. double radius_;
  155. Mat invMask_;
  156. };
  157. inline ColorInpainter::ColorInpainter(int _method, double _radius)
  158. : method_(_method), radius_(_radius) {}
  159. CV_EXPORTS void calcFlowMask(
  160. const Mat &flowX, const Mat &flowY, const Mat &errors, float maxError,
  161. const Mat &mask0, const Mat &mask1, Mat &flowMask);
  162. CV_EXPORTS void completeFrameAccordingToFlow(
  163. const Mat &flowMask, const Mat &flowX, const Mat &flowY, const Mat &frame1, const Mat &mask1,
  164. float distThresh, Mat& frame0, Mat &mask0);
  165. //! @}
  166. } // namespace videostab
  167. } // namespace cv
  168. #endif