Board.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 Board
  9. /**
  10. * Board of ArUco markers
  11. *
  12. * A board is a set of markers in the 3D space with a common coordinate system.
  13. * The common form of a board of marker is a planar (2D) board, however any 3D layout can be used.
  14. * A Board object is composed by:
  15. * - The object points of the marker corners, i.e. their coordinates respect to the board system.
  16. * - The dictionary which indicates the type of markers of the board
  17. * - The identifier of all the markers in the board.
  18. */
  19. public class Board : 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_Board_delete(nativeObj);
  32. nativeObj = IntPtr.Zero;
  33. }
  34. }
  35. finally
  36. {
  37. base.Dispose(disposing);
  38. }
  39. }
  40. protected internal Board(IntPtr addr) : base(addr) { }
  41. public IntPtr getNativeObjAddr() { return nativeObj; }
  42. // internal usage only
  43. public static Board __fromPtr__(IntPtr addr) { return new Board(addr); }
  44. //
  45. // C++: cv::aruco::Board::Board(vector_Mat objPoints, Dictionary dictionary, Mat ids)
  46. //
  47. /**
  48. * Common Board constructor
  49. *
  50. * param objPoints array of object points of all the marker corners in the board
  51. * param dictionary the dictionary of markers employed for this board
  52. * param ids vector of the identifiers of the markers in the board
  53. */
  54. public Board(List<Mat> objPoints, Dictionary dictionary, Mat ids)
  55. {
  56. if (dictionary != null) dictionary.ThrowIfDisposed();
  57. if (ids != null) ids.ThrowIfDisposed();
  58. Mat objPoints_mat = Converters.vector_Mat_to_Mat(objPoints);
  59. nativeObj = DisposableObject.ThrowIfNullIntPtr(objdetect_Board_Board_10(objPoints_mat.nativeObj, dictionary.nativeObj, ids.nativeObj));
  60. }
  61. //
  62. // C++: Dictionary cv::aruco::Board::getDictionary()
  63. //
  64. /**
  65. * return the Dictionary of markers employed for this board
  66. * return automatically generated
  67. */
  68. public Dictionary getDictionary()
  69. {
  70. ThrowIfDisposed();
  71. return new Dictionary(DisposableObject.ThrowIfNullIntPtr(objdetect_Board_getDictionary_10(nativeObj)));
  72. }
  73. //
  74. // C++: vector_vector_Point3f cv::aruco::Board::getObjPoints()
  75. //
  76. /**
  77. * return array of object points of all the marker corners in the board.
  78. *
  79. * Each marker include its 4 corners in this order:
  80. * - objPoints[i][0] - left-top point of i-th marker
  81. * - objPoints[i][1] - right-top point of i-th marker
  82. * - objPoints[i][2] - right-bottom point of i-th marker
  83. * - objPoints[i][3] - left-bottom point of i-th marker
  84. *
  85. * Markers are placed in a certain order - row by row, left to right in every row. For M markers, the size is Mx4.
  86. * return automatically generated
  87. */
  88. public List<MatOfPoint3f> getObjPoints()
  89. {
  90. ThrowIfDisposed();
  91. List<MatOfPoint3f> retVal = new List<MatOfPoint3f>();
  92. Mat retValMat = new Mat(DisposableObject.ThrowIfNullIntPtr(objdetect_Board_getObjPoints_10(nativeObj)));
  93. Converters.Mat_to_vector_vector_Point3f(retValMat, retVal);
  94. return retVal;
  95. }
  96. //
  97. // C++: vector_int cv::aruco::Board::getIds()
  98. //
  99. /**
  100. * vector of the identifiers of the markers in the board (should be the same size as objPoints)
  101. * return vector of the identifiers of the markers
  102. */
  103. public MatOfInt getIds()
  104. {
  105. ThrowIfDisposed();
  106. return MatOfInt.fromNativeAddr(DisposableObject.ThrowIfNullIntPtr(objdetect_Board_getIds_10(nativeObj)));
  107. }
  108. //
  109. // C++: Point3f cv::aruco::Board::getRightBottomCorner()
  110. //
  111. /**
  112. * get coordinate of the bottom right corner of the board, is set when calling the function create()
  113. * return automatically generated
  114. */
  115. public Point3 getRightBottomCorner()
  116. {
  117. ThrowIfDisposed();
  118. double[] tmpArray = new double[3];
  119. objdetect_Board_getRightBottomCorner_10(nativeObj, tmpArray);
  120. Point3 retVal = new Point3(tmpArray);
  121. return retVal;
  122. }
  123. //
  124. // C++: void cv::aruco::Board::matchImagePoints(vector_Mat detectedCorners, Mat detectedIds, Mat& objPoints, Mat& imgPoints)
  125. //
  126. /**
  127. * Given a board configuration and a set of detected markers, returns the corresponding
  128. * image points and object points, can be used in solvePnP()
  129. *
  130. * param detectedCorners List of detected marker corners of the board.
  131. * For cv::Board and cv::GridBoard the method expects std::vector&lt;std::vector&lt;Point2f&gt;&gt; or std::vector&lt;Mat&gt; with Aruco marker corners.
  132. * For cv::CharucoBoard the method expects std::vector&lt;Point2f&gt; or Mat with ChAruco corners (chess board corners matched with Aruco markers).
  133. *
  134. * param detectedIds List of identifiers for each marker or charuco corner.
  135. * For any Board class the method expects std::vector&lt;int&gt; or Mat.
  136. *
  137. * param objPoints Vector of marker points in the board coordinate space.
  138. * For any Board class the method expects std::vector&lt;cv::Point3f&gt; objectPoints or cv::Mat
  139. *
  140. * param imgPoints Vector of marker points in the image coordinate space.
  141. * For any Board class the method expects std::vector&lt;cv::Point2f&gt; objectPoints or cv::Mat
  142. *
  143. * SEE: solvePnP
  144. */
  145. public void matchImagePoints(List<Mat> detectedCorners, Mat detectedIds, Mat objPoints, Mat imgPoints)
  146. {
  147. ThrowIfDisposed();
  148. if (detectedIds != null) detectedIds.ThrowIfDisposed();
  149. if (objPoints != null) objPoints.ThrowIfDisposed();
  150. if (imgPoints != null) imgPoints.ThrowIfDisposed();
  151. Mat detectedCorners_mat = Converters.vector_Mat_to_Mat(detectedCorners);
  152. objdetect_Board_matchImagePoints_10(nativeObj, detectedCorners_mat.nativeObj, detectedIds.nativeObj, objPoints.nativeObj, imgPoints.nativeObj);
  153. }
  154. //
  155. // C++: void cv::aruco::Board::generateImage(Size outSize, Mat& img, int marginSize = 0, int borderBits = 1)
  156. //
  157. /**
  158. * Draw a planar board
  159. *
  160. * param outSize size of the output image in pixels.
  161. * param img output image with the board. The size of this image will be outSize
  162. * and the board will be on the center, keeping the board proportions.
  163. * param marginSize minimum margins (in pixels) of the board in the output image
  164. * param borderBits width of the marker borders.
  165. *
  166. * This function return the image of the board, ready to be printed.
  167. */
  168. public void generateImage(Size outSize, Mat img, int marginSize, int borderBits)
  169. {
  170. ThrowIfDisposed();
  171. if (img != null) img.ThrowIfDisposed();
  172. objdetect_Board_generateImage_10(nativeObj, outSize.width, outSize.height, img.nativeObj, marginSize, borderBits);
  173. }
  174. /**
  175. * Draw a planar board
  176. *
  177. * param outSize size of the output image in pixels.
  178. * param img output image with the board. The size of this image will be outSize
  179. * and the board will be on the center, keeping the board proportions.
  180. * param marginSize minimum margins (in pixels) of the board in the output image
  181. *
  182. * This function return the image of the board, ready to be printed.
  183. */
  184. public void generateImage(Size outSize, Mat img, int marginSize)
  185. {
  186. ThrowIfDisposed();
  187. if (img != null) img.ThrowIfDisposed();
  188. objdetect_Board_generateImage_11(nativeObj, outSize.width, outSize.height, img.nativeObj, marginSize);
  189. }
  190. /**
  191. * Draw a planar board
  192. *
  193. * param outSize size of the output image in pixels.
  194. * param img output image with the board. The size of this image will be outSize
  195. * and the board will be on the center, keeping the board proportions.
  196. *
  197. * This function return the image of the board, ready to be printed.
  198. */
  199. public void generateImage(Size outSize, Mat img)
  200. {
  201. ThrowIfDisposed();
  202. if (img != null) img.ThrowIfDisposed();
  203. objdetect_Board_generateImage_12(nativeObj, outSize.width, outSize.height, img.nativeObj);
  204. }
  205. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  206. const string LIBNAME = "__Internal";
  207. #else
  208. const string LIBNAME = "opencvforunity";
  209. #endif
  210. // C++: cv::aruco::Board::Board(vector_Mat objPoints, Dictionary dictionary, Mat ids)
  211. [DllImport(LIBNAME)]
  212. private static extern IntPtr objdetect_Board_Board_10(IntPtr objPoints_mat_nativeObj, IntPtr dictionary_nativeObj, IntPtr ids_nativeObj);
  213. // C++: Dictionary cv::aruco::Board::getDictionary()
  214. [DllImport(LIBNAME)]
  215. private static extern IntPtr objdetect_Board_getDictionary_10(IntPtr nativeObj);
  216. // C++: vector_vector_Point3f cv::aruco::Board::getObjPoints()
  217. [DllImport(LIBNAME)]
  218. private static extern IntPtr objdetect_Board_getObjPoints_10(IntPtr nativeObj);
  219. // C++: vector_int cv::aruco::Board::getIds()
  220. [DllImport(LIBNAME)]
  221. private static extern IntPtr objdetect_Board_getIds_10(IntPtr nativeObj);
  222. // C++: Point3f cv::aruco::Board::getRightBottomCorner()
  223. [DllImport(LIBNAME)]
  224. private static extern void objdetect_Board_getRightBottomCorner_10(IntPtr nativeObj, double[] retVal);
  225. // C++: void cv::aruco::Board::matchImagePoints(vector_Mat detectedCorners, Mat detectedIds, Mat& objPoints, Mat& imgPoints)
  226. [DllImport(LIBNAME)]
  227. private static extern void objdetect_Board_matchImagePoints_10(IntPtr nativeObj, IntPtr detectedCorners_mat_nativeObj, IntPtr detectedIds_nativeObj, IntPtr objPoints_nativeObj, IntPtr imgPoints_nativeObj);
  228. // C++: void cv::aruco::Board::generateImage(Size outSize, Mat& img, int marginSize = 0, int borderBits = 1)
  229. [DllImport(LIBNAME)]
  230. private static extern void objdetect_Board_generateImage_10(IntPtr nativeObj, double outSize_width, double outSize_height, IntPtr img_nativeObj, int marginSize, int borderBits);
  231. [DllImport(LIBNAME)]
  232. private static extern void objdetect_Board_generateImage_11(IntPtr nativeObj, double outSize_width, double outSize_height, IntPtr img_nativeObj, int marginSize);
  233. [DllImport(LIBNAME)]
  234. private static extern void objdetect_Board_generateImage_12(IntPtr nativeObj, double outSize_width, double outSize_height, IntPtr img_nativeObj);
  235. // native support for java finalize()
  236. [DllImport(LIBNAME)]
  237. private static extern void objdetect_Board_delete(IntPtr nativeObj);
  238. }
  239. }