Dictionary.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.ObjdetectModule
  7. {
  8. // C++: class Dictionary
  9. /**
  10. * Dictionary/Set of markers, it contains the inner codification
  11. *
  12. * BytesList contains the marker codewords where:
  13. * - bytesList.rows is the dictionary size
  14. * - each marker is encoded using {code nbytes = ceil(markerSize*markerSize/8.)}
  15. * - each row contains all 4 rotations of the marker, so its length is {code 4*nbytes}
  16. *
  17. * {code bytesList.ptr(i)[k*nbytes + j]} is then the j-th byte of i-th marker, in its k-th rotation.
  18. */
  19. public class Dictionary : DisposableOpenCVObject
  20. {
  21. protected override void Dispose(bool disposing)
  22. {
  23. try
  24. {
  25. if (disposing)
  26. {
  27. }
  28. if (IsEnabledDispose)
  29. {
  30. if (nativeObj != IntPtr.Zero)
  31. objdetect_Dictionary_delete(nativeObj);
  32. nativeObj = IntPtr.Zero;
  33. }
  34. }
  35. finally
  36. {
  37. base.Dispose(disposing);
  38. }
  39. }
  40. protected internal Dictionary(IntPtr addr) : base(addr) { }
  41. public IntPtr getNativeObjAddr() { return nativeObj; }
  42. // internal usage only
  43. public static Dictionary __fromPtr__(IntPtr addr) { return new Dictionary(addr); }
  44. //
  45. // C++: cv::aruco::Dictionary::Dictionary()
  46. //
  47. public Dictionary()
  48. {
  49. nativeObj = DisposableObject.ThrowIfNullIntPtr(objdetect_Dictionary_Dictionary_10());
  50. }
  51. //
  52. // C++: cv::aruco::Dictionary::Dictionary(Mat bytesList, int _markerSize, int maxcorr = 0)
  53. //
  54. public Dictionary(Mat bytesList, int _markerSize, int maxcorr)
  55. {
  56. if (bytesList != null) bytesList.ThrowIfDisposed();
  57. nativeObj = DisposableObject.ThrowIfNullIntPtr(objdetect_Dictionary_Dictionary_11(bytesList.nativeObj, _markerSize, maxcorr));
  58. }
  59. public Dictionary(Mat bytesList, int _markerSize)
  60. {
  61. if (bytesList != null) bytesList.ThrowIfDisposed();
  62. nativeObj = DisposableObject.ThrowIfNullIntPtr(objdetect_Dictionary_Dictionary_12(bytesList.nativeObj, _markerSize));
  63. }
  64. //
  65. // C++: bool cv::aruco::Dictionary::readDictionary(FileNode fn)
  66. //
  67. // Unknown type 'FileNode' (I), skipping the function
  68. //
  69. // C++: void cv::aruco::Dictionary::writeDictionary(FileStorage fs, String name = String())
  70. //
  71. // Unknown type 'FileStorage' (I), skipping the function
  72. //
  73. // C++: bool cv::aruco::Dictionary::identify(Mat onlyBits, int& idx, int& rotation, double maxCorrectionRate)
  74. //
  75. /**
  76. * Given a matrix of bits. Returns whether if marker is identified or not.
  77. *
  78. * It returns by reference the correct id (if any) and the correct rotation
  79. * param onlyBits automatically generated
  80. * param idx automatically generated
  81. * param rotation automatically generated
  82. * param maxCorrectionRate automatically generated
  83. * return automatically generated
  84. */
  85. public bool identify(Mat onlyBits, int[] idx, int[] rotation, double maxCorrectionRate)
  86. {
  87. ThrowIfDisposed();
  88. if (onlyBits != null) onlyBits.ThrowIfDisposed();
  89. double[] idx_out = new double[1];
  90. double[] rotation_out = new double[1];
  91. bool retVal = objdetect_Dictionary_identify_10(nativeObj, onlyBits.nativeObj, idx_out, rotation_out, maxCorrectionRate);
  92. if (idx != null) idx[0] = (int)idx_out[0];
  93. if (rotation != null) rotation[0] = (int)rotation_out[0];
  94. return retVal;
  95. }
  96. //
  97. // C++: int cv::aruco::Dictionary::getDistanceToId(Mat bits, int id, bool allRotations = true)
  98. //
  99. /**
  100. * Returns the distance of the input bits to the specific id.
  101. *
  102. * If allRotations is true, the four posible bits rotation are considered
  103. * param bits automatically generated
  104. * param id automatically generated
  105. * param allRotations automatically generated
  106. * return automatically generated
  107. */
  108. public int getDistanceToId(Mat bits, int id, bool allRotations)
  109. {
  110. ThrowIfDisposed();
  111. if (bits != null) bits.ThrowIfDisposed();
  112. return objdetect_Dictionary_getDistanceToId_10(nativeObj, bits.nativeObj, id, allRotations);
  113. }
  114. /**
  115. * Returns the distance of the input bits to the specific id.
  116. *
  117. * If allRotations is true, the four posible bits rotation are considered
  118. * param bits automatically generated
  119. * param id automatically generated
  120. * return automatically generated
  121. */
  122. public int getDistanceToId(Mat bits, int id)
  123. {
  124. ThrowIfDisposed();
  125. if (bits != null) bits.ThrowIfDisposed();
  126. return objdetect_Dictionary_getDistanceToId_11(nativeObj, bits.nativeObj, id);
  127. }
  128. //
  129. // C++: void cv::aruco::Dictionary::generateImageMarker(int id, int sidePixels, Mat& _img, int borderBits = 1)
  130. //
  131. /**
  132. * Generate a canonical marker image
  133. * param id automatically generated
  134. * param sidePixels automatically generated
  135. * param _img automatically generated
  136. * param borderBits automatically generated
  137. */
  138. public void generateImageMarker(int id, int sidePixels, Mat _img, int borderBits)
  139. {
  140. ThrowIfDisposed();
  141. if (_img != null) _img.ThrowIfDisposed();
  142. objdetect_Dictionary_generateImageMarker_10(nativeObj, id, sidePixels, _img.nativeObj, borderBits);
  143. }
  144. /**
  145. * Generate a canonical marker image
  146. * param id automatically generated
  147. * param sidePixels automatically generated
  148. * param _img automatically generated
  149. */
  150. public void generateImageMarker(int id, int sidePixels, Mat _img)
  151. {
  152. ThrowIfDisposed();
  153. if (_img != null) _img.ThrowIfDisposed();
  154. objdetect_Dictionary_generateImageMarker_11(nativeObj, id, sidePixels, _img.nativeObj);
  155. }
  156. //
  157. // C++: static Mat cv::aruco::Dictionary::getByteListFromBits(Mat bits)
  158. //
  159. /**
  160. * Transform matrix of bits to list of bytes in the 4 rotations
  161. * param bits automatically generated
  162. * return automatically generated
  163. */
  164. public static Mat getByteListFromBits(Mat bits)
  165. {
  166. if (bits != null) bits.ThrowIfDisposed();
  167. return new Mat(DisposableObject.ThrowIfNullIntPtr(objdetect_Dictionary_getByteListFromBits_10(bits.nativeObj)));
  168. }
  169. //
  170. // C++: static Mat cv::aruco::Dictionary::getBitsFromByteList(Mat byteList, int markerSize)
  171. //
  172. /**
  173. * Transform list of bytes to matrix of bits
  174. * param byteList automatically generated
  175. * param markerSize automatically generated
  176. * return automatically generated
  177. */
  178. public static Mat getBitsFromByteList(Mat byteList, int markerSize)
  179. {
  180. if (byteList != null) byteList.ThrowIfDisposed();
  181. return new Mat(DisposableObject.ThrowIfNullIntPtr(objdetect_Dictionary_getBitsFromByteList_10(byteList.nativeObj, markerSize)));
  182. }
  183. //
  184. // C++: Mat Dictionary::bytesList
  185. //
  186. public Mat get_bytesList()
  187. {
  188. ThrowIfDisposed();
  189. return new Mat(DisposableObject.ThrowIfNullIntPtr(objdetect_Dictionary_get_1bytesList_10(nativeObj)));
  190. }
  191. //
  192. // C++: void Dictionary::bytesList
  193. //
  194. public void set_bytesList(Mat bytesList)
  195. {
  196. ThrowIfDisposed();
  197. if (bytesList != null) bytesList.ThrowIfDisposed();
  198. objdetect_Dictionary_set_1bytesList_10(nativeObj, bytesList.nativeObj);
  199. }
  200. //
  201. // C++: int Dictionary::markerSize
  202. //
  203. public int get_markerSize()
  204. {
  205. ThrowIfDisposed();
  206. return objdetect_Dictionary_get_1markerSize_10(nativeObj);
  207. }
  208. //
  209. // C++: void Dictionary::markerSize
  210. //
  211. public void set_markerSize(int markerSize)
  212. {
  213. ThrowIfDisposed();
  214. objdetect_Dictionary_set_1markerSize_10(nativeObj, markerSize);
  215. }
  216. //
  217. // C++: int Dictionary::maxCorrectionBits
  218. //
  219. public int get_maxCorrectionBits()
  220. {
  221. ThrowIfDisposed();
  222. return objdetect_Dictionary_get_1maxCorrectionBits_10(nativeObj);
  223. }
  224. //
  225. // C++: void Dictionary::maxCorrectionBits
  226. //
  227. public void set_maxCorrectionBits(int maxCorrectionBits)
  228. {
  229. ThrowIfDisposed();
  230. objdetect_Dictionary_set_1maxCorrectionBits_10(nativeObj, maxCorrectionBits);
  231. }
  232. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  233. const string LIBNAME = "__Internal";
  234. #else
  235. const string LIBNAME = "opencvforunity";
  236. #endif
  237. // C++: cv::aruco::Dictionary::Dictionary()
  238. [DllImport(LIBNAME)]
  239. private static extern IntPtr objdetect_Dictionary_Dictionary_10();
  240. // C++: cv::aruco::Dictionary::Dictionary(Mat bytesList, int _markerSize, int maxcorr = 0)
  241. [DllImport(LIBNAME)]
  242. private static extern IntPtr objdetect_Dictionary_Dictionary_11(IntPtr bytesList_nativeObj, int _markerSize, int maxcorr);
  243. [DllImport(LIBNAME)]
  244. private static extern IntPtr objdetect_Dictionary_Dictionary_12(IntPtr bytesList_nativeObj, int _markerSize);
  245. // C++: bool cv::aruco::Dictionary::identify(Mat onlyBits, int& idx, int& rotation, double maxCorrectionRate)
  246. [DllImport(LIBNAME)]
  247. [return: MarshalAs(UnmanagedType.U1)]
  248. private static extern bool objdetect_Dictionary_identify_10(IntPtr nativeObj, IntPtr onlyBits_nativeObj, double[] idx_out, double[] rotation_out, double maxCorrectionRate);
  249. // C++: int cv::aruco::Dictionary::getDistanceToId(Mat bits, int id, bool allRotations = true)
  250. [DllImport(LIBNAME)]
  251. private static extern int objdetect_Dictionary_getDistanceToId_10(IntPtr nativeObj, IntPtr bits_nativeObj, int id, [MarshalAs(UnmanagedType.U1)] bool allRotations);
  252. [DllImport(LIBNAME)]
  253. private static extern int objdetect_Dictionary_getDistanceToId_11(IntPtr nativeObj, IntPtr bits_nativeObj, int id);
  254. // C++: void cv::aruco::Dictionary::generateImageMarker(int id, int sidePixels, Mat& _img, int borderBits = 1)
  255. [DllImport(LIBNAME)]
  256. private static extern void objdetect_Dictionary_generateImageMarker_10(IntPtr nativeObj, int id, int sidePixels, IntPtr _img_nativeObj, int borderBits);
  257. [DllImport(LIBNAME)]
  258. private static extern void objdetect_Dictionary_generateImageMarker_11(IntPtr nativeObj, int id, int sidePixels, IntPtr _img_nativeObj);
  259. // C++: static Mat cv::aruco::Dictionary::getByteListFromBits(Mat bits)
  260. [DllImport(LIBNAME)]
  261. private static extern IntPtr objdetect_Dictionary_getByteListFromBits_10(IntPtr bits_nativeObj);
  262. // C++: static Mat cv::aruco::Dictionary::getBitsFromByteList(Mat byteList, int markerSize)
  263. [DllImport(LIBNAME)]
  264. private static extern IntPtr objdetect_Dictionary_getBitsFromByteList_10(IntPtr byteList_nativeObj, int markerSize);
  265. // C++: Mat Dictionary::bytesList
  266. [DllImport(LIBNAME)]
  267. private static extern IntPtr objdetect_Dictionary_get_1bytesList_10(IntPtr nativeObj);
  268. // C++: void Dictionary::bytesList
  269. [DllImport(LIBNAME)]
  270. private static extern void objdetect_Dictionary_set_1bytesList_10(IntPtr nativeObj, IntPtr bytesList_nativeObj);
  271. // C++: int Dictionary::markerSize
  272. [DllImport(LIBNAME)]
  273. private static extern int objdetect_Dictionary_get_1markerSize_10(IntPtr nativeObj);
  274. // C++: void Dictionary::markerSize
  275. [DllImport(LIBNAME)]
  276. private static extern void objdetect_Dictionary_set_1markerSize_10(IntPtr nativeObj, int markerSize);
  277. // C++: int Dictionary::maxCorrectionBits
  278. [DllImport(LIBNAME)]
  279. private static extern int objdetect_Dictionary_get_1maxCorrectionBits_10(IntPtr nativeObj);
  280. // C++: void Dictionary::maxCorrectionBits
  281. [DllImport(LIBNAME)]
  282. private static extern void objdetect_Dictionary_set_1maxCorrectionBits_10(IntPtr nativeObj, int maxCorrectionBits);
  283. // native support for java finalize()
  284. [DllImport(LIBNAME)]
  285. private static extern void objdetect_Dictionary_delete(IntPtr nativeObj);
  286. }
  287. }