exposure_compensate.hpp 11 KB

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