RTrees.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.MlModule
  7. {
  8. // C++: class RTrees
  9. /**
  10. * The class implements the random forest predictor.
  11. *
  12. * SEE: REF: ml_intro_rtrees
  13. */
  14. public class RTrees : DTrees
  15. {
  16. protected override void Dispose(bool disposing)
  17. {
  18. try
  19. {
  20. if (disposing)
  21. {
  22. }
  23. if (IsEnabledDispose)
  24. {
  25. if (nativeObj != IntPtr.Zero)
  26. ml_RTrees_delete(nativeObj);
  27. nativeObj = IntPtr.Zero;
  28. }
  29. }
  30. finally
  31. {
  32. base.Dispose(disposing);
  33. }
  34. }
  35. protected internal RTrees(IntPtr addr) : base(addr) { }
  36. // internal usage only
  37. public static new RTrees __fromPtr__(IntPtr addr) { return new RTrees(addr); }
  38. //
  39. // C++: bool cv::ml::RTrees::getCalculateVarImportance()
  40. //
  41. /**
  42. * SEE: setCalculateVarImportance
  43. * return automatically generated
  44. */
  45. public bool getCalculateVarImportance()
  46. {
  47. ThrowIfDisposed();
  48. return ml_RTrees_getCalculateVarImportance_10(nativeObj);
  49. }
  50. //
  51. // C++: void cv::ml::RTrees::setCalculateVarImportance(bool val)
  52. //
  53. /**
  54. * getCalculateVarImportance SEE: getCalculateVarImportance
  55. * param val automatically generated
  56. */
  57. public void setCalculateVarImportance(bool val)
  58. {
  59. ThrowIfDisposed();
  60. ml_RTrees_setCalculateVarImportance_10(nativeObj, val);
  61. }
  62. //
  63. // C++: int cv::ml::RTrees::getActiveVarCount()
  64. //
  65. /**
  66. * SEE: setActiveVarCount
  67. * return automatically generated
  68. */
  69. public int getActiveVarCount()
  70. {
  71. ThrowIfDisposed();
  72. return ml_RTrees_getActiveVarCount_10(nativeObj);
  73. }
  74. //
  75. // C++: void cv::ml::RTrees::setActiveVarCount(int val)
  76. //
  77. /**
  78. * getActiveVarCount SEE: getActiveVarCount
  79. * param val automatically generated
  80. */
  81. public void setActiveVarCount(int val)
  82. {
  83. ThrowIfDisposed();
  84. ml_RTrees_setActiveVarCount_10(nativeObj, val);
  85. }
  86. //
  87. // C++: TermCriteria cv::ml::RTrees::getTermCriteria()
  88. //
  89. /**
  90. * SEE: setTermCriteria
  91. * return automatically generated
  92. */
  93. public TermCriteria getTermCriteria()
  94. {
  95. ThrowIfDisposed();
  96. double[] tmpArray = new double[3];
  97. ml_RTrees_getTermCriteria_10(nativeObj, tmpArray);
  98. TermCriteria retVal = new TermCriteria(tmpArray);
  99. return retVal;
  100. }
  101. //
  102. // C++: void cv::ml::RTrees::setTermCriteria(TermCriteria val)
  103. //
  104. /**
  105. * getTermCriteria SEE: getTermCriteria
  106. * param val automatically generated
  107. */
  108. public void setTermCriteria(TermCriteria val)
  109. {
  110. ThrowIfDisposed();
  111. ml_RTrees_setTermCriteria_10(nativeObj, val.type, val.maxCount, val.epsilon);
  112. }
  113. //
  114. // C++: Mat cv::ml::RTrees::getVarImportance()
  115. //
  116. /**
  117. * Returns the variable importance array.
  118. * The method returns the variable importance vector, computed at the training stage when
  119. * CalculateVarImportance is set to true. If this flag was set to false, the empty matrix is
  120. * returned.
  121. * return automatically generated
  122. */
  123. public Mat getVarImportance()
  124. {
  125. ThrowIfDisposed();
  126. return new Mat(DisposableObject.ThrowIfNullIntPtr(ml_RTrees_getVarImportance_10(nativeObj)));
  127. }
  128. //
  129. // C++: void cv::ml::RTrees::getVotes(Mat samples, Mat& results, int flags)
  130. //
  131. /**
  132. * Returns the result of each individual tree in the forest.
  133. * In case the model is a regression problem, the method will return each of the trees'
  134. * results for each of the sample cases. If the model is a classifier, it will return
  135. * a Mat with samples + 1 rows, where the first row gives the class number and the
  136. * following rows return the votes each class had for each sample.
  137. * param samples Array containing the samples for which votes will be calculated.
  138. * param results Array where the result of the calculation will be written.
  139. * param flags Flags for defining the type of RTrees.
  140. */
  141. public void getVotes(Mat samples, Mat results, int flags)
  142. {
  143. ThrowIfDisposed();
  144. if (samples != null) samples.ThrowIfDisposed();
  145. if (results != null) results.ThrowIfDisposed();
  146. ml_RTrees_getVotes_10(nativeObj, samples.nativeObj, results.nativeObj, flags);
  147. }
  148. //
  149. // C++: double cv::ml::RTrees::getOOBError()
  150. //
  151. /**
  152. * Returns the OOB error value, computed at the training stage when calcOOBError is set to true.
  153. * If this flag was set to false, 0 is returned. The OOB error is also scaled by sample weighting.
  154. * return automatically generated
  155. */
  156. public double getOOBError()
  157. {
  158. ThrowIfDisposed();
  159. return ml_RTrees_getOOBError_10(nativeObj);
  160. }
  161. //
  162. // C++: static Ptr_RTrees cv::ml::RTrees::create()
  163. //
  164. /**
  165. * Creates the empty model.
  166. * Use StatModel::train to train the model, StatModel::train to create and train the model,
  167. * Algorithm::load to load the pre-trained model.
  168. * return automatically generated
  169. */
  170. public static new RTrees create()
  171. {
  172. return RTrees.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_RTrees_create_10()));
  173. }
  174. //
  175. // C++: static Ptr_RTrees cv::ml::RTrees::load(String filepath, String nodeName = String())
  176. //
  177. /**
  178. * Loads and creates a serialized RTree from a file
  179. *
  180. * Use RTree::save to serialize and store an RTree to disk.
  181. * Load the RTree from this file again, by calling this function with the path to the file.
  182. * Optionally specify the node for the file containing the classifier
  183. *
  184. * param filepath path to serialized RTree
  185. * param nodeName name of node containing the classifier
  186. * return automatically generated
  187. */
  188. public static new RTrees load(string filepath, string nodeName)
  189. {
  190. return RTrees.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_RTrees_load_10(filepath, nodeName)));
  191. }
  192. /**
  193. * Loads and creates a serialized RTree from a file
  194. *
  195. * Use RTree::save to serialize and store an RTree to disk.
  196. * Load the RTree from this file again, by calling this function with the path to the file.
  197. * Optionally specify the node for the file containing the classifier
  198. *
  199. * param filepath path to serialized RTree
  200. * return automatically generated
  201. */
  202. public static new RTrees load(string filepath)
  203. {
  204. return RTrees.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_RTrees_load_11(filepath)));
  205. }
  206. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  207. const string LIBNAME = "__Internal";
  208. #else
  209. const string LIBNAME = "opencvforunity";
  210. #endif
  211. // C++: bool cv::ml::RTrees::getCalculateVarImportance()
  212. [DllImport(LIBNAME)]
  213. [return: MarshalAs(UnmanagedType.U1)]
  214. private static extern bool ml_RTrees_getCalculateVarImportance_10(IntPtr nativeObj);
  215. // C++: void cv::ml::RTrees::setCalculateVarImportance(bool val)
  216. [DllImport(LIBNAME)]
  217. private static extern void ml_RTrees_setCalculateVarImportance_10(IntPtr nativeObj, [MarshalAs(UnmanagedType.U1)] bool val);
  218. // C++: int cv::ml::RTrees::getActiveVarCount()
  219. [DllImport(LIBNAME)]
  220. private static extern int ml_RTrees_getActiveVarCount_10(IntPtr nativeObj);
  221. // C++: void cv::ml::RTrees::setActiveVarCount(int val)
  222. [DllImport(LIBNAME)]
  223. private static extern void ml_RTrees_setActiveVarCount_10(IntPtr nativeObj, int val);
  224. // C++: TermCriteria cv::ml::RTrees::getTermCriteria()
  225. [DllImport(LIBNAME)]
  226. private static extern void ml_RTrees_getTermCriteria_10(IntPtr nativeObj, double[] retVal);
  227. // C++: void cv::ml::RTrees::setTermCriteria(TermCriteria val)
  228. [DllImport(LIBNAME)]
  229. private static extern void ml_RTrees_setTermCriteria_10(IntPtr nativeObj, int val_type, int val_maxCount, double val_epsilon);
  230. // C++: Mat cv::ml::RTrees::getVarImportance()
  231. [DllImport(LIBNAME)]
  232. private static extern IntPtr ml_RTrees_getVarImportance_10(IntPtr nativeObj);
  233. // C++: void cv::ml::RTrees::getVotes(Mat samples, Mat& results, int flags)
  234. [DllImport(LIBNAME)]
  235. private static extern void ml_RTrees_getVotes_10(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj, int flags);
  236. // C++: double cv::ml::RTrees::getOOBError()
  237. [DllImport(LIBNAME)]
  238. private static extern double ml_RTrees_getOOBError_10(IntPtr nativeObj);
  239. // C++: static Ptr_RTrees cv::ml::RTrees::create()
  240. [DllImport(LIBNAME)]
  241. private static extern IntPtr ml_RTrees_create_10();
  242. // C++: static Ptr_RTrees cv::ml::RTrees::load(String filepath, String nodeName = String())
  243. [DllImport(LIBNAME)]
  244. private static extern IntPtr ml_RTrees_load_10(string filepath, string nodeName);
  245. [DllImport(LIBNAME)]
  246. private static extern IntPtr ml_RTrees_load_11(string filepath);
  247. // native support for java finalize()
  248. [DllImport(LIBNAME)]
  249. private static extern void ml_RTrees_delete(IntPtr nativeObj);
  250. }
  251. }