charuco.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. */
  31. #ifndef __OPENCV_CHARUCO_HPP__
  32. #define __OPENCV_CHARUCO_HPP__
  33. #include <opencv2/core.hpp>
  34. #include <vector>
  35. #include <opencv2/aruco.hpp>
  36. namespace cv {
  37. namespace aruco {
  38. //! @addtogroup aruco
  39. //! @{
  40. /**
  41. * @brief ChArUco board
  42. * Specific class for ChArUco boards. A ChArUco board is a planar board where the markers are placed
  43. * inside the white squares of a chessboard. The benefits of ChArUco boards is that they provide
  44. * both, ArUco markers versatility and chessboard corner precision, which is important for
  45. * calibration and pose estimation.
  46. * This class also allows the easy creation and drawing of ChArUco boards.
  47. */
  48. class CV_EXPORTS_W CharucoBoard : public Board {
  49. public:
  50. // vector of chessboard 3D corners precalculated
  51. CV_PROP std::vector< Point3f > chessboardCorners;
  52. // for each charuco corner, nearest marker id and nearest marker corner id of each marker
  53. CV_PROP std::vector< std::vector< int > > nearestMarkerIdx;
  54. CV_PROP std::vector< std::vector< int > > nearestMarkerCorners;
  55. /**
  56. * @brief Draw a ChArUco board
  57. *
  58. * @param outSize size of the output image in pixels.
  59. * @param img output image with the board. The size of this image will be outSize
  60. * and the board will be on the center, keeping the board proportions.
  61. * @param marginSize minimum margins (in pixels) of the board in the output image
  62. * @param borderBits width of the marker borders.
  63. *
  64. * This function return the image of the ChArUco board, ready to be printed.
  65. */
  66. CV_WRAP void draw(Size outSize, OutputArray img, int marginSize = 0, int borderBits = 1);
  67. /**
  68. * @brief Create a CharucoBoard object
  69. *
  70. * @param squaresX number of chessboard squares in X direction
  71. * @param squaresY number of chessboard squares in Y direction
  72. * @param squareLength chessboard square side length (normally in meters)
  73. * @param markerLength marker side length (same unit than squareLength)
  74. * @param dictionary dictionary of markers indicating the type of markers.
  75. * The first markers in the dictionary are used to fill the white chessboard squares.
  76. * @return the output CharucoBoard object
  77. *
  78. * This functions creates a CharucoBoard object given the number of squares in each direction
  79. * and the size of the markers and chessboard squares.
  80. */
  81. CV_WRAP static Ptr<CharucoBoard> create(int squaresX, int squaresY, float squareLength,
  82. float markerLength, const Ptr<Dictionary> &dictionary);
  83. /**
  84. *
  85. */
  86. CV_WRAP Size getChessboardSize() const { return Size(_squaresX, _squaresY); }
  87. /**
  88. *
  89. */
  90. CV_WRAP float getSquareLength() const { return _squareLength; }
  91. /**
  92. *
  93. */
  94. CV_WRAP float getMarkerLength() const { return _markerLength; }
  95. private:
  96. void _getNearestMarkerCorners();
  97. // number of markers in X and Y directions
  98. int _squaresX, _squaresY;
  99. // size of chessboard squares side (normally in meters)
  100. float _squareLength;
  101. // marker side lenght (normally in meters)
  102. float _markerLength;
  103. };
  104. /**
  105. * @brief Interpolate position of ChArUco board corners
  106. * @param markerCorners vector of already detected markers corners. For each marker, its four
  107. * corners are provided, (e.g std::vector<std::vector<cv::Point2f> > ). For N detected markers, the
  108. * dimensions of this array should be Nx4. The order of the corners should be clockwise.
  109. * @param markerIds list of identifiers for each marker in corners
  110. * @param image input image necesary for corner refinement. Note that markers are not detected and
  111. * should be sent in corners and ids parameters.
  112. * @param board layout of ChArUco board.
  113. * @param charucoCorners interpolated chessboard corners
  114. * @param charucoIds interpolated chessboard corners identifiers
  115. * @param cameraMatrix optional 3x3 floating-point camera matrix
  116. * \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$
  117. * @param distCoeffs optional vector of distortion coefficients
  118. * \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 or 12 elements
  119. * @param minMarkers number of adjacent markers that must be detected to return a charuco corner
  120. *
  121. * This function receives the detected markers and returns the 2D position of the chessboard corners
  122. * from a ChArUco board using the detected Aruco markers. If camera parameters are provided,
  123. * the process is based in an approximated pose estimation, else it is based on local homography.
  124. * Only visible corners are returned. For each corner, its corresponding identifier is
  125. * also returned in charucoIds.
  126. * The function returns the number of interpolated corners.
  127. */
  128. CV_EXPORTS_W int interpolateCornersCharuco(InputArrayOfArrays markerCorners, InputArray markerIds,
  129. InputArray image, const Ptr<CharucoBoard> &board,
  130. OutputArray charucoCorners, OutputArray charucoIds,
  131. InputArray cameraMatrix = noArray(),
  132. InputArray distCoeffs = noArray(), int minMarkers = 2);
  133. /**
  134. * @brief Pose estimation for a ChArUco board given some of their corners
  135. * @param charucoCorners vector of detected charuco corners
  136. * @param charucoIds list of identifiers for each corner in charucoCorners
  137. * @param board layout of ChArUco board.
  138. * @param cameraMatrix input 3x3 floating-point camera matrix
  139. * \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$
  140. * @param distCoeffs vector of distortion coefficients
  141. * \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 or 12 elements
  142. * @param rvec Output vector (e.g. cv::Mat) corresponding to the rotation vector of the board
  143. * (see cv::Rodrigues).
  144. * @param tvec Output vector (e.g. cv::Mat) corresponding to the translation vector of the board.
  145. * @param useExtrinsicGuess defines whether initial guess for \b rvec and \b tvec will be used or not.
  146. *
  147. * This function estimates a Charuco board pose from some detected corners.
  148. * The function checks if the input corners are enough and valid to perform pose estimation.
  149. * If pose estimation is valid, returns true, else returns false.
  150. */
  151. CV_EXPORTS_W bool estimatePoseCharucoBoard(InputArray charucoCorners, InputArray charucoIds,
  152. const Ptr<CharucoBoard> &board, InputArray cameraMatrix,
  153. InputArray distCoeffs, OutputArray rvec, OutputArray tvec,
  154. bool useExtrinsicGuess = false);
  155. /**
  156. * @brief Draws a set of Charuco corners
  157. * @param image input/output image. It must have 1 or 3 channels. The number of channels is not
  158. * altered.
  159. * @param charucoCorners vector of detected charuco corners
  160. * @param charucoIds list of identifiers for each corner in charucoCorners
  161. * @param cornerColor color of the square surrounding each corner
  162. *
  163. * This function draws a set of detected Charuco corners. If identifiers vector is provided, it also
  164. * draws the id of each corner.
  165. */
  166. CV_EXPORTS_W void drawDetectedCornersCharuco(InputOutputArray image, InputArray charucoCorners,
  167. InputArray charucoIds = noArray(),
  168. Scalar cornerColor = Scalar(255, 0, 0));
  169. /**
  170. * @brief Calibrate a camera using Charuco corners
  171. *
  172. * @param charucoCorners vector of detected charuco corners per frame
  173. * @param charucoIds list of identifiers for each corner in charucoCorners per frame
  174. * @param board Marker Board layout
  175. * @param imageSize input image size
  176. * @param cameraMatrix Output 3x3 floating-point camera matrix
  177. * \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . If CV\_CALIB\_USE\_INTRINSIC\_GUESS
  178. * and/or CV_CALIB_FIX_ASPECT_RATIO are specified, some or all of fx, fy, cx, cy must be
  179. * initialized before calling the function.
  180. * @param distCoeffs Output vector of distortion coefficients
  181. * \f$(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6],[s_1, s_2, s_3, s_4]])\f$ of 4, 5, 8 or 12 elements
  182. * @param rvecs Output vector of rotation vectors (see Rodrigues ) estimated for each board view
  183. * (e.g. std::vector<cv::Mat>>). That is, each k-th rotation vector together with the corresponding
  184. * k-th translation vector (see the next output parameter description) brings the board pattern
  185. * from the model coordinate space (in which object points are specified) to the world coordinate
  186. * space, that is, a real position of the board pattern in the k-th pattern view (k=0.. *M* -1).
  187. * @param tvecs Output vector of translation vectors estimated for each pattern view.
  188. * @param stdDeviationsIntrinsics Output vector of standard deviations estimated for intrinsic parameters.
  189. * Order of deviations values:
  190. * \f$(f_x, f_y, c_x, c_y, k_1, k_2, p_1, p_2, k_3, k_4, k_5, k_6 , s_1, s_2, s_3,
  191. * s_4, \tau_x, \tau_y)\f$ If one of parameters is not estimated, it's deviation is equals to zero.
  192. * @param stdDeviationsExtrinsics Output vector of standard deviations estimated for extrinsic parameters.
  193. * Order of deviations values: \f$(R_1, T_1, \dotsc , R_M, T_M)\f$ where M is number of pattern views,
  194. * \f$R_i, T_i\f$ are concatenated 1x3 vectors.
  195. * @param perViewErrors Output vector of average re-projection errors estimated for each pattern view.
  196. * @param flags flags Different flags for the calibration process (see #calibrateCamera for details).
  197. * @param criteria Termination criteria for the iterative optimization algorithm.
  198. *
  199. * This function calibrates a camera using a set of corners of a Charuco Board. The function
  200. * receives a list of detected corners and its identifiers from several views of the Board.
  201. * The function returns the final re-projection error.
  202. */
  203. CV_EXPORTS_AS(calibrateCameraCharucoExtended) double calibrateCameraCharuco(
  204. InputArrayOfArrays charucoCorners, InputArrayOfArrays charucoIds, const Ptr<CharucoBoard> &board,
  205. Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
  206. OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
  207. OutputArray stdDeviationsIntrinsics, OutputArray stdDeviationsExtrinsics,
  208. OutputArray perViewErrors, int flags = 0,
  209. TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON));
  210. /** @brief It's the same function as #calibrateCameraCharuco but without calibration error estimation.
  211. */
  212. CV_EXPORTS_W double calibrateCameraCharuco(
  213. InputArrayOfArrays charucoCorners, InputArrayOfArrays charucoIds, const Ptr<CharucoBoard> &board,
  214. Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
  215. OutputArrayOfArrays rvecs = noArray(), OutputArrayOfArrays tvecs = noArray(), int flags = 0,
  216. TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON));
  217. /**
  218. * @brief Detect ChArUco Diamond markers
  219. *
  220. * @param image input image necessary for corner subpixel.
  221. * @param markerCorners list of detected marker corners from detectMarkers function.
  222. * @param markerIds list of marker ids in markerCorners.
  223. * @param squareMarkerLengthRate rate between square and marker length:
  224. * squareMarkerLengthRate = squareLength/markerLength. The real units are not necessary.
  225. * @param diamondCorners output list of detected diamond corners (4 corners per diamond). The order
  226. * is the same than in marker corners: top left, top right, bottom right and bottom left. Similar
  227. * format than the corners returned by detectMarkers (e.g std::vector<std::vector<cv::Point2f> > ).
  228. * @param diamondIds ids of the diamonds in diamondCorners. The id of each diamond is in fact of
  229. * type Vec4i, so each diamond has 4 ids, which are the ids of the aruco markers composing the
  230. * diamond.
  231. * @param cameraMatrix Optional camera calibration matrix.
  232. * @param distCoeffs Optional camera distortion coefficients.
  233. *
  234. * This function detects Diamond markers from the previous detected ArUco markers. The diamonds
  235. * are returned in the diamondCorners and diamondIds parameters. If camera calibration parameters
  236. * are provided, the diamond search is based on reprojection. If not, diamond search is based on
  237. * homography. Homography is faster than reprojection but can slightly reduce the detection rate.
  238. */
  239. CV_EXPORTS_W void detectCharucoDiamond(InputArray image, InputArrayOfArrays markerCorners,
  240. InputArray markerIds, float squareMarkerLengthRate,
  241. OutputArrayOfArrays diamondCorners, OutputArray diamondIds,
  242. InputArray cameraMatrix = noArray(),
  243. InputArray distCoeffs = noArray());
  244. /**
  245. * @brief Draw a set of detected ChArUco Diamond markers
  246. *
  247. * @param image input/output image. It must have 1 or 3 channels. The number of channels is not
  248. * altered.
  249. * @param diamondCorners positions of diamond corners in the same format returned by
  250. * detectCharucoDiamond(). (e.g std::vector<std::vector<cv::Point2f> > ). For N detected markers,
  251. * the dimensions of this array should be Nx4. The order of the corners should be clockwise.
  252. * @param diamondIds vector of identifiers for diamonds in diamondCorners, in the same format
  253. * returned by detectCharucoDiamond() (e.g. std::vector<Vec4i>).
  254. * Optional, if not provided, ids are not painted.
  255. * @param borderColor color of marker borders. Rest of colors (text color and first corner color)
  256. * are calculated based on this one.
  257. *
  258. * Given an array of detected diamonds, this functions draws them in the image. The marker borders
  259. * are painted and the markers identifiers if provided.
  260. * Useful for debugging purposes.
  261. */
  262. CV_EXPORTS_W void drawDetectedDiamonds(InputOutputArray image, InputArrayOfArrays diamondCorners,
  263. InputArray diamondIds = noArray(),
  264. Scalar borderColor = Scalar(0, 0, 255));
  265. /**
  266. * @brief Draw a ChArUco Diamond marker
  267. *
  268. * @param dictionary dictionary of markers indicating the type of markers.
  269. * @param ids list of 4 ids for each ArUco marker in the ChArUco marker.
  270. * @param squareLength size of the chessboard squares in pixels.
  271. * @param markerLength size of the markers in pixels.
  272. * @param img output image with the marker. The size of this image will be
  273. * 3*squareLength + 2*marginSize,.
  274. * @param marginSize minimum margins (in pixels) of the marker in the output image
  275. * @param borderBits width of the marker borders.
  276. *
  277. * This function return the image of a ChArUco marker, ready to be printed.
  278. */
  279. // TODO cannot be exported yet; conversion from/to Vec4i is not wrapped in core
  280. CV_EXPORTS void drawCharucoDiamond(const Ptr<Dictionary> &dictionary, Vec4i ids, int squareLength,
  281. int markerLength, OutputArray img, int marginSize = 0,
  282. int borderBits = 1);
  283. //! @}
  284. }
  285. }
  286. #endif