TEBLID.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.Features2dModule;
  3. using OpenCVForUnity.UtilsModule;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace OpenCVForUnity.Xfeatures2dModule
  8. {
  9. // C++: class TEBLID
  10. /**
  11. * Class implementing TEBLID (Triplet-based Efficient Binary Local Image Descriptor),
  12. * described in CITE: Suarez2021TEBLID.
  13. *
  14. * TEBLID stands for Triplet-based Efficient Binary Local Image Descriptor, although originally it was called BAD
  15. * \cite Suarez2021TEBLID. It is an improvement over BEBLID \cite Suarez2020BEBLID, that uses triplet loss,
  16. * hard negative mining, and anchor swap to improve the image matching results.
  17. * It is able to describe keypoints from any detector just by changing the scale_factor parameter.
  18. * TEBLID is as efficient as ORB, BEBLID or BRISK, but the triplet-based training objective selected more
  19. * discriminative features that explain the accuracy gain. It is also more compact than BEBLID,
  20. * when running the [AKAZE example](https://github.com/opencv/opencv/blob/4.x/samples/cpp/tutorial_code/features2D/AKAZE_match.cpp)
  21. * with 10000 keypoints detected by ORB, BEBLID obtains 561 inliers (75%) with 512 bits, whereas
  22. * TEBLID obtains 621 (75.2%) with 256 bits. ORB obtains only 493 inliers (63%).
  23. *
  24. * If you find this code useful, please add a reference to the following paper:
  25. * <BLOCKQUOTE> Iago Suárez, José M. Buenaposada, and Luis Baumela.
  26. * Revisiting Binary Local Image Description for Resource Limited Devices.
  27. * IEEE Robotics and Automation Letters, vol. 6, no. 4, pp. 8317-8324, Oct. 2021. </BLOCKQUOTE>
  28. *
  29. * The descriptor was trained in Liberty split of the UBC datasets \cite winder2007learning .
  30. */
  31. public class TEBLID : Feature2D
  32. {
  33. protected override void Dispose(bool disposing)
  34. {
  35. try
  36. {
  37. if (disposing)
  38. {
  39. }
  40. if (IsEnabledDispose)
  41. {
  42. if (nativeObj != IntPtr.Zero)
  43. xfeatures2d_TEBLID_delete(nativeObj);
  44. nativeObj = IntPtr.Zero;
  45. }
  46. }
  47. finally
  48. {
  49. base.Dispose(disposing);
  50. }
  51. }
  52. protected internal TEBLID(IntPtr addr) : base(addr) { }
  53. // internal usage only
  54. public static new TEBLID __fromPtr__(IntPtr addr) { return new TEBLID(addr); }
  55. // C++: enum cv.xfeatures2d.TEBLID.TeblidSize
  56. public const int SIZE_256_BITS = 102;
  57. public const int SIZE_512_BITS = 103;
  58. //
  59. // C++: static Ptr_TEBLID cv::xfeatures2d::TEBLID::create(float scale_factor, int n_bits = TEBLID::SIZE_256_BITS)
  60. //
  61. /**
  62. * Creates the TEBLID descriptor.
  63. * param scale_factor Adjust the sampling window around detected keypoints:
  64. * <ul>
  65. * <li>
  66. * &lt;b&gt; 1.00f &lt;/b&gt; should be the scale for ORB keypoints
  67. * </li>
  68. * <li>
  69. * &lt;b&gt; 6.75f &lt;/b&gt; should be the scale for SIFT detected keypoints
  70. * </li>
  71. * <li>
  72. * &lt;b&gt; 6.25f &lt;/b&gt; is default and fits for KAZE, SURF detected keypoints
  73. * </li>
  74. * <li>
  75. * &lt;b&gt; 5.00f &lt;/b&gt; should be the scale for AKAZE, MSD, AGAST, FAST, BRISK keypoints
  76. * </li>
  77. * </ul>
  78. * param n_bits Determine the number of bits in the descriptor. Should be either
  79. * TEBLID::SIZE_256_BITS or TEBLID::SIZE_512_BITS.
  80. * return automatically generated
  81. */
  82. public static TEBLID create(float scale_factor, int n_bits)
  83. {
  84. return TEBLID.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(xfeatures2d_TEBLID_create_10(scale_factor, n_bits)));
  85. }
  86. /**
  87. * Creates the TEBLID descriptor.
  88. * param scale_factor Adjust the sampling window around detected keypoints:
  89. * <ul>
  90. * <li>
  91. * &lt;b&gt; 1.00f &lt;/b&gt; should be the scale for ORB keypoints
  92. * </li>
  93. * <li>
  94. * &lt;b&gt; 6.75f &lt;/b&gt; should be the scale for SIFT detected keypoints
  95. * </li>
  96. * <li>
  97. * &lt;b&gt; 6.25f &lt;/b&gt; is default and fits for KAZE, SURF detected keypoints
  98. * </li>
  99. * <li>
  100. * &lt;b&gt; 5.00f &lt;/b&gt; should be the scale for AKAZE, MSD, AGAST, FAST, BRISK keypoints
  101. * </li>
  102. * </ul>
  103. * TEBLID::SIZE_256_BITS or TEBLID::SIZE_512_BITS.
  104. * return automatically generated
  105. */
  106. public static TEBLID create(float scale_factor)
  107. {
  108. return TEBLID.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(xfeatures2d_TEBLID_create_11(scale_factor)));
  109. }
  110. //
  111. // C++: String cv::xfeatures2d::TEBLID::getDefaultName()
  112. //
  113. public override string getDefaultName()
  114. {
  115. ThrowIfDisposed();
  116. string retVal = Marshal.PtrToStringAnsi(DisposableObject.ThrowIfNullIntPtr(xfeatures2d_TEBLID_getDefaultName_10(nativeObj)));
  117. return retVal;
  118. }
  119. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  120. const string LIBNAME = "__Internal";
  121. #else
  122. const string LIBNAME = "opencvforunity";
  123. #endif
  124. // C++: static Ptr_TEBLID cv::xfeatures2d::TEBLID::create(float scale_factor, int n_bits = TEBLID::SIZE_256_BITS)
  125. [DllImport(LIBNAME)]
  126. private static extern IntPtr xfeatures2d_TEBLID_create_10(float scale_factor, int n_bits);
  127. [DllImport(LIBNAME)]
  128. private static extern IntPtr xfeatures2d_TEBLID_create_11(float scale_factor);
  129. // C++: String cv::xfeatures2d::TEBLID::getDefaultName()
  130. [DllImport(LIBNAME)]
  131. private static extern IntPtr xfeatures2d_TEBLID_getDefaultName_10(IntPtr nativeObj);
  132. // native support for java finalize()
  133. [DllImport(LIBNAME)]
  134. private static extern void xfeatures2d_TEBLID_delete(IntPtr nativeObj);
  135. }
  136. }