CharucoBoard.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 CharucoBoard
  9. /**
  10. * ChArUco board is a planar chessboard where the markers are placed inside the white squares of a chessboard.
  11. *
  12. * The benefits of ChArUco boards is that they provide both, ArUco markers versatility and chessboard corner precision,
  13. * which is important for calibration and pose estimation. The board image can be drawn using generateImage() method.
  14. */
  15. public class CharucoBoard : Board
  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. objdetect_CharucoBoard_delete(nativeObj);
  28. nativeObj = IntPtr.Zero;
  29. }
  30. }
  31. finally
  32. {
  33. base.Dispose(disposing);
  34. }
  35. }
  36. protected internal CharucoBoard(IntPtr addr) : base(addr) { }
  37. // internal usage only
  38. public static new CharucoBoard __fromPtr__(IntPtr addr) { return new CharucoBoard(addr); }
  39. //
  40. // C++: cv::aruco::CharucoBoard::CharucoBoard(Size size, float squareLength, float markerLength, Dictionary dictionary, Mat ids = Mat())
  41. //
  42. /**
  43. * CharucoBoard constructor
  44. *
  45. * param size number of chessboard squares in x and y directions
  46. * param squareLength squareLength chessboard square side length (normally in meters)
  47. * param markerLength marker side length (same unit than squareLength)
  48. * param dictionary dictionary of markers indicating the type of markers
  49. * param ids array of id used markers
  50. * The first markers in the dictionary are used to fill the white chessboard squares.
  51. */
  52. public CharucoBoard(Size size, float squareLength, float markerLength, Dictionary dictionary, Mat ids) :
  53. base(DisposableObject.ThrowIfNullIntPtr(objdetect_CharucoBoard_CharucoBoard_10(size.width, size.height, squareLength, markerLength, dictionary.nativeObj, ids.nativeObj)))
  54. {
  55. }
  56. /**
  57. * CharucoBoard constructor
  58. *
  59. * param size number of chessboard squares in x and y directions
  60. * param squareLength squareLength chessboard square side length (normally in meters)
  61. * param markerLength marker side length (same unit than squareLength)
  62. * param dictionary dictionary of markers indicating the type of markers
  63. * The first markers in the dictionary are used to fill the white chessboard squares.
  64. */
  65. public CharucoBoard(Size size, float squareLength, float markerLength, Dictionary dictionary) :
  66. base(DisposableObject.ThrowIfNullIntPtr(objdetect_CharucoBoard_CharucoBoard_11(size.width, size.height, squareLength, markerLength, dictionary.nativeObj)))
  67. {
  68. }
  69. //
  70. // C++: void cv::aruco::CharucoBoard::setLegacyPattern(bool legacyPattern)
  71. //
  72. /**
  73. * set legacy chessboard pattern.
  74. *
  75. * Legacy setting creates chessboard patterns starting with a white box in the upper left corner
  76. * if there is an even row count of chessboard boxes, otherwise it starts with a black box.
  77. * This setting ensures compatibility to patterns created with OpenCV versions prior OpenCV 4.6.0.
  78. * See https://github.com/opencv/opencv/issues/23152.
  79. *
  80. * Default value: false.
  81. * param legacyPattern automatically generated
  82. */
  83. public void setLegacyPattern(bool legacyPattern)
  84. {
  85. ThrowIfDisposed();
  86. objdetect_CharucoBoard_setLegacyPattern_10(nativeObj, legacyPattern);
  87. }
  88. //
  89. // C++: bool cv::aruco::CharucoBoard::getLegacyPattern()
  90. //
  91. public bool getLegacyPattern()
  92. {
  93. ThrowIfDisposed();
  94. return objdetect_CharucoBoard_getLegacyPattern_10(nativeObj);
  95. }
  96. //
  97. // C++: Size cv::aruco::CharucoBoard::getChessboardSize()
  98. //
  99. public Size getChessboardSize()
  100. {
  101. ThrowIfDisposed();
  102. double[] tmpArray = new double[2];
  103. objdetect_CharucoBoard_getChessboardSize_10(nativeObj, tmpArray);
  104. Size retVal = new Size(tmpArray);
  105. return retVal;
  106. }
  107. //
  108. // C++: float cv::aruco::CharucoBoard::getSquareLength()
  109. //
  110. public float getSquareLength()
  111. {
  112. ThrowIfDisposed();
  113. return objdetect_CharucoBoard_getSquareLength_10(nativeObj);
  114. }
  115. //
  116. // C++: float cv::aruco::CharucoBoard::getMarkerLength()
  117. //
  118. public float getMarkerLength()
  119. {
  120. ThrowIfDisposed();
  121. return objdetect_CharucoBoard_getMarkerLength_10(nativeObj);
  122. }
  123. //
  124. // C++: vector_Point3f cv::aruco::CharucoBoard::getChessboardCorners()
  125. //
  126. /**
  127. * get CharucoBoard::chessboardCorners
  128. * return automatically generated
  129. */
  130. public MatOfPoint3f getChessboardCorners()
  131. {
  132. ThrowIfDisposed();
  133. return MatOfPoint3f.fromNativeAddr(DisposableObject.ThrowIfNullIntPtr(objdetect_CharucoBoard_getChessboardCorners_10(nativeObj)));
  134. }
  135. //
  136. // C++: bool cv::aruco::CharucoBoard::checkCharucoCornersCollinear(Mat charucoIds)
  137. //
  138. /**
  139. * check whether the ChArUco markers are collinear
  140. *
  141. * param charucoIds list of identifiers for each corner in charucoCorners per frame.
  142. * return bool value, 1 (true) if detected corners form a line, 0 (false) if they do not.
  143. * solvePnP, calibration functions will fail if the corners are collinear (true).
  144. *
  145. * The number of ids in charucoIDs should be <= the number of chessboard corners in the board.
  146. * This functions checks whether the charuco corners are on a straight line (returns true, if so), or not (false).
  147. * Axis parallel, as well as diagonal and other straight lines detected. Degenerate cases:
  148. * for number of charucoIDs <= 2,the function returns true.
  149. */
  150. public bool checkCharucoCornersCollinear(Mat charucoIds)
  151. {
  152. ThrowIfDisposed();
  153. if (charucoIds != null) charucoIds.ThrowIfDisposed();
  154. return objdetect_CharucoBoard_checkCharucoCornersCollinear_10(nativeObj, charucoIds.nativeObj);
  155. }
  156. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  157. const string LIBNAME = "__Internal";
  158. #else
  159. const string LIBNAME = "opencvforunity";
  160. #endif
  161. // C++: cv::aruco::CharucoBoard::CharucoBoard(Size size, float squareLength, float markerLength, Dictionary dictionary, Mat ids = Mat())
  162. [DllImport(LIBNAME)]
  163. private static extern IntPtr objdetect_CharucoBoard_CharucoBoard_10(double size_width, double size_height, float squareLength, float markerLength, IntPtr dictionary_nativeObj, IntPtr ids_nativeObj);
  164. [DllImport(LIBNAME)]
  165. private static extern IntPtr objdetect_CharucoBoard_CharucoBoard_11(double size_width, double size_height, float squareLength, float markerLength, IntPtr dictionary_nativeObj);
  166. // C++: void cv::aruco::CharucoBoard::setLegacyPattern(bool legacyPattern)
  167. [DllImport(LIBNAME)]
  168. private static extern void objdetect_CharucoBoard_setLegacyPattern_10(IntPtr nativeObj, [MarshalAs(UnmanagedType.U1)] bool legacyPattern);
  169. // C++: bool cv::aruco::CharucoBoard::getLegacyPattern()
  170. [DllImport(LIBNAME)]
  171. [return: MarshalAs(UnmanagedType.U1)]
  172. private static extern bool objdetect_CharucoBoard_getLegacyPattern_10(IntPtr nativeObj);
  173. // C++: Size cv::aruco::CharucoBoard::getChessboardSize()
  174. [DllImport(LIBNAME)]
  175. private static extern void objdetect_CharucoBoard_getChessboardSize_10(IntPtr nativeObj, double[] retVal);
  176. // C++: float cv::aruco::CharucoBoard::getSquareLength()
  177. [DllImport(LIBNAME)]
  178. private static extern float objdetect_CharucoBoard_getSquareLength_10(IntPtr nativeObj);
  179. // C++: float cv::aruco::CharucoBoard::getMarkerLength()
  180. [DllImport(LIBNAME)]
  181. private static extern float objdetect_CharucoBoard_getMarkerLength_10(IntPtr nativeObj);
  182. // C++: vector_Point3f cv::aruco::CharucoBoard::getChessboardCorners()
  183. [DllImport(LIBNAME)]
  184. private static extern IntPtr objdetect_CharucoBoard_getChessboardCorners_10(IntPtr nativeObj);
  185. // C++: bool cv::aruco::CharucoBoard::checkCharucoCornersCollinear(Mat charucoIds)
  186. [DllImport(LIBNAME)]
  187. [return: MarshalAs(UnmanagedType.U1)]
  188. private static extern bool objdetect_CharucoBoard_checkCharucoCornersCollinear_10(IntPtr nativeObj, IntPtr charucoIds_nativeObj);
  189. // native support for java finalize()
  190. [DllImport(LIBNAME)]
  191. private static extern void objdetect_CharucoBoard_delete(IntPtr nativeObj);
  192. }
  193. }