LearningBasedWB.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.XphotoModule
  7. {
  8. // C++: class LearningBasedWB
  9. /**
  10. * More sophisticated learning-based automatic white balance algorithm.
  11. *
  12. * As REF: GrayworldWB, this algorithm works by applying different gains to the input
  13. * image channels, but their computation is a bit more involved compared to the
  14. * simple gray-world assumption. More details about the algorithm can be found in
  15. * CITE: Cheng2015 .
  16. *
  17. * To mask out saturated pixels this function uses only pixels that satisfy the
  18. * following condition:
  19. *
  20. * \( \frac{\textrm{max}(R,G,B)}{\texttt{range_max_val}} < \texttt{saturation_thresh} \)
  21. *
  22. * Currently supports images of type REF: CV_8UC3 and REF: CV_16UC3.
  23. */
  24. public class LearningBasedWB : WhiteBalancer
  25. {
  26. protected override void Dispose(bool disposing)
  27. {
  28. try
  29. {
  30. if (disposing)
  31. {
  32. }
  33. if (IsEnabledDispose)
  34. {
  35. if (nativeObj != IntPtr.Zero)
  36. xphoto_LearningBasedWB_delete(nativeObj);
  37. nativeObj = IntPtr.Zero;
  38. }
  39. }
  40. finally
  41. {
  42. base.Dispose(disposing);
  43. }
  44. }
  45. protected internal LearningBasedWB(IntPtr addr) : base(addr) { }
  46. // internal usage only
  47. public static new LearningBasedWB __fromPtr__(IntPtr addr) { return new LearningBasedWB(addr); }
  48. //
  49. // C++: void cv::xphoto::LearningBasedWB::extractSimpleFeatures(Mat src, Mat& dst)
  50. //
  51. /**
  52. * Implements the feature extraction part of the algorithm.
  53. *
  54. * In accordance with CITE: Cheng2015 , computes the following features for the input image:
  55. * 1. Chromaticity of an average (R,G,B) tuple
  56. * 2. Chromaticity of the brightest (R,G,B) tuple (while ignoring saturated pixels)
  57. * 3. Chromaticity of the dominant (R,G,B) tuple (the one that has the highest value in the RGB histogram)
  58. * 4. Mode of the chromaticity palette, that is constructed by taking 300 most common colors according to
  59. * the RGB histogram and projecting them on the chromaticity plane. Mode is the most high-density point
  60. * of the palette, which is computed by a straightforward fixed-bandwidth kernel density estimator with
  61. * a Epanechnikov kernel function.
  62. *
  63. * param src Input three-channel image (BGR color space is assumed).
  64. * param dst An array of four (r,g) chromaticity tuples corresponding to the features listed above.
  65. */
  66. public void extractSimpleFeatures(Mat src, Mat dst)
  67. {
  68. ThrowIfDisposed();
  69. if (src != null) src.ThrowIfDisposed();
  70. if (dst != null) dst.ThrowIfDisposed();
  71. xphoto_LearningBasedWB_extractSimpleFeatures_10(nativeObj, src.nativeObj, dst.nativeObj);
  72. }
  73. //
  74. // C++: int cv::xphoto::LearningBasedWB::getRangeMaxVal()
  75. //
  76. /**
  77. * Maximum possible value of the input image (e.g. 255 for 8 bit images,
  78. * 4095 for 12 bit images)
  79. * SEE: setRangeMaxVal
  80. * return automatically generated
  81. */
  82. public int getRangeMaxVal()
  83. {
  84. ThrowIfDisposed();
  85. return xphoto_LearningBasedWB_getRangeMaxVal_10(nativeObj);
  86. }
  87. //
  88. // C++: void cv::xphoto::LearningBasedWB::setRangeMaxVal(int val)
  89. //
  90. /**
  91. * getRangeMaxVal SEE: getRangeMaxVal
  92. * param val automatically generated
  93. */
  94. public void setRangeMaxVal(int val)
  95. {
  96. ThrowIfDisposed();
  97. xphoto_LearningBasedWB_setRangeMaxVal_10(nativeObj, val);
  98. }
  99. //
  100. // C++: float cv::xphoto::LearningBasedWB::getSaturationThreshold()
  101. //
  102. /**
  103. * Threshold that is used to determine saturated pixels, i.e. pixels where at least one of the
  104. * channels exceeds \(\texttt{saturation_threshold}\times\texttt{range_max_val}\) are ignored.
  105. * SEE: setSaturationThreshold
  106. * return automatically generated
  107. */
  108. public float getSaturationThreshold()
  109. {
  110. ThrowIfDisposed();
  111. return xphoto_LearningBasedWB_getSaturationThreshold_10(nativeObj);
  112. }
  113. //
  114. // C++: void cv::xphoto::LearningBasedWB::setSaturationThreshold(float val)
  115. //
  116. /**
  117. * getSaturationThreshold SEE: getSaturationThreshold
  118. * param val automatically generated
  119. */
  120. public void setSaturationThreshold(float val)
  121. {
  122. ThrowIfDisposed();
  123. xphoto_LearningBasedWB_setSaturationThreshold_10(nativeObj, val);
  124. }
  125. //
  126. // C++: int cv::xphoto::LearningBasedWB::getHistBinNum()
  127. //
  128. /**
  129. * Defines the size of one dimension of a three-dimensional RGB histogram that is used internally
  130. * by the algorithm. It often makes sense to increase the number of bins for images with higher bit depth
  131. * (e.g. 256 bins for a 12 bit image).
  132. * SEE: setHistBinNum
  133. * return automatically generated
  134. */
  135. public int getHistBinNum()
  136. {
  137. ThrowIfDisposed();
  138. return xphoto_LearningBasedWB_getHistBinNum_10(nativeObj);
  139. }
  140. //
  141. // C++: void cv::xphoto::LearningBasedWB::setHistBinNum(int val)
  142. //
  143. /**
  144. * getHistBinNum SEE: getHistBinNum
  145. * param val automatically generated
  146. */
  147. public void setHistBinNum(int val)
  148. {
  149. ThrowIfDisposed();
  150. xphoto_LearningBasedWB_setHistBinNum_10(nativeObj, val);
  151. }
  152. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  153. const string LIBNAME = "__Internal";
  154. #else
  155. const string LIBNAME = "opencvforunity";
  156. #endif
  157. // C++: void cv::xphoto::LearningBasedWB::extractSimpleFeatures(Mat src, Mat& dst)
  158. [DllImport(LIBNAME)]
  159. private static extern void xphoto_LearningBasedWB_extractSimpleFeatures_10(IntPtr nativeObj, IntPtr src_nativeObj, IntPtr dst_nativeObj);
  160. // C++: int cv::xphoto::LearningBasedWB::getRangeMaxVal()
  161. [DllImport(LIBNAME)]
  162. private static extern int xphoto_LearningBasedWB_getRangeMaxVal_10(IntPtr nativeObj);
  163. // C++: void cv::xphoto::LearningBasedWB::setRangeMaxVal(int val)
  164. [DllImport(LIBNAME)]
  165. private static extern void xphoto_LearningBasedWB_setRangeMaxVal_10(IntPtr nativeObj, int val);
  166. // C++: float cv::xphoto::LearningBasedWB::getSaturationThreshold()
  167. [DllImport(LIBNAME)]
  168. private static extern float xphoto_LearningBasedWB_getSaturationThreshold_10(IntPtr nativeObj);
  169. // C++: void cv::xphoto::LearningBasedWB::setSaturationThreshold(float val)
  170. [DllImport(LIBNAME)]
  171. private static extern void xphoto_LearningBasedWB_setSaturationThreshold_10(IntPtr nativeObj, float val);
  172. // C++: int cv::xphoto::LearningBasedWB::getHistBinNum()
  173. [DllImport(LIBNAME)]
  174. private static extern int xphoto_LearningBasedWB_getHistBinNum_10(IntPtr nativeObj);
  175. // C++: void cv::xphoto::LearningBasedWB::setHistBinNum(int val)
  176. [DllImport(LIBNAME)]
  177. private static extern void xphoto_LearningBasedWB_setHistBinNum_10(IntPtr nativeObj, int val);
  178. // native support for java finalize()
  179. [DllImport(LIBNAME)]
  180. private static extern void xphoto_LearningBasedWB_delete(IntPtr nativeObj);
  181. }
  182. }