KNearest.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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 KNearest
  9. /**
  10. * The class implements K-Nearest Neighbors model
  11. *
  12. * SEE: REF: ml_intro_knn
  13. */
  14. public class KNearest : StatModel
  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_KNearest_delete(nativeObj);
  27. nativeObj = IntPtr.Zero;
  28. }
  29. }
  30. finally
  31. {
  32. base.Dispose(disposing);
  33. }
  34. }
  35. protected internal KNearest(IntPtr addr) : base(addr) { }
  36. // internal usage only
  37. public static new KNearest __fromPtr__(IntPtr addr) { return new KNearest(addr); }
  38. // C++: enum cv.ml.KNearest.Types
  39. public const int BRUTE_FORCE = 1;
  40. public const int KDTREE = 2;
  41. //
  42. // C++: int cv::ml::KNearest::getDefaultK()
  43. //
  44. /**
  45. * SEE: setDefaultK
  46. * return automatically generated
  47. */
  48. public int getDefaultK()
  49. {
  50. ThrowIfDisposed();
  51. return ml_KNearest_getDefaultK_10(nativeObj);
  52. }
  53. //
  54. // C++: void cv::ml::KNearest::setDefaultK(int val)
  55. //
  56. /**
  57. * getDefaultK SEE: getDefaultK
  58. * param val automatically generated
  59. */
  60. public void setDefaultK(int val)
  61. {
  62. ThrowIfDisposed();
  63. ml_KNearest_setDefaultK_10(nativeObj, val);
  64. }
  65. //
  66. // C++: bool cv::ml::KNearest::getIsClassifier()
  67. //
  68. /**
  69. * SEE: setIsClassifier
  70. * return automatically generated
  71. */
  72. public bool getIsClassifier()
  73. {
  74. ThrowIfDisposed();
  75. return ml_KNearest_getIsClassifier_10(nativeObj);
  76. }
  77. //
  78. // C++: void cv::ml::KNearest::setIsClassifier(bool val)
  79. //
  80. /**
  81. * getIsClassifier SEE: getIsClassifier
  82. * param val automatically generated
  83. */
  84. public void setIsClassifier(bool val)
  85. {
  86. ThrowIfDisposed();
  87. ml_KNearest_setIsClassifier_10(nativeObj, val);
  88. }
  89. //
  90. // C++: int cv::ml::KNearest::getEmax()
  91. //
  92. /**
  93. * SEE: setEmax
  94. * return automatically generated
  95. */
  96. public int getEmax()
  97. {
  98. ThrowIfDisposed();
  99. return ml_KNearest_getEmax_10(nativeObj);
  100. }
  101. //
  102. // C++: void cv::ml::KNearest::setEmax(int val)
  103. //
  104. /**
  105. * getEmax SEE: getEmax
  106. * param val automatically generated
  107. */
  108. public void setEmax(int val)
  109. {
  110. ThrowIfDisposed();
  111. ml_KNearest_setEmax_10(nativeObj, val);
  112. }
  113. //
  114. // C++: int cv::ml::KNearest::getAlgorithmType()
  115. //
  116. /**
  117. * SEE: setAlgorithmType
  118. * return automatically generated
  119. */
  120. public int getAlgorithmType()
  121. {
  122. ThrowIfDisposed();
  123. return ml_KNearest_getAlgorithmType_10(nativeObj);
  124. }
  125. //
  126. // C++: void cv::ml::KNearest::setAlgorithmType(int val)
  127. //
  128. /**
  129. * getAlgorithmType SEE: getAlgorithmType
  130. * param val automatically generated
  131. */
  132. public void setAlgorithmType(int val)
  133. {
  134. ThrowIfDisposed();
  135. ml_KNearest_setAlgorithmType_10(nativeObj, val);
  136. }
  137. //
  138. // C++: float cv::ml::KNearest::findNearest(Mat samples, int k, Mat& results, Mat& neighborResponses = Mat(), Mat& dist = Mat())
  139. //
  140. /**
  141. * Finds the neighbors and predicts responses for input vectors.
  142. *
  143. * param samples Input samples stored by rows. It is a single-precision floating-point matrix of
  144. * {code <number_of_samples> * k} size.
  145. * param k Number of used nearest neighbors. Should be greater than 1.
  146. * param results Vector with results of prediction (regression or classification) for each input
  147. * sample. It is a single-precision floating-point vector with {code <number_of_samples>} elements.
  148. * param neighborResponses Optional output values for corresponding neighbors. It is a single-
  149. * precision floating-point matrix of {code <number_of_samples> * k} size.
  150. * param dist Optional output distances from the input vectors to the corresponding neighbors. It
  151. * is a single-precision floating-point matrix of {code <number_of_samples> * k} size.
  152. *
  153. * For each input vector (a row of the matrix samples), the method finds the k nearest neighbors.
  154. * In case of regression, the predicted result is a mean value of the particular vector's neighbor
  155. * responses. In case of classification, the class is determined by voting.
  156. *
  157. * For each input vector, the neighbors are sorted by their distances to the vector.
  158. *
  159. * In case of C++ interface you can use output pointers to empty matrices and the function will
  160. * allocate memory itself.
  161. *
  162. * If only a single input vector is passed, all output matrices are optional and the predicted
  163. * value is returned by the method.
  164. *
  165. * The function is parallelized with the TBB library.
  166. * return automatically generated
  167. */
  168. public float findNearest(Mat samples, int k, Mat results, Mat neighborResponses, Mat dist)
  169. {
  170. ThrowIfDisposed();
  171. if (samples != null) samples.ThrowIfDisposed();
  172. if (results != null) results.ThrowIfDisposed();
  173. if (neighborResponses != null) neighborResponses.ThrowIfDisposed();
  174. if (dist != null) dist.ThrowIfDisposed();
  175. return ml_KNearest_findNearest_10(nativeObj, samples.nativeObj, k, results.nativeObj, neighborResponses.nativeObj, dist.nativeObj);
  176. }
  177. /**
  178. * Finds the neighbors and predicts responses for input vectors.
  179. *
  180. * param samples Input samples stored by rows. It is a single-precision floating-point matrix of
  181. * {code <number_of_samples> * k} size.
  182. * param k Number of used nearest neighbors. Should be greater than 1.
  183. * param results Vector with results of prediction (regression or classification) for each input
  184. * sample. It is a single-precision floating-point vector with {code <number_of_samples>} elements.
  185. * param neighborResponses Optional output values for corresponding neighbors. It is a single-
  186. * precision floating-point matrix of {code <number_of_samples> * k} size.
  187. * is a single-precision floating-point matrix of {code <number_of_samples> * k} size.
  188. *
  189. * For each input vector (a row of the matrix samples), the method finds the k nearest neighbors.
  190. * In case of regression, the predicted result is a mean value of the particular vector's neighbor
  191. * responses. In case of classification, the class is determined by voting.
  192. *
  193. * For each input vector, the neighbors are sorted by their distances to the vector.
  194. *
  195. * In case of C++ interface you can use output pointers to empty matrices and the function will
  196. * allocate memory itself.
  197. *
  198. * If only a single input vector is passed, all output matrices are optional and the predicted
  199. * value is returned by the method.
  200. *
  201. * The function is parallelized with the TBB library.
  202. * return automatically generated
  203. */
  204. public float findNearest(Mat samples, int k, Mat results, Mat neighborResponses)
  205. {
  206. ThrowIfDisposed();
  207. if (samples != null) samples.ThrowIfDisposed();
  208. if (results != null) results.ThrowIfDisposed();
  209. if (neighborResponses != null) neighborResponses.ThrowIfDisposed();
  210. return ml_KNearest_findNearest_11(nativeObj, samples.nativeObj, k, results.nativeObj, neighborResponses.nativeObj);
  211. }
  212. /**
  213. * Finds the neighbors and predicts responses for input vectors.
  214. *
  215. * param samples Input samples stored by rows. It is a single-precision floating-point matrix of
  216. * {code <number_of_samples> * k} size.
  217. * param k Number of used nearest neighbors. Should be greater than 1.
  218. * param results Vector with results of prediction (regression or classification) for each input
  219. * sample. It is a single-precision floating-point vector with {code <number_of_samples>} elements.
  220. * precision floating-point matrix of {code <number_of_samples> * k} size.
  221. * is a single-precision floating-point matrix of {code <number_of_samples> * k} size.
  222. *
  223. * For each input vector (a row of the matrix samples), the method finds the k nearest neighbors.
  224. * In case of regression, the predicted result is a mean value of the particular vector's neighbor
  225. * responses. In case of classification, the class is determined by voting.
  226. *
  227. * For each input vector, the neighbors are sorted by their distances to the vector.
  228. *
  229. * In case of C++ interface you can use output pointers to empty matrices and the function will
  230. * allocate memory itself.
  231. *
  232. * If only a single input vector is passed, all output matrices are optional and the predicted
  233. * value is returned by the method.
  234. *
  235. * The function is parallelized with the TBB library.
  236. * return automatically generated
  237. */
  238. public float findNearest(Mat samples, int k, Mat results)
  239. {
  240. ThrowIfDisposed();
  241. if (samples != null) samples.ThrowIfDisposed();
  242. if (results != null) results.ThrowIfDisposed();
  243. return ml_KNearest_findNearest_12(nativeObj, samples.nativeObj, k, results.nativeObj);
  244. }
  245. //
  246. // C++: static Ptr_KNearest cv::ml::KNearest::create()
  247. //
  248. /**
  249. * Creates the empty model
  250. *
  251. * The static method creates empty %KNearest classifier. It should be then trained using StatModel::train method.
  252. * return automatically generated
  253. */
  254. public static KNearest create()
  255. {
  256. return KNearest.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_KNearest_create_10()));
  257. }
  258. //
  259. // C++: static Ptr_KNearest cv::ml::KNearest::load(String filepath)
  260. //
  261. /**
  262. * Loads and creates a serialized knearest from a file
  263. *
  264. * Use KNearest::save to serialize and store an KNearest to disk.
  265. * Load the KNearest from this file again, by calling this function with the path to the file.
  266. *
  267. * param filepath path to serialized KNearest
  268. * return automatically generated
  269. */
  270. public static KNearest load(string filepath)
  271. {
  272. return KNearest.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(ml_KNearest_load_10(filepath)));
  273. }
  274. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  275. const string LIBNAME = "__Internal";
  276. #else
  277. const string LIBNAME = "opencvforunity";
  278. #endif
  279. // C++: int cv::ml::KNearest::getDefaultK()
  280. [DllImport(LIBNAME)]
  281. private static extern int ml_KNearest_getDefaultK_10(IntPtr nativeObj);
  282. // C++: void cv::ml::KNearest::setDefaultK(int val)
  283. [DllImport(LIBNAME)]
  284. private static extern void ml_KNearest_setDefaultK_10(IntPtr nativeObj, int val);
  285. // C++: bool cv::ml::KNearest::getIsClassifier()
  286. [DllImport(LIBNAME)]
  287. [return: MarshalAs(UnmanagedType.U1)]
  288. private static extern bool ml_KNearest_getIsClassifier_10(IntPtr nativeObj);
  289. // C++: void cv::ml::KNearest::setIsClassifier(bool val)
  290. [DllImport(LIBNAME)]
  291. private static extern void ml_KNearest_setIsClassifier_10(IntPtr nativeObj, [MarshalAs(UnmanagedType.U1)] bool val);
  292. // C++: int cv::ml::KNearest::getEmax()
  293. [DllImport(LIBNAME)]
  294. private static extern int ml_KNearest_getEmax_10(IntPtr nativeObj);
  295. // C++: void cv::ml::KNearest::setEmax(int val)
  296. [DllImport(LIBNAME)]
  297. private static extern void ml_KNearest_setEmax_10(IntPtr nativeObj, int val);
  298. // C++: int cv::ml::KNearest::getAlgorithmType()
  299. [DllImport(LIBNAME)]
  300. private static extern int ml_KNearest_getAlgorithmType_10(IntPtr nativeObj);
  301. // C++: void cv::ml::KNearest::setAlgorithmType(int val)
  302. [DllImport(LIBNAME)]
  303. private static extern void ml_KNearest_setAlgorithmType_10(IntPtr nativeObj, int val);
  304. // C++: float cv::ml::KNearest::findNearest(Mat samples, int k, Mat& results, Mat& neighborResponses = Mat(), Mat& dist = Mat())
  305. [DllImport(LIBNAME)]
  306. private static extern float ml_KNearest_findNearest_10(IntPtr nativeObj, IntPtr samples_nativeObj, int k, IntPtr results_nativeObj, IntPtr neighborResponses_nativeObj, IntPtr dist_nativeObj);
  307. [DllImport(LIBNAME)]
  308. private static extern float ml_KNearest_findNearest_11(IntPtr nativeObj, IntPtr samples_nativeObj, int k, IntPtr results_nativeObj, IntPtr neighborResponses_nativeObj);
  309. [DllImport(LIBNAME)]
  310. private static extern float ml_KNearest_findNearest_12(IntPtr nativeObj, IntPtr samples_nativeObj, int k, IntPtr results_nativeObj);
  311. // C++: static Ptr_KNearest cv::ml::KNearest::create()
  312. [DllImport(LIBNAME)]
  313. private static extern IntPtr ml_KNearest_create_10();
  314. // C++: static Ptr_KNearest cv::ml::KNearest::load(String filepath)
  315. [DllImport(LIBNAME)]
  316. private static extern IntPtr ml_KNearest_load_10(string filepath);
  317. // native support for java finalize()
  318. [DllImport(LIBNAME)]
  319. private static extern void ml_KNearest_delete(IntPtr nativeObj);
  320. }
  321. }