blenders.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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, 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_STITCHING_BLENDERS_HPP
  43. #define OPENCV_STITCHING_BLENDERS_HPP
  44. #if defined(NO)
  45. # warning Detected Apple 'NO' macro definition, it can cause build conflicts. Please, include this header before any Apple headers.
  46. #endif
  47. #include "opencv2/core.hpp"
  48. #include "opencv2/core/cuda.hpp"
  49. namespace cv {
  50. namespace detail {
  51. //! @addtogroup stitching_blend
  52. //! @{
  53. /** @brief Base class for all blenders.
  54. Simple blender which puts one image over another
  55. */
  56. class CV_EXPORTS_W Blender
  57. {
  58. public:
  59. virtual ~Blender() {}
  60. enum { NO, FEATHER, MULTI_BAND };
  61. CV_WRAP static Ptr<Blender> createDefault(int type, bool try_gpu = false);
  62. /** @brief Prepares the blender for blending.
  63. @param corners Source images top-left corners
  64. @param sizes Source image sizes
  65. */
  66. CV_WRAP virtual void prepare(const std::vector<Point> &corners, const std::vector<Size> &sizes);
  67. /** @overload */
  68. CV_WRAP virtual void prepare(Rect dst_roi);
  69. /** @brief Processes the image.
  70. @param img Source image
  71. @param mask Source image mask
  72. @param tl Source image top-left corners
  73. */
  74. CV_WRAP virtual void feed(InputArray img, InputArray mask, Point tl);
  75. /** @brief Blends and returns the final pano.
  76. @param dst Final pano
  77. @param dst_mask Final pano mask
  78. */
  79. CV_WRAP virtual void blend(CV_IN_OUT InputOutputArray dst,CV_IN_OUT InputOutputArray dst_mask);
  80. protected:
  81. UMat dst_, dst_mask_;
  82. Rect dst_roi_;
  83. };
  84. /** @brief Simple blender which mixes images at its borders.
  85. */
  86. class CV_EXPORTS_W FeatherBlender : public Blender
  87. {
  88. public:
  89. CV_WRAP FeatherBlender(float sharpness = 0.02f);
  90. CV_WRAP float sharpness() const { return sharpness_; }
  91. CV_WRAP void setSharpness(float val) { sharpness_ = val; }
  92. CV_WRAP void prepare(Rect dst_roi) CV_OVERRIDE;
  93. CV_WRAP void feed(InputArray img, InputArray mask, Point tl) CV_OVERRIDE;
  94. CV_WRAP void blend(InputOutputArray dst, InputOutputArray dst_mask) CV_OVERRIDE;
  95. //! Creates weight maps for fixed set of source images by their masks and top-left corners.
  96. //! Final image can be obtained by simple weighting of the source images.
  97. CV_WRAP Rect createWeightMaps(const std::vector<UMat> &masks, const std::vector<Point> &corners,
  98. CV_IN_OUT std::vector<UMat> &weight_maps);
  99. private:
  100. float sharpness_;
  101. UMat weight_map_;
  102. UMat dst_weight_map_;
  103. };
  104. inline FeatherBlender::FeatherBlender(float _sharpness) { setSharpness(_sharpness); }
  105. /** @brief Blender which uses multi-band blending algorithm (see @cite BA83).
  106. */
  107. class CV_EXPORTS_W MultiBandBlender : public Blender
  108. {
  109. public:
  110. CV_WRAP MultiBandBlender(int try_gpu = false, int num_bands = 5, int weight_type = CV_32F);
  111. CV_WRAP int numBands() const { return actual_num_bands_; }
  112. CV_WRAP void setNumBands(int val) { actual_num_bands_ = val; }
  113. CV_WRAP void prepare(Rect dst_roi) CV_OVERRIDE;
  114. CV_WRAP void feed(InputArray img, InputArray mask, Point tl) CV_OVERRIDE;
  115. CV_WRAP void blend(CV_IN_OUT InputOutputArray dst, CV_IN_OUT InputOutputArray dst_mask) CV_OVERRIDE;
  116. private:
  117. int actual_num_bands_, num_bands_;
  118. std::vector<UMat> dst_pyr_laplace_;
  119. std::vector<UMat> dst_band_weights_;
  120. Rect dst_roi_final_;
  121. bool can_use_gpu_;
  122. int weight_type_; //CV_32F or CV_16S
  123. #if defined(HAVE_OPENCV_CUDAARITHM) && defined(HAVE_OPENCV_CUDAWARPING)
  124. std::vector<cuda::GpuMat> gpu_dst_pyr_laplace_;
  125. std::vector<cuda::GpuMat> gpu_dst_band_weights_;
  126. std::vector<Point> gpu_tl_points_;
  127. std::vector<cuda::GpuMat> gpu_imgs_with_border_;
  128. std::vector<std::vector<cuda::GpuMat> > gpu_weight_pyr_gauss_vec_;
  129. std::vector<std::vector<cuda::GpuMat> > gpu_src_pyr_laplace_vec_;
  130. std::vector<std::vector<cuda::GpuMat> > gpu_ups_;
  131. cuda::GpuMat gpu_dst_mask_;
  132. cuda::GpuMat gpu_mask_;
  133. cuda::GpuMat gpu_img_;
  134. cuda::GpuMat gpu_weight_map_;
  135. cuda::GpuMat gpu_add_mask_;
  136. int gpu_feed_idx_;
  137. bool gpu_initialized_;
  138. #endif
  139. };
  140. //////////////////////////////////////////////////////////////////////////////
  141. // Auxiliary functions
  142. void CV_EXPORTS_W normalizeUsingWeightMap(InputArray weight, CV_IN_OUT InputOutputArray src);
  143. void CV_EXPORTS_W createWeightMap(InputArray mask, float sharpness, CV_IN_OUT InputOutputArray weight);
  144. void CV_EXPORTS_W createLaplacePyr(InputArray img, int num_levels, CV_IN_OUT std::vector<UMat>& pyr);
  145. void CV_EXPORTS_W createLaplacePyrGpu(InputArray img, int num_levels, CV_IN_OUT std::vector<UMat>& pyr);
  146. // Restores source image
  147. void CV_EXPORTS_W restoreImageFromLaplacePyr(CV_IN_OUT std::vector<UMat>& pyr);
  148. void CV_EXPORTS_W restoreImageFromLaplacePyrGpu(CV_IN_OUT std::vector<UMat>& pyr);
  149. //! @}
  150. } // namespace detail
  151. } // namespace cv
  152. #endif // OPENCV_STITCHING_BLENDERS_HPP