white_balance.hpp 9.2 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-2011, 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_SIMPLE_COLOR_BALANCE_HPP__
  43. #define __OPENCV_SIMPLE_COLOR_BALANCE_HPP__
  44. /** @file
  45. @date Jun 26, 2014
  46. @author Yury Gitman
  47. */
  48. #include <opencv2/core.hpp>
  49. namespace cv
  50. {
  51. namespace xphoto
  52. {
  53. //! @addtogroup xphoto
  54. //! @{
  55. /** @brief The base class for auto white balance algorithms.
  56. */
  57. class CV_EXPORTS_W WhiteBalancer : public Algorithm
  58. {
  59. public:
  60. /** @brief Applies white balancing to the input image
  61. @param src Input image
  62. @param dst White balancing result
  63. @sa cvtColor, equalizeHist
  64. */
  65. CV_WRAP virtual void balanceWhite(InputArray src, OutputArray dst) = 0;
  66. };
  67. /** @brief A simple white balance algorithm that works by independently stretching
  68. each of the input image channels to the specified range. For increased robustness
  69. it ignores the top and bottom \f$p\%\f$ of pixel values.
  70. */
  71. class CV_EXPORTS_W SimpleWB : public WhiteBalancer
  72. {
  73. public:
  74. /** @brief Input image range minimum value
  75. @see setInputMin */
  76. CV_WRAP virtual float getInputMin() const = 0;
  77. /** @copybrief getInputMin @see getInputMin */
  78. CV_WRAP virtual void setInputMin(float val) = 0;
  79. /** @brief Input image range maximum value
  80. @see setInputMax */
  81. CV_WRAP virtual float getInputMax() const = 0;
  82. /** @copybrief getInputMax @see getInputMax */
  83. CV_WRAP virtual void setInputMax(float val) = 0;
  84. /** @brief Output image range minimum value
  85. @see setOutputMin */
  86. CV_WRAP virtual float getOutputMin() const = 0;
  87. /** @copybrief getOutputMin @see getOutputMin */
  88. CV_WRAP virtual void setOutputMin(float val) = 0;
  89. /** @brief Output image range maximum value
  90. @see setOutputMax */
  91. CV_WRAP virtual float getOutputMax() const = 0;
  92. /** @copybrief getOutputMax @see getOutputMax */
  93. CV_WRAP virtual void setOutputMax(float val) = 0;
  94. /** @brief Percent of top/bottom values to ignore
  95. @see setP */
  96. CV_WRAP virtual float getP() const = 0;
  97. /** @copybrief getP @see getP */
  98. CV_WRAP virtual void setP(float val) = 0;
  99. };
  100. /** @brief Creates an instance of SimpleWB
  101. */
  102. CV_EXPORTS_W Ptr<SimpleWB> createSimpleWB();
  103. /** @brief Gray-world white balance algorithm
  104. This algorithm scales the values of pixels based on a
  105. gray-world assumption which states that the average of all channels
  106. should result in a gray image.
  107. It adds a modification which thresholds pixels based on their
  108. saturation value and only uses pixels below the provided threshold in
  109. finding average pixel values.
  110. Saturation is calculated using the following for a 3-channel RGB image per
  111. pixel I and is in the range [0, 1]:
  112. \f[ \texttt{Saturation} [I] = \frac{\textrm{max}(R,G,B) - \textrm{min}(R,G,B)
  113. }{\textrm{max}(R,G,B)} \f]
  114. A threshold of 1 means that all pixels are used to white-balance, while a
  115. threshold of 0 means no pixels are used. Lower thresholds are useful in
  116. white-balancing saturated images.
  117. Currently supports images of type @ref CV_8UC3 and @ref CV_16UC3.
  118. */
  119. class CV_EXPORTS_W GrayworldWB : public WhiteBalancer
  120. {
  121. public:
  122. /** @brief Maximum saturation for a pixel to be included in the
  123. gray-world assumption
  124. @see setSaturationThreshold */
  125. CV_WRAP virtual float getSaturationThreshold() const = 0;
  126. /** @copybrief getSaturationThreshold @see getSaturationThreshold */
  127. CV_WRAP virtual void setSaturationThreshold(float val) = 0;
  128. };
  129. /** @brief Creates an instance of GrayworldWB
  130. */
  131. CV_EXPORTS_W Ptr<GrayworldWB> createGrayworldWB();
  132. /** @brief More sophisticated learning-based automatic white balance algorithm.
  133. As @ref GrayworldWB, this algorithm works by applying different gains to the input
  134. image channels, but their computation is a bit more involved compared to the
  135. simple gray-world assumption. More details about the algorithm can be found in
  136. @cite Cheng2015 .
  137. To mask out saturated pixels this function uses only pixels that satisfy the
  138. following condition:
  139. \f[ \frac{\textrm{max}(R,G,B)}{\texttt{range_max_val}} < \texttt{saturation_thresh} \f]
  140. Currently supports images of type @ref CV_8UC3 and @ref CV_16UC3.
  141. */
  142. class CV_EXPORTS_W LearningBasedWB : public WhiteBalancer
  143. {
  144. public:
  145. /** @brief Implements the feature extraction part of the algorithm.
  146. In accordance with @cite Cheng2015 , computes the following features for the input image:
  147. 1. Chromaticity of an average (R,G,B) tuple
  148. 2. Chromaticity of the brightest (R,G,B) tuple (while ignoring saturated pixels)
  149. 3. Chromaticity of the dominant (R,G,B) tuple (the one that has the highest value in the RGB histogram)
  150. 4. Mode of the chromaticity palette, that is constructed by taking 300 most common colors according to
  151. the RGB histogram and projecting them on the chromaticity plane. Mode is the most high-density point
  152. of the palette, which is computed by a straightforward fixed-bandwidth kernel density estimator with
  153. a Epanechnikov kernel function.
  154. @param src Input three-channel image (BGR color space is assumed).
  155. @param dst An array of four (r,g) chromaticity tuples corresponding to the features listed above.
  156. */
  157. CV_WRAP virtual void extractSimpleFeatures(InputArray src, OutputArray dst) = 0;
  158. /** @brief Maximum possible value of the input image (e.g. 255 for 8 bit images,
  159. 4095 for 12 bit images)
  160. @see setRangeMaxVal */
  161. CV_WRAP virtual int getRangeMaxVal() const = 0;
  162. /** @copybrief getRangeMaxVal @see getRangeMaxVal */
  163. CV_WRAP virtual void setRangeMaxVal(int val) = 0;
  164. /** @brief Threshold that is used to determine saturated pixels, i.e. pixels where at least one of the
  165. channels exceeds \f$\texttt{saturation_threshold}\times\texttt{range_max_val}\f$ are ignored.
  166. @see setSaturationThreshold */
  167. CV_WRAP virtual float getSaturationThreshold() const = 0;
  168. /** @copybrief getSaturationThreshold @see getSaturationThreshold */
  169. CV_WRAP virtual void setSaturationThreshold(float val) = 0;
  170. /** @brief Defines the size of one dimension of a three-dimensional RGB histogram that is used internally
  171. by the algorithm. It often makes sense to increase the number of bins for images with higher bit depth
  172. (e.g. 256 bins for a 12 bit image).
  173. @see setHistBinNum */
  174. CV_WRAP virtual int getHistBinNum() const = 0;
  175. /** @copybrief getHistBinNum @see getHistBinNum */
  176. CV_WRAP virtual void setHistBinNum(int val) = 0;
  177. };
  178. /** @brief Creates an instance of LearningBasedWB
  179. @param path_to_model Path to a .yml file with the model. If not specified, the default model is used
  180. */
  181. CV_EXPORTS_W Ptr<LearningBasedWB> createLearningBasedWB(const String& path_to_model = String());
  182. /** @brief Implements an efficient fixed-point approximation for applying channel gains, which is
  183. the last step of multiple white balance algorithms.
  184. @param src Input three-channel image in the BGR color space (either CV_8UC3 or CV_16UC3)
  185. @param dst Output image of the same size and type as src.
  186. @param gainB gain for the B channel
  187. @param gainG gain for the G channel
  188. @param gainR gain for the R channel
  189. */
  190. CV_EXPORTS_W void applyChannelGains(InputArray src, OutputArray dst, float gainB, float gainG, float gainR);
  191. //! @}
  192. }
  193. }
  194. #endif // __OPENCV_SIMPLE_COLOR_BALANCE_HPP__