BOWTrainer.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.Features2dModule
  7. {
  8. // C++: class BOWTrainer
  9. /**
  10. * Abstract base class for training the *bag of visual words* vocabulary from a set of descriptors.
  11. *
  12. * For details, see, for example, *Visual Categorization with Bags of Keypoints* by Gabriella Csurka,
  13. * Christopher R. Dance, Lixin Fan, Jutta Willamowski, Cedric Bray, 2004. :
  14. */
  15. public class BOWTrainer : DisposableOpenCVObject
  16. {
  17. protected override void Dispose(bool disposing)
  18. {
  19. try
  20. {
  21. if (disposing)
  22. {
  23. }
  24. if (IsEnabledDispose)
  25. {
  26. if (nativeObj != IntPtr.Zero)
  27. features2d_BOWTrainer_delete(nativeObj);
  28. nativeObj = IntPtr.Zero;
  29. }
  30. }
  31. finally
  32. {
  33. base.Dispose(disposing);
  34. }
  35. }
  36. protected internal BOWTrainer(IntPtr addr) : base(addr) { }
  37. public IntPtr getNativeObjAddr() { return nativeObj; }
  38. // internal usage only
  39. public static BOWTrainer __fromPtr__(IntPtr addr) { return new BOWTrainer(addr); }
  40. //
  41. // C++: void cv::BOWTrainer::add(Mat descriptors)
  42. //
  43. /**
  44. * Adds descriptors to a training set.
  45. *
  46. * param descriptors Descriptors to add to a training set. Each row of the descriptors matrix is a
  47. * descriptor.
  48. *
  49. * The training set is clustered using clustermethod to construct the vocabulary.
  50. */
  51. public void add(Mat descriptors)
  52. {
  53. ThrowIfDisposed();
  54. if (descriptors != null) descriptors.ThrowIfDisposed();
  55. features2d_BOWTrainer_add_10(nativeObj, descriptors.nativeObj);
  56. }
  57. //
  58. // C++: vector_Mat cv::BOWTrainer::getDescriptors()
  59. //
  60. /**
  61. * Returns a training set of descriptors.
  62. * return automatically generated
  63. */
  64. public List<Mat> getDescriptors()
  65. {
  66. ThrowIfDisposed();
  67. List<Mat> retVal = new List<Mat>();
  68. Mat retValMat = new Mat(DisposableObject.ThrowIfNullIntPtr(features2d_BOWTrainer_getDescriptors_10(nativeObj)));
  69. Converters.Mat_to_vector_Mat(retValMat, retVal);
  70. return retVal;
  71. }
  72. //
  73. // C++: int cv::BOWTrainer::descriptorsCount()
  74. //
  75. /**
  76. * Returns the count of all descriptors stored in the training set.
  77. * return automatically generated
  78. */
  79. public int descriptorsCount()
  80. {
  81. ThrowIfDisposed();
  82. return features2d_BOWTrainer_descriptorsCount_10(nativeObj);
  83. }
  84. //
  85. // C++: void cv::BOWTrainer::clear()
  86. //
  87. public void clear()
  88. {
  89. ThrowIfDisposed();
  90. features2d_BOWTrainer_clear_10(nativeObj);
  91. }
  92. //
  93. // C++: Mat cv::BOWTrainer::cluster()
  94. //
  95. public virtual Mat cluster()
  96. {
  97. ThrowIfDisposed();
  98. return new Mat(DisposableObject.ThrowIfNullIntPtr(features2d_BOWTrainer_cluster_10(nativeObj)));
  99. }
  100. //
  101. // C++: Mat cv::BOWTrainer::cluster(Mat descriptors)
  102. //
  103. /**
  104. * Clusters train descriptors.
  105. *
  106. * param descriptors Descriptors to cluster. Each row of the descriptors matrix is a descriptor.
  107. * Descriptors are not added to the inner train descriptor set.
  108. *
  109. * The vocabulary consists of cluster centers. So, this method returns the vocabulary. In the first
  110. * variant of the method, train descriptors stored in the object are clustered. In the second variant,
  111. * input descriptors are clustered.
  112. * return automatically generated
  113. */
  114. public virtual Mat cluster(Mat descriptors)
  115. {
  116. ThrowIfDisposed();
  117. if (descriptors != null) descriptors.ThrowIfDisposed();
  118. return new Mat(DisposableObject.ThrowIfNullIntPtr(features2d_BOWTrainer_cluster_11(nativeObj, descriptors.nativeObj)));
  119. }
  120. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  121. const string LIBNAME = "__Internal";
  122. #else
  123. const string LIBNAME = "opencvforunity";
  124. #endif
  125. // C++: void cv::BOWTrainer::add(Mat descriptors)
  126. [DllImport(LIBNAME)]
  127. private static extern void features2d_BOWTrainer_add_10(IntPtr nativeObj, IntPtr descriptors_nativeObj);
  128. // C++: vector_Mat cv::BOWTrainer::getDescriptors()
  129. [DllImport(LIBNAME)]
  130. private static extern IntPtr features2d_BOWTrainer_getDescriptors_10(IntPtr nativeObj);
  131. // C++: int cv::BOWTrainer::descriptorsCount()
  132. [DllImport(LIBNAME)]
  133. private static extern int features2d_BOWTrainer_descriptorsCount_10(IntPtr nativeObj);
  134. // C++: void cv::BOWTrainer::clear()
  135. [DllImport(LIBNAME)]
  136. private static extern void features2d_BOWTrainer_clear_10(IntPtr nativeObj);
  137. // C++: Mat cv::BOWTrainer::cluster()
  138. [DllImport(LIBNAME)]
  139. private static extern IntPtr features2d_BOWTrainer_cluster_10(IntPtr nativeObj);
  140. // C++: Mat cv::BOWTrainer::cluster(Mat descriptors)
  141. [DllImport(LIBNAME)]
  142. private static extern IntPtr features2d_BOWTrainer_cluster_11(IntPtr nativeObj, IntPtr descriptors_nativeObj);
  143. // native support for java finalize()
  144. [DllImport(LIBNAME)]
  145. private static extern void features2d_BOWTrainer_delete(IntPtr nativeObj);
  146. }
  147. }