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_EXPOSURE_COMPENSATE_HPP
44 #define OPENCV_STITCHING_EXPOSURE_COMPENSATE_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 
52 namespace cv {
53 namespace detail {
54 
55 //! @addtogroup stitching_exposure
56 //! @{
57 
58 /** @brief Base class for all exposure compensators.
59  */
60 class CV_EXPORTS_W ExposureCompensator
61 {
62 public:
ExposureCompensator()63     ExposureCompensator(): updateGain(true) {}
~ExposureCompensator()64     virtual ~ExposureCompensator() {}
65 
66     enum { NO, GAIN, GAIN_BLOCKS, CHANNELS, CHANNELS_BLOCKS };
67     CV_WRAP static Ptr<ExposureCompensator> createDefault(int type);
68 
69     /**
70     @param corners Source image top-left corners
71     @param images Source images
72     @param masks Image masks to update (second value in pair specifies the value which should be used
73     to detect where image is)
74         */
75     CV_WRAP void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
76         const std::vector<UMat> &masks);
77     /** @overload */
78     virtual void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
79         const std::vector<std::pair<UMat, uchar> > &masks) = 0;
80     /** @brief Compensate exposure in the specified image.
81 
82     @param index Image index
83     @param corner Image top-left corner
84     @param image Image to process
85     @param mask Image mask
86         */
87     CV_WRAP virtual void apply(int index, Point corner, InputOutputArray image, InputArray mask) = 0;
getMatGains(CV_OUT std::vector<Mat> &)88     CV_WRAP virtual void getMatGains(CV_OUT std::vector<Mat>& ) {CV_Error(Error::StsInternal, "");};
setMatGains(std::vector<Mat> &)89     CV_WRAP virtual void setMatGains(std::vector<Mat>& ) { CV_Error(Error::StsInternal, ""); };
setUpdateGain(bool b)90     CV_WRAP void setUpdateGain(bool b) { updateGain = b; };
getUpdateGain()91     CV_WRAP bool getUpdateGain() { return updateGain; };
92 protected :
93     bool updateGain;
94 };
95 
96 /** @brief Stub exposure compensator which does nothing.
97  */
98 class CV_EXPORTS_W NoExposureCompensator : public ExposureCompensator
99 {
100 public:
feed(const std::vector<Point> &,const std::vector<UMat> &,const std::vector<std::pair<UMat,uchar>> &)101     void feed(const std::vector<Point> &/*corners*/, const std::vector<UMat> &/*images*/,
102               const std::vector<std::pair<UMat,uchar> > &/*masks*/) CV_OVERRIDE { }
apply(int,Point,InputOutputArray,InputArray)103     CV_WRAP void apply(int /*index*/, Point /*corner*/, InputOutputArray /*image*/, InputArray /*mask*/) CV_OVERRIDE { }
getMatGains(CV_OUT std::vector<Mat> & umv)104     CV_WRAP void getMatGains(CV_OUT std::vector<Mat>& umv) CV_OVERRIDE { umv.clear(); return; };
setMatGains(std::vector<Mat> & umv)105     CV_WRAP void setMatGains(std::vector<Mat>& umv) CV_OVERRIDE { umv.clear(); return; };
106 };
107 
108 /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image
109 intensities, see @cite BL07 and @cite WJ10 for details.
110  */
111 class CV_EXPORTS_W GainCompensator : public ExposureCompensator
112 {
113 public:
114     // This Constructor only exists to make source level compatibility detector happy
GainCompensator()115     CV_WRAP GainCompensator()
116             : GainCompensator(1) {}
GainCompensator(int nr_feeds)117     CV_WRAP GainCompensator(int nr_feeds)
118             : nr_feeds_(nr_feeds), similarity_threshold_(1) {}
119     void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
120               const std::vector<std::pair<UMat,uchar> > &masks) CV_OVERRIDE;
121     void singleFeed(const std::vector<Point> &corners, const std::vector<UMat> &images,
122                     const std::vector<std::pair<UMat,uchar> > &masks);
123     CV_WRAP void apply(int index, Point corner, InputOutputArray image, InputArray mask) CV_OVERRIDE;
124     CV_WRAP void getMatGains(CV_OUT std::vector<Mat>& umv) CV_OVERRIDE ;
125     CV_WRAP void setMatGains(std::vector<Mat>& umv) CV_OVERRIDE ;
setNrFeeds(int nr_feeds)126     CV_WRAP void setNrFeeds(int nr_feeds) { nr_feeds_ = nr_feeds; }
getNrFeeds()127     CV_WRAP int getNrFeeds() { return nr_feeds_; }
setSimilarityThreshold(double similarity_threshold)128     CV_WRAP void setSimilarityThreshold(double similarity_threshold) { similarity_threshold_ = similarity_threshold; }
getSimilarityThreshold() const129     CV_WRAP double getSimilarityThreshold() const { return similarity_threshold_; }
130     void prepareSimilarityMask(const std::vector<Point> &corners, const std::vector<UMat> &images);
131     std::vector<double> gains() const;
132 
133 private:
134     UMat buildSimilarityMask(InputArray src_array1, InputArray src_array2);
135 
136     Mat_<double> gains_;
137     int nr_feeds_;
138     double similarity_threshold_;
139     std::vector<UMat> similarities_;
140 };
141 
142 /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image
143 intensities on each channel independently.
144  */
145 class CV_EXPORTS_W ChannelsCompensator : public ExposureCompensator
146 {
147 public:
ChannelsCompensator(int nr_feeds=1)148     CV_WRAP ChannelsCompensator(int nr_feeds=1)
149         : nr_feeds_(nr_feeds), similarity_threshold_(1) {}
150     void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
151               const std::vector<std::pair<UMat,uchar> > &masks) CV_OVERRIDE;
152     CV_WRAP void apply(int index, Point corner, InputOutputArray image, InputArray mask) CV_OVERRIDE;
153     CV_WRAP void getMatGains(CV_OUT std::vector<Mat>& umv) CV_OVERRIDE;
154     CV_WRAP void setMatGains(std::vector<Mat>& umv) CV_OVERRIDE;
setNrFeeds(int nr_feeds)155     CV_WRAP void setNrFeeds(int nr_feeds) { nr_feeds_ = nr_feeds; }
getNrFeeds()156     CV_WRAP int getNrFeeds() { return nr_feeds_; }
setSimilarityThreshold(double similarity_threshold)157     CV_WRAP void setSimilarityThreshold(double similarity_threshold) { similarity_threshold_ = similarity_threshold; }
getSimilarityThreshold() const158     CV_WRAP double getSimilarityThreshold() const { return similarity_threshold_; }
gains() const159     std::vector<Scalar> gains() const { return gains_; }
160 
161 private:
162     std::vector<Scalar> gains_;
163     int nr_feeds_;
164     double similarity_threshold_;
165 };
166 
167 /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image blocks.
168  */
169 class CV_EXPORTS_W BlocksCompensator : public ExposureCompensator
170 {
171 public:
BlocksCompensator(int bl_width=32,int bl_height=32,int nr_feeds=1)172     BlocksCompensator(int bl_width=32, int bl_height=32, int nr_feeds=1)
173             : bl_width_(bl_width), bl_height_(bl_height), nr_feeds_(nr_feeds), nr_gain_filtering_iterations_(2),
174               similarity_threshold_(1) {}
175     CV_WRAP void apply(int index, Point corner, InputOutputArray image, InputArray mask) CV_OVERRIDE;
176     CV_WRAP void getMatGains(CV_OUT std::vector<Mat>& umv) CV_OVERRIDE;
177     CV_WRAP void setMatGains(std::vector<Mat>& umv) CV_OVERRIDE;
setNrFeeds(int nr_feeds)178     CV_WRAP void setNrFeeds(int nr_feeds) { nr_feeds_ = nr_feeds; }
getNrFeeds()179     CV_WRAP int getNrFeeds() { return nr_feeds_; }
setSimilarityThreshold(double similarity_threshold)180     CV_WRAP void setSimilarityThreshold(double similarity_threshold) { similarity_threshold_ = similarity_threshold; }
getSimilarityThreshold() const181     CV_WRAP double getSimilarityThreshold() const { return similarity_threshold_; }
setBlockSize(int width,int height)182     CV_WRAP void setBlockSize(int width, int height) { bl_width_ = width; bl_height_ = height; }
setBlockSize(Size size)183     CV_WRAP void setBlockSize(Size size) { setBlockSize(size.width, size.height); }
getBlockSize() const184     CV_WRAP Size getBlockSize() const { return Size(bl_width_, bl_height_); }
setNrGainsFilteringIterations(int nr_iterations)185     CV_WRAP void setNrGainsFilteringIterations(int nr_iterations) { nr_gain_filtering_iterations_ = nr_iterations; }
getNrGainsFilteringIterations() const186     CV_WRAP int getNrGainsFilteringIterations() const { return nr_gain_filtering_iterations_; }
187 
188 protected:
189     template<class Compensator>
190     void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
191               const std::vector<std::pair<UMat,uchar> > &masks);
192 
193 private:
194     UMat getGainMap(const GainCompensator& compensator, int bl_idx, Size bl_per_img);
195     UMat getGainMap(const ChannelsCompensator& compensator, int bl_idx, Size bl_per_img);
196 
197     int bl_width_, bl_height_;
198     std::vector<UMat> gain_maps_;
199     int nr_feeds_;
200     int nr_gain_filtering_iterations_;
201     double similarity_threshold_;
202 };
203 
204 /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image block
205 intensities, see @cite UES01 for details.
206  */
207 class CV_EXPORTS_W BlocksGainCompensator : public BlocksCompensator
208 {
209 public:
210     // This Constructor only exists to make source level compatibility detector happy
BlocksGainCompensator(int bl_width=32,int bl_height=32)211     CV_WRAP BlocksGainCompensator(int bl_width = 32, int bl_height = 32)
212             : BlocksGainCompensator(bl_width, bl_height, 1) {}
BlocksGainCompensator(int bl_width,int bl_height,int nr_feeds)213     CV_WRAP BlocksGainCompensator(int bl_width, int bl_height, int nr_feeds)
214             : BlocksCompensator(bl_width, bl_height, nr_feeds) {}
215 
216     void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
217               const std::vector<std::pair<UMat,uchar> > &masks) CV_OVERRIDE;
218 
219     // This function only exists to make source level compatibility detector happy
apply(int index,Point corner,InputOutputArray image,InputArray mask)220     CV_WRAP void apply(int index, Point corner, InputOutputArray image, InputArray mask) CV_OVERRIDE {
221         BlocksCompensator::apply(index, corner, image, mask); }
222     // This function only exists to make source level compatibility detector happy
getMatGains(CV_OUT std::vector<Mat> & umv)223     CV_WRAP void getMatGains(CV_OUT std::vector<Mat>& umv) CV_OVERRIDE { BlocksCompensator::getMatGains(umv); }
224     // This function only exists to make source level compatibility detector happy
setMatGains(std::vector<Mat> & umv)225     CV_WRAP void setMatGains(std::vector<Mat>& umv) CV_OVERRIDE { BlocksCompensator::setMatGains(umv); }
226 };
227 
228 /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image block
229 on each channel.
230  */
231 class CV_EXPORTS_W BlocksChannelsCompensator : public BlocksCompensator
232 {
233 public:
BlocksChannelsCompensator(int bl_width=32,int bl_height=32,int nr_feeds=1)234     CV_WRAP BlocksChannelsCompensator(int bl_width=32, int bl_height=32, int nr_feeds=1)
235             : BlocksCompensator(bl_width, bl_height, nr_feeds) {}
236 
237     void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
238               const std::vector<std::pair<UMat,uchar> > &masks) CV_OVERRIDE;
239 };
240 //! @}
241 
242 } // namespace detail
243 } // namespace cv
244 
245 #endif // OPENCV_STITCHING_EXPOSURE_COMPENSATE_HPP
246