exposure_compensate.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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), similarity_threshold_(1) {}
  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. CV_WRAP void setSimilarityThreshold(double similarity_threshold) { similarity_threshold_ = similarity_threshold; }
  118. CV_WRAP double getSimilarityThreshold() const { return similarity_threshold_; }
  119. void prepareSimilarityMask(const std::vector<Point> &corners, const std::vector<UMat> &images);
  120. std::vector<double> gains() const;
  121. private:
  122. UMat buildSimilarityMask(InputArray src_array1, InputArray src_array2);
  123. Mat_<double> gains_;
  124. int nr_feeds_;
  125. double similarity_threshold_;
  126. std::vector<UMat> similarities_;
  127. };
  128. /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image
  129. intensities on each channel independently.
  130. */
  131. class CV_EXPORTS_W ChannelsCompensator : public ExposureCompensator
  132. {
  133. public:
  134. CV_WRAP ChannelsCompensator(int nr_feeds=1)
  135. : nr_feeds_(nr_feeds), similarity_threshold_(1) {}
  136. void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
  137. const std::vector<std::pair<UMat,uchar> > &masks) CV_OVERRIDE;
  138. CV_WRAP void apply(int index, Point corner, InputOutputArray image, InputArray mask) CV_OVERRIDE;
  139. CV_WRAP void getMatGains(CV_OUT std::vector<Mat>& umv) CV_OVERRIDE;
  140. CV_WRAP void setMatGains(std::vector<Mat>& umv) CV_OVERRIDE;
  141. CV_WRAP void setNrFeeds(int nr_feeds) { nr_feeds_ = nr_feeds; }
  142. CV_WRAP int getNrFeeds() { return nr_feeds_; }
  143. CV_WRAP void setSimilarityThreshold(double similarity_threshold) { similarity_threshold_ = similarity_threshold; }
  144. CV_WRAP double getSimilarityThreshold() const { return similarity_threshold_; }
  145. std::vector<Scalar> gains() const { return gains_; }
  146. private:
  147. std::vector<Scalar> gains_;
  148. int nr_feeds_;
  149. double similarity_threshold_;
  150. };
  151. /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image blocks.
  152. */
  153. class CV_EXPORTS_W BlocksCompensator : public ExposureCompensator
  154. {
  155. public:
  156. BlocksCompensator(int bl_width=32, int bl_height=32, int nr_feeds=1)
  157. : bl_width_(bl_width), bl_height_(bl_height), nr_feeds_(nr_feeds), nr_gain_filtering_iterations_(2),
  158. similarity_threshold_(1) {}
  159. CV_WRAP void apply(int index, Point corner, InputOutputArray image, InputArray mask) CV_OVERRIDE;
  160. CV_WRAP void getMatGains(CV_OUT std::vector<Mat>& umv) CV_OVERRIDE;
  161. CV_WRAP void setMatGains(std::vector<Mat>& umv) CV_OVERRIDE;
  162. CV_WRAP void setNrFeeds(int nr_feeds) { nr_feeds_ = nr_feeds; }
  163. CV_WRAP int getNrFeeds() { return nr_feeds_; }
  164. CV_WRAP void setSimilarityThreshold(double similarity_threshold) { similarity_threshold_ = similarity_threshold; }
  165. CV_WRAP double getSimilarityThreshold() const { return similarity_threshold_; }
  166. CV_WRAP void setBlockSize(int width, int height) { bl_width_ = width; bl_height_ = height; }
  167. CV_WRAP void setBlockSize(Size size) { setBlockSize(size.width, size.height); }
  168. CV_WRAP Size getBlockSize() const { return Size(bl_width_, bl_height_); }
  169. CV_WRAP void setNrGainsFilteringIterations(int nr_iterations) { nr_gain_filtering_iterations_ = nr_iterations; }
  170. CV_WRAP int getNrGainsFilteringIterations() const { return nr_gain_filtering_iterations_; }
  171. protected:
  172. template<class Compensator>
  173. void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
  174. const std::vector<std::pair<UMat,uchar> > &masks);
  175. private:
  176. UMat getGainMap(const GainCompensator& compensator, int bl_idx, Size bl_per_img);
  177. UMat getGainMap(const ChannelsCompensator& compensator, int bl_idx, Size bl_per_img);
  178. int bl_width_, bl_height_;
  179. std::vector<UMat> gain_maps_;
  180. int nr_feeds_;
  181. int nr_gain_filtering_iterations_;
  182. double similarity_threshold_;
  183. };
  184. /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image block
  185. intensities, see @cite UES01 for details.
  186. */
  187. class CV_EXPORTS_W BlocksGainCompensator : public BlocksCompensator
  188. {
  189. public:
  190. // This Constructor only exists to make source level compatibility detector happy
  191. CV_WRAP BlocksGainCompensator(int bl_width = 32, int bl_height = 32)
  192. : BlocksGainCompensator(bl_width, bl_height, 1) {}
  193. CV_WRAP BlocksGainCompensator(int bl_width, int bl_height, int nr_feeds)
  194. : BlocksCompensator(bl_width, bl_height, nr_feeds) {}
  195. void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
  196. const std::vector<std::pair<UMat,uchar> > &masks) CV_OVERRIDE;
  197. // This function only exists to make source level compatibility detector happy
  198. CV_WRAP void apply(int index, Point corner, InputOutputArray image, InputArray mask) CV_OVERRIDE {
  199. BlocksCompensator::apply(index, corner, image, mask); }
  200. // This function only exists to make source level compatibility detector happy
  201. CV_WRAP void getMatGains(CV_OUT std::vector<Mat>& umv) CV_OVERRIDE { BlocksCompensator::getMatGains(umv); }
  202. // This function only exists to make source level compatibility detector happy
  203. CV_WRAP void setMatGains(std::vector<Mat>& umv) CV_OVERRIDE { BlocksCompensator::setMatGains(umv); }
  204. };
  205. /** @brief Exposure compensator which tries to remove exposure related artifacts by adjusting image block
  206. on each channel.
  207. */
  208. class CV_EXPORTS_W BlocksChannelsCompensator : public BlocksCompensator
  209. {
  210. public:
  211. CV_WRAP BlocksChannelsCompensator(int bl_width=32, int bl_height=32, int nr_feeds=1)
  212. : BlocksCompensator(bl_width, bl_height, nr_feeds) {}
  213. void feed(const std::vector<Point> &corners, const std::vector<UMat> &images,
  214. const std::vector<std::pair<UMat,uchar> > &masks) CV_OVERRIDE;
  215. };
  216. //! @}
  217. } // namespace detail
  218. } // namespace cv
  219. #endif // OPENCV_STITCHING_EXPOSURE_COMPENSATE_HPP