blenders.hpp 5.9 KB

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