Xfeatures2d.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 Xfeatures2d
  10. public class Xfeatures2d
  11. {
  12. //
  13. // C++: void cv::xfeatures2d::matchGMS(Size size1, Size size2, vector_KeyPoint keypoints1, vector_KeyPoint keypoints2, vector_DMatch matches1to2, vector_DMatch& matchesGMS, bool withRotation = false, bool withScale = false, double thresholdFactor = 6.0)
  14. //
  15. /**
  16. * GMS (Grid-based Motion Statistics) feature matching strategy described in CITE: Bian2017gms .
  17. * param size1 Input size of image1.
  18. * param size2 Input size of image2.
  19. * param keypoints1 Input keypoints of image1.
  20. * param keypoints2 Input keypoints of image2.
  21. * param matches1to2 Input 1-nearest neighbor matches.
  22. * param matchesGMS Matches returned by the GMS matching strategy.
  23. * param withRotation Take rotation transformation into account.
  24. * param withScale Take scale transformation into account.
  25. * param thresholdFactor The higher, the less matches.
  26. * <b>Note:</b>
  27. * Since GMS works well when the number of features is large, we recommend to use the ORB feature and set FastThreshold to 0 to get as many as possible features quickly.
  28. * If matching results are not satisfying, please add more features. (We use 10000 for images with 640 X 480).
  29. * If your images have big rotation and scale changes, please set withRotation or withScale to true.
  30. */
  31. public static void matchGMS(Size size1, Size size2, MatOfKeyPoint keypoints1, MatOfKeyPoint keypoints2, MatOfDMatch matches1to2, MatOfDMatch matchesGMS, bool withRotation, bool withScale, double thresholdFactor)
  32. {
  33. if (keypoints1 != null) keypoints1.ThrowIfDisposed();
  34. if (keypoints2 != null) keypoints2.ThrowIfDisposed();
  35. if (matches1to2 != null) matches1to2.ThrowIfDisposed();
  36. if (matchesGMS != null) matchesGMS.ThrowIfDisposed();
  37. Mat keypoints1_mat = keypoints1;
  38. Mat keypoints2_mat = keypoints2;
  39. Mat matches1to2_mat = matches1to2;
  40. Mat matchesGMS_mat = matchesGMS;
  41. xfeatures2d_Xfeatures2d_matchGMS_10(size1.width, size1.height, size2.width, size2.height, keypoints1_mat.nativeObj, keypoints2_mat.nativeObj, matches1to2_mat.nativeObj, matchesGMS_mat.nativeObj, withRotation, withScale, thresholdFactor);
  42. }
  43. /**
  44. * GMS (Grid-based Motion Statistics) feature matching strategy described in CITE: Bian2017gms .
  45. * param size1 Input size of image1.
  46. * param size2 Input size of image2.
  47. * param keypoints1 Input keypoints of image1.
  48. * param keypoints2 Input keypoints of image2.
  49. * param matches1to2 Input 1-nearest neighbor matches.
  50. * param matchesGMS Matches returned by the GMS matching strategy.
  51. * param withRotation Take rotation transformation into account.
  52. * param withScale Take scale transformation into account.
  53. * <b>Note:</b>
  54. * Since GMS works well when the number of features is large, we recommend to use the ORB feature and set FastThreshold to 0 to get as many as possible features quickly.
  55. * If matching results are not satisfying, please add more features. (We use 10000 for images with 640 X 480).
  56. * If your images have big rotation and scale changes, please set withRotation or withScale to true.
  57. */
  58. public static void matchGMS(Size size1, Size size2, MatOfKeyPoint keypoints1, MatOfKeyPoint keypoints2, MatOfDMatch matches1to2, MatOfDMatch matchesGMS, bool withRotation, bool withScale)
  59. {
  60. if (keypoints1 != null) keypoints1.ThrowIfDisposed();
  61. if (keypoints2 != null) keypoints2.ThrowIfDisposed();
  62. if (matches1to2 != null) matches1to2.ThrowIfDisposed();
  63. if (matchesGMS != null) matchesGMS.ThrowIfDisposed();
  64. Mat keypoints1_mat = keypoints1;
  65. Mat keypoints2_mat = keypoints2;
  66. Mat matches1to2_mat = matches1to2;
  67. Mat matchesGMS_mat = matchesGMS;
  68. xfeatures2d_Xfeatures2d_matchGMS_11(size1.width, size1.height, size2.width, size2.height, keypoints1_mat.nativeObj, keypoints2_mat.nativeObj, matches1to2_mat.nativeObj, matchesGMS_mat.nativeObj, withRotation, withScale);
  69. }
  70. /**
  71. * GMS (Grid-based Motion Statistics) feature matching strategy described in CITE: Bian2017gms .
  72. * param size1 Input size of image1.
  73. * param size2 Input size of image2.
  74. * param keypoints1 Input keypoints of image1.
  75. * param keypoints2 Input keypoints of image2.
  76. * param matches1to2 Input 1-nearest neighbor matches.
  77. * param matchesGMS Matches returned by the GMS matching strategy.
  78. * param withRotation Take rotation transformation into account.
  79. * <b>Note:</b>
  80. * Since GMS works well when the number of features is large, we recommend to use the ORB feature and set FastThreshold to 0 to get as many as possible features quickly.
  81. * If matching results are not satisfying, please add more features. (We use 10000 for images with 640 X 480).
  82. * If your images have big rotation and scale changes, please set withRotation or withScale to true.
  83. */
  84. public static void matchGMS(Size size1, Size size2, MatOfKeyPoint keypoints1, MatOfKeyPoint keypoints2, MatOfDMatch matches1to2, MatOfDMatch matchesGMS, bool withRotation)
  85. {
  86. if (keypoints1 != null) keypoints1.ThrowIfDisposed();
  87. if (keypoints2 != null) keypoints2.ThrowIfDisposed();
  88. if (matches1to2 != null) matches1to2.ThrowIfDisposed();
  89. if (matchesGMS != null) matchesGMS.ThrowIfDisposed();
  90. Mat keypoints1_mat = keypoints1;
  91. Mat keypoints2_mat = keypoints2;
  92. Mat matches1to2_mat = matches1to2;
  93. Mat matchesGMS_mat = matchesGMS;
  94. xfeatures2d_Xfeatures2d_matchGMS_12(size1.width, size1.height, size2.width, size2.height, keypoints1_mat.nativeObj, keypoints2_mat.nativeObj, matches1to2_mat.nativeObj, matchesGMS_mat.nativeObj, withRotation);
  95. }
  96. /**
  97. * GMS (Grid-based Motion Statistics) feature matching strategy described in CITE: Bian2017gms .
  98. * param size1 Input size of image1.
  99. * param size2 Input size of image2.
  100. * param keypoints1 Input keypoints of image1.
  101. * param keypoints2 Input keypoints of image2.
  102. * param matches1to2 Input 1-nearest neighbor matches.
  103. * param matchesGMS Matches returned by the GMS matching strategy.
  104. * <b>Note:</b>
  105. * Since GMS works well when the number of features is large, we recommend to use the ORB feature and set FastThreshold to 0 to get as many as possible features quickly.
  106. * If matching results are not satisfying, please add more features. (We use 10000 for images with 640 X 480).
  107. * If your images have big rotation and scale changes, please set withRotation or withScale to true.
  108. */
  109. public static void matchGMS(Size size1, Size size2, MatOfKeyPoint keypoints1, MatOfKeyPoint keypoints2, MatOfDMatch matches1to2, MatOfDMatch matchesGMS)
  110. {
  111. if (keypoints1 != null) keypoints1.ThrowIfDisposed();
  112. if (keypoints2 != null) keypoints2.ThrowIfDisposed();
  113. if (matches1to2 != null) matches1to2.ThrowIfDisposed();
  114. if (matchesGMS != null) matchesGMS.ThrowIfDisposed();
  115. Mat keypoints1_mat = keypoints1;
  116. Mat keypoints2_mat = keypoints2;
  117. Mat matches1to2_mat = matches1to2;
  118. Mat matchesGMS_mat = matchesGMS;
  119. xfeatures2d_Xfeatures2d_matchGMS_13(size1.width, size1.height, size2.width, size2.height, keypoints1_mat.nativeObj, keypoints2_mat.nativeObj, matches1to2_mat.nativeObj, matchesGMS_mat.nativeObj);
  120. }
  121. //
  122. // C++: void cv::xfeatures2d::matchLOGOS(vector_KeyPoint keypoints1, vector_KeyPoint keypoints2, vector_int nn1, vector_int nn2, vector_DMatch matches1to2)
  123. //
  124. /**
  125. * LOGOS (Local geometric support for high-outlier spatial verification) feature matching strategy described in CITE: Lowry2018LOGOSLG .
  126. * param keypoints1 Input keypoints of image1.
  127. * param keypoints2 Input keypoints of image2.
  128. * param nn1 Index to the closest BoW centroid for each descriptors of image1.
  129. * param nn2 Index to the closest BoW centroid for each descriptors of image2.
  130. * param matches1to2 Matches returned by the LOGOS matching strategy.
  131. * <b>Note:</b>
  132. * This matching strategy is suitable for features matching against large scale database.
  133. * First step consists in constructing the bag-of-words (BoW) from a representative image database.
  134. * Image descriptors are then represented by their closest codevector (nearest BoW centroid).
  135. */
  136. public static void matchLOGOS(MatOfKeyPoint keypoints1, MatOfKeyPoint keypoints2, MatOfInt nn1, MatOfInt nn2, MatOfDMatch matches1to2)
  137. {
  138. if (keypoints1 != null) keypoints1.ThrowIfDisposed();
  139. if (keypoints2 != null) keypoints2.ThrowIfDisposed();
  140. if (nn1 != null) nn1.ThrowIfDisposed();
  141. if (nn2 != null) nn2.ThrowIfDisposed();
  142. if (matches1to2 != null) matches1to2.ThrowIfDisposed();
  143. Mat keypoints1_mat = keypoints1;
  144. Mat keypoints2_mat = keypoints2;
  145. Mat nn1_mat = nn1;
  146. Mat nn2_mat = nn2;
  147. Mat matches1to2_mat = matches1to2;
  148. xfeatures2d_Xfeatures2d_matchLOGOS_10(keypoints1_mat.nativeObj, keypoints2_mat.nativeObj, nn1_mat.nativeObj, nn2_mat.nativeObj, matches1to2_mat.nativeObj);
  149. }
  150. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  151. const string LIBNAME = "__Internal";
  152. #else
  153. const string LIBNAME = "opencvforunity";
  154. #endif
  155. // C++: void cv::xfeatures2d::matchGMS(Size size1, Size size2, vector_KeyPoint keypoints1, vector_KeyPoint keypoints2, vector_DMatch matches1to2, vector_DMatch& matchesGMS, bool withRotation = false, bool withScale = false, double thresholdFactor = 6.0)
  156. [DllImport(LIBNAME)]
  157. private static extern void xfeatures2d_Xfeatures2d_matchGMS_10(double size1_width, double size1_height, double size2_width, double size2_height, IntPtr keypoints1_mat_nativeObj, IntPtr keypoints2_mat_nativeObj, IntPtr matches1to2_mat_nativeObj, IntPtr matchesGMS_mat_nativeObj, [MarshalAs(UnmanagedType.U1)] bool withRotation, [MarshalAs(UnmanagedType.U1)] bool withScale, double thresholdFactor);
  158. [DllImport(LIBNAME)]
  159. private static extern void xfeatures2d_Xfeatures2d_matchGMS_11(double size1_width, double size1_height, double size2_width, double size2_height, IntPtr keypoints1_mat_nativeObj, IntPtr keypoints2_mat_nativeObj, IntPtr matches1to2_mat_nativeObj, IntPtr matchesGMS_mat_nativeObj, [MarshalAs(UnmanagedType.U1)] bool withRotation, [MarshalAs(UnmanagedType.U1)] bool withScale);
  160. [DllImport(LIBNAME)]
  161. private static extern void xfeatures2d_Xfeatures2d_matchGMS_12(double size1_width, double size1_height, double size2_width, double size2_height, IntPtr keypoints1_mat_nativeObj, IntPtr keypoints2_mat_nativeObj, IntPtr matches1to2_mat_nativeObj, IntPtr matchesGMS_mat_nativeObj, [MarshalAs(UnmanagedType.U1)] bool withRotation);
  162. [DllImport(LIBNAME)]
  163. private static extern void xfeatures2d_Xfeatures2d_matchGMS_13(double size1_width, double size1_height, double size2_width, double size2_height, IntPtr keypoints1_mat_nativeObj, IntPtr keypoints2_mat_nativeObj, IntPtr matches1to2_mat_nativeObj, IntPtr matchesGMS_mat_nativeObj);
  164. // C++: void cv::xfeatures2d::matchLOGOS(vector_KeyPoint keypoints1, vector_KeyPoint keypoints2, vector_int nn1, vector_int nn2, vector_DMatch matches1to2)
  165. [DllImport(LIBNAME)]
  166. private static extern void xfeatures2d_Xfeatures2d_matchLOGOS_10(IntPtr keypoints1_mat_nativeObj, IntPtr keypoints2_mat_nativeObj, IntPtr nn1_mat_nativeObj, IntPtr nn2_mat_nativeObj, IntPtr matches1to2_mat_nativeObj);
  167. }
  168. }