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