StatModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 StatModel
  9. /**
  10. * Base class for statistical models in OpenCV ML.
  11. */
  12. public class StatModel : Algorithm
  13. {
  14. protected override void Dispose(bool disposing)
  15. {
  16. try
  17. {
  18. if (disposing)
  19. {
  20. }
  21. if (IsEnabledDispose)
  22. {
  23. if (nativeObj != IntPtr.Zero)
  24. ml_StatModel_delete(nativeObj);
  25. nativeObj = IntPtr.Zero;
  26. }
  27. }
  28. finally
  29. {
  30. base.Dispose(disposing);
  31. }
  32. }
  33. protected internal StatModel(IntPtr addr) : base(addr) { }
  34. // internal usage only
  35. public static new StatModel __fromPtr__(IntPtr addr) { return new StatModel(addr); }
  36. // C++: enum cv.ml.StatModel.Flags
  37. public const int UPDATE_MODEL = 1;
  38. public const int RAW_OUTPUT = 1;
  39. public const int COMPRESSED_INPUT = 2;
  40. public const int PREPROCESSED_INPUT = 4;
  41. //
  42. // C++: int cv::ml::StatModel::getVarCount()
  43. //
  44. /**
  45. * Returns the number of variables in training samples
  46. * return automatically generated
  47. */
  48. public int getVarCount()
  49. {
  50. ThrowIfDisposed();
  51. return ml_StatModel_getVarCount_10(nativeObj);
  52. }
  53. //
  54. // C++: bool cv::ml::StatModel::empty()
  55. //
  56. public override bool empty()
  57. {
  58. ThrowIfDisposed();
  59. return ml_StatModel_empty_10(nativeObj);
  60. }
  61. //
  62. // C++: bool cv::ml::StatModel::isTrained()
  63. //
  64. /**
  65. * Returns true if the model is trained
  66. * return automatically generated
  67. */
  68. public bool isTrained()
  69. {
  70. ThrowIfDisposed();
  71. return ml_StatModel_isTrained_10(nativeObj);
  72. }
  73. //
  74. // C++: bool cv::ml::StatModel::isClassifier()
  75. //
  76. /**
  77. * Returns true if the model is classifier
  78. * return automatically generated
  79. */
  80. public bool isClassifier()
  81. {
  82. ThrowIfDisposed();
  83. return ml_StatModel_isClassifier_10(nativeObj);
  84. }
  85. //
  86. // C++: bool cv::ml::StatModel::train(Ptr_TrainData trainData, int flags = 0)
  87. //
  88. /**
  89. * Trains the statistical model
  90. *
  91. * param trainData training data that can be loaded from file using TrainData::loadFromCSV or
  92. * created with TrainData::create.
  93. * param flags optional flags, depending on the model. Some of the models can be updated with the
  94. * new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).
  95. * return automatically generated
  96. */
  97. public bool train(TrainData trainData, int flags)
  98. {
  99. ThrowIfDisposed();
  100. if (trainData != null) trainData.ThrowIfDisposed();
  101. return ml_StatModel_train_10(nativeObj, trainData.getNativeObjAddr(), flags);
  102. }
  103. /**
  104. * Trains the statistical model
  105. *
  106. * param trainData training data that can be loaded from file using TrainData::loadFromCSV or
  107. * created with TrainData::create.
  108. * new training samples, not completely overwritten (such as NormalBayesClassifier or ANN_MLP).
  109. * return automatically generated
  110. */
  111. public bool train(TrainData trainData)
  112. {
  113. ThrowIfDisposed();
  114. if (trainData != null) trainData.ThrowIfDisposed();
  115. return ml_StatModel_train_11(nativeObj, trainData.getNativeObjAddr());
  116. }
  117. //
  118. // C++: bool cv::ml::StatModel::train(Mat samples, int layout, Mat responses)
  119. //
  120. /**
  121. * Trains the statistical model
  122. *
  123. * param samples training samples
  124. * param layout See ml::SampleTypes.
  125. * param responses vector of responses associated with the training samples.
  126. * return automatically generated
  127. */
  128. public bool train(Mat samples, int layout, Mat responses)
  129. {
  130. ThrowIfDisposed();
  131. if (samples != null) samples.ThrowIfDisposed();
  132. if (responses != null) responses.ThrowIfDisposed();
  133. return ml_StatModel_train_12(nativeObj, samples.nativeObj, layout, responses.nativeObj);
  134. }
  135. //
  136. // C++: float cv::ml::StatModel::calcError(Ptr_TrainData data, bool test, Mat& resp)
  137. //
  138. /**
  139. * Computes error on the training or test dataset
  140. *
  141. * param data the training data
  142. * param test if true, the error is computed over the test subset of the data, otherwise it's
  143. * computed over the training subset of the data. Please note that if you loaded a completely
  144. * different dataset to evaluate already trained classifier, you will probably want not to set
  145. * the test subset at all with TrainData::setTrainTestSplitRatio and specify test=false, so
  146. * that the error is computed for the whole new set. Yes, this sounds a bit confusing.
  147. * param resp the optional output responses.
  148. *
  149. * The method uses StatModel::predict to compute the error. For regression models the error is
  150. * computed as RMS, for classifiers - as a percent of missclassified samples (0%-100%).
  151. * return automatically generated
  152. */
  153. public float calcError(TrainData data, bool test, Mat resp)
  154. {
  155. ThrowIfDisposed();
  156. if (data != null) data.ThrowIfDisposed();
  157. if (resp != null) resp.ThrowIfDisposed();
  158. return ml_StatModel_calcError_10(nativeObj, data.getNativeObjAddr(), test, resp.nativeObj);
  159. }
  160. //
  161. // C++: float cv::ml::StatModel::predict(Mat samples, Mat& results = Mat(), int flags = 0)
  162. //
  163. /**
  164. * Predicts response(s) for the provided sample(s)
  165. *
  166. * param samples The input samples, floating-point matrix
  167. * param results The optional output matrix of results.
  168. * param flags The optional flags, model-dependent. See cv::ml::StatModel::Flags.
  169. * return automatically generated
  170. */
  171. public virtual float predict(Mat samples, Mat results, int flags)
  172. {
  173. ThrowIfDisposed();
  174. if (samples != null) samples.ThrowIfDisposed();
  175. if (results != null) results.ThrowIfDisposed();
  176. return ml_StatModel_predict_10(nativeObj, samples.nativeObj, results.nativeObj, flags);
  177. }
  178. /**
  179. * Predicts response(s) for the provided sample(s)
  180. *
  181. * param samples The input samples, floating-point matrix
  182. * param results The optional output matrix of results.
  183. * return automatically generated
  184. */
  185. public virtual float predict(Mat samples, Mat results)
  186. {
  187. ThrowIfDisposed();
  188. if (samples != null) samples.ThrowIfDisposed();
  189. if (results != null) results.ThrowIfDisposed();
  190. return ml_StatModel_predict_11(nativeObj, samples.nativeObj, results.nativeObj);
  191. }
  192. /**
  193. * Predicts response(s) for the provided sample(s)
  194. *
  195. * param samples The input samples, floating-point matrix
  196. * return automatically generated
  197. */
  198. public virtual float predict(Mat samples)
  199. {
  200. ThrowIfDisposed();
  201. if (samples != null) samples.ThrowIfDisposed();
  202. return ml_StatModel_predict_12(nativeObj, samples.nativeObj);
  203. }
  204. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  205. const string LIBNAME = "__Internal";
  206. #else
  207. const string LIBNAME = "opencvforunity";
  208. #endif
  209. // C++: int cv::ml::StatModel::getVarCount()
  210. [DllImport(LIBNAME)]
  211. private static extern int ml_StatModel_getVarCount_10(IntPtr nativeObj);
  212. // C++: bool cv::ml::StatModel::empty()
  213. [DllImport(LIBNAME)]
  214. [return: MarshalAs(UnmanagedType.U1)]
  215. private static extern bool ml_StatModel_empty_10(IntPtr nativeObj);
  216. // C++: bool cv::ml::StatModel::isTrained()
  217. [DllImport(LIBNAME)]
  218. [return: MarshalAs(UnmanagedType.U1)]
  219. private static extern bool ml_StatModel_isTrained_10(IntPtr nativeObj);
  220. // C++: bool cv::ml::StatModel::isClassifier()
  221. [DllImport(LIBNAME)]
  222. [return: MarshalAs(UnmanagedType.U1)]
  223. private static extern bool ml_StatModel_isClassifier_10(IntPtr nativeObj);
  224. // C++: bool cv::ml::StatModel::train(Ptr_TrainData trainData, int flags = 0)
  225. [DllImport(LIBNAME)]
  226. [return: MarshalAs(UnmanagedType.U1)]
  227. private static extern bool ml_StatModel_train_10(IntPtr nativeObj, IntPtr trainData_nativeObj, int flags);
  228. [DllImport(LIBNAME)]
  229. [return: MarshalAs(UnmanagedType.U1)]
  230. private static extern bool ml_StatModel_train_11(IntPtr nativeObj, IntPtr trainData_nativeObj);
  231. // C++: bool cv::ml::StatModel::train(Mat samples, int layout, Mat responses)
  232. [DllImport(LIBNAME)]
  233. [return: MarshalAs(UnmanagedType.U1)]
  234. private static extern bool ml_StatModel_train_12(IntPtr nativeObj, IntPtr samples_nativeObj, int layout, IntPtr responses_nativeObj);
  235. // C++: float cv::ml::StatModel::calcError(Ptr_TrainData data, bool test, Mat& resp)
  236. [DllImport(LIBNAME)]
  237. private static extern float ml_StatModel_calcError_10(IntPtr nativeObj, IntPtr data_nativeObj, [MarshalAs(UnmanagedType.U1)] bool test, IntPtr resp_nativeObj);
  238. // C++: float cv::ml::StatModel::predict(Mat samples, Mat& results = Mat(), int flags = 0)
  239. [DllImport(LIBNAME)]
  240. private static extern float ml_StatModel_predict_10(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj, int flags);
  241. [DllImport(LIBNAME)]
  242. private static extern float ml_StatModel_predict_11(IntPtr nativeObj, IntPtr samples_nativeObj, IntPtr results_nativeObj);
  243. [DllImport(LIBNAME)]
  244. private static extern float ml_StatModel_predict_12(IntPtr nativeObj, IntPtr samples_nativeObj);
  245. // native support for java finalize()
  246. [DllImport(LIBNAME)]
  247. private static extern void ml_StatModel_delete(IntPtr nativeObj);
  248. }
  249. }