aruco.hpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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_ARUCO_HPP__
  32. #define __OPENCV_ARUCO_HPP__
  33. #include <opencv2/core.hpp>
  34. #include <opencv2/calib3d.hpp>
  35. #include <vector>
  36. #include "opencv2/aruco/dictionary.hpp"
  37. /**
  38. * @defgroup aruco ArUco Marker Detection
  39. * This module is dedicated to square fiducial markers (also known as Augmented Reality Markers)
  40. * These markers are useful for easy, fast and robust camera pose estimation.ç
  41. *
  42. * The main functionalities are:
  43. * - Detection of markers in an image
  44. * - Pose estimation from a single marker or from a board/set of markers
  45. * - Detection of ChArUco board for high subpixel accuracy
  46. * - Camera calibration from both, ArUco boards and ChArUco boards.
  47. * - Detection of ChArUco diamond markers
  48. * The samples directory includes easy examples of how to use the module.
  49. *
  50. * The implementation is based on the ArUco Library by R. Muñoz-Salinas and S. Garrido-Jurado @cite Aruco2014.
  51. *
  52. * Markers can also be detected based on the AprilTag 2 @cite wang2016iros fiducial detection method.
  53. *
  54. * @sa S. Garrido-Jurado, R. Muñoz-Salinas, F. J. Madrid-Cuevas, and M. J. Marín-Jiménez. 2014.
  55. * "Automatic generation and detection of highly reliable fiducial markers under occlusion".
  56. * Pattern Recogn. 47, 6 (June 2014), 2280-2292. DOI=10.1016/j.patcog.2014.01.005
  57. *
  58. * @sa http://www.uco.es/investiga/grupos/ava/node/26
  59. *
  60. * This module has been originally developed by Sergio Garrido-Jurado as a project
  61. * for Google Summer of Code 2015 (GSoC 15).
  62. *
  63. *
  64. */
  65. namespace cv {
  66. namespace aruco {
  67. //! @addtogroup aruco
  68. //! @{
  69. enum CornerRefineMethod{
  70. CORNER_REFINE_NONE, ///< Tag and corners detection based on the ArUco approach
  71. CORNER_REFINE_SUBPIX, ///< ArUco approach and refine the corners locations using corner subpixel accuracy
  72. CORNER_REFINE_CONTOUR, ///< ArUco approach and refine the corners locations using the contour-points line fitting
  73. CORNER_REFINE_APRILTAG, ///< Tag and corners detection based on the AprilTag 2 approach @cite wang2016iros
  74. };
  75. /**
  76. * @brief Parameters for the detectMarker process:
  77. * - adaptiveThreshWinSizeMin: minimum window size for adaptive thresholding before finding
  78. * contours (default 3).
  79. * - adaptiveThreshWinSizeMax: maximum window size for adaptive thresholding before finding
  80. * contours (default 23).
  81. * - adaptiveThreshWinSizeStep: increments from adaptiveThreshWinSizeMin to adaptiveThreshWinSizeMax
  82. * during the thresholding (default 10).
  83. * - adaptiveThreshConstant: constant for adaptive thresholding before finding contours (default 7)
  84. * - minMarkerPerimeterRate: determine minimum perimeter for marker contour to be detected. This
  85. * is defined as a rate respect to the maximum dimension of the input image (default 0.03).
  86. * - maxMarkerPerimeterRate: determine maximum perimeter for marker contour to be detected. This
  87. * is defined as a rate respect to the maximum dimension of the input image (default 4.0).
  88. * - polygonalApproxAccuracyRate: minimum accuracy during the polygonal approximation process to
  89. * determine which contours are squares. (default 0.03)
  90. * - minCornerDistanceRate: minimum distance between corners for detected markers relative to its
  91. * perimeter (default 0.05)
  92. * - minDistanceToBorder: minimum distance of any corner to the image border for detected markers
  93. * (in pixels) (default 3)
  94. * - minMarkerDistanceRate: minimum mean distance beetween two marker corners to be considered
  95. * similar, so that the smaller one is removed. The rate is relative to the smaller perimeter
  96. * of the two markers (default 0.05).
  97. * - cornerRefinementMethod: corner refinement method. (CORNER_REFINE_NONE, no refinement.
  98. * CORNER_REFINE_SUBPIX, do subpixel refinement. CORNER_REFINE_CONTOUR use contour-Points,
  99. * CORNER_REFINE_APRILTAG use the AprilTag2 approach). (default CORNER_REFINE_NONE)
  100. * - cornerRefinementWinSize: window size for the corner refinement process (in pixels) (default 5).
  101. * - cornerRefinementMaxIterations: maximum number of iterations for stop criteria of the corner
  102. * refinement process (default 30).
  103. * - cornerRefinementMinAccuracy: minimum error for the stop cristeria of the corner refinement
  104. * process (default: 0.1)
  105. * - markerBorderBits: number of bits of the marker border, i.e. marker border width (default 1).
  106. * - perspectiveRemovePixelPerCell: number of bits (per dimension) for each cell of the marker
  107. * when removing the perspective (default 4).
  108. * - perspectiveRemoveIgnoredMarginPerCell: width of the margin of pixels on each cell not
  109. * considered for the determination of the cell bit. Represents the rate respect to the total
  110. * size of the cell, i.e. perspectiveRemovePixelPerCell (default 0.13)
  111. * - maxErroneousBitsInBorderRate: maximum number of accepted erroneous bits in the border (i.e.
  112. * number of allowed white bits in the border). Represented as a rate respect to the total
  113. * number of bits per marker (default 0.35).
  114. * - minOtsuStdDev: minimun standard deviation in pixels values during the decodification step to
  115. * apply Otsu thresholding (otherwise, all the bits are set to 0 or 1 depending on mean higher
  116. * than 128 or not) (default 5.0)
  117. * - errorCorrectionRate error correction rate respect to the maximun error correction capability
  118. * for each dictionary. (default 0.6).
  119. * - aprilTagMinClusterPixels: reject quads containing too few pixels. (default 5)
  120. * - aprilTagMaxNmaxima: how many corner candidates to consider when segmenting a group of pixels into a quad. (default 10)
  121. * - aprilTagCriticalRad: Reject quads where pairs of edges have angles that are close to straight or close to
  122. * 180 degrees. Zero means that no quads are rejected. (In radians) (default 10*PI/180)
  123. * - aprilTagMaxLineFitMse: When fitting lines to the contours, what is the maximum mean squared error
  124. * allowed? This is useful in rejecting contours that are far from being quad shaped; rejecting
  125. * these quads "early" saves expensive decoding processing. (default 10.0)
  126. * - aprilTagMinWhiteBlackDiff: When we build our model of black & white pixels, we add an extra check that
  127. * the white model must be (overall) brighter than the black model. How much brighter? (in pixel values, [0,255]). (default 5)
  128. * - aprilTagDeglitch: should the thresholded image be deglitched? Only useful for very noisy images. (default 0)
  129. * - aprilTagQuadDecimate: Detection of quads can be done on a lower-resolution image, improving speed at a
  130. * cost of pose accuracy and a slight decrease in detection rate. Decoding the binary payload is still
  131. * done at full resolution. (default 0.0)
  132. * - aprilTagQuadSigma: What Gaussian blur should be applied to the segmented image (used for quad detection?)
  133. * Parameter is the standard deviation in pixels. Very noisy images benefit from non-zero values (e.g. 0.8). (default 0.0)
  134. * - detectInvertedMarker: to check if there is a white marker. In order to generate a "white" marker just
  135. * invert a normal marker by using a tilde, ~markerImage. (default false)
  136. */
  137. struct CV_EXPORTS_W DetectorParameters {
  138. DetectorParameters();
  139. CV_WRAP static Ptr<DetectorParameters> create();
  140. CV_WRAP bool readDetectorParameters(const FileNode& fn);
  141. CV_PROP_RW int adaptiveThreshWinSizeMin;
  142. CV_PROP_RW int adaptiveThreshWinSizeMax;
  143. CV_PROP_RW int adaptiveThreshWinSizeStep;
  144. CV_PROP_RW double adaptiveThreshConstant;
  145. CV_PROP_RW double minMarkerPerimeterRate;
  146. CV_PROP_RW double maxMarkerPerimeterRate;
  147. CV_PROP_RW double polygonalApproxAccuracyRate;
  148. CV_PROP_RW double minCornerDistanceRate;
  149. CV_PROP_RW int minDistanceToBorder;
  150. CV_PROP_RW double minMarkerDistanceRate;
  151. CV_PROP_RW int cornerRefinementMethod;
  152. CV_PROP_RW int cornerRefinementWinSize;
  153. CV_PROP_RW int cornerRefinementMaxIterations;
  154. CV_PROP_RW double cornerRefinementMinAccuracy;
  155. CV_PROP_RW int markerBorderBits;
  156. CV_PROP_RW int perspectiveRemovePixelPerCell;
  157. CV_PROP_RW double perspectiveRemoveIgnoredMarginPerCell;
  158. CV_PROP_RW double maxErroneousBitsInBorderRate;
  159. CV_PROP_RW double minOtsuStdDev;
  160. CV_PROP_RW double errorCorrectionRate;
  161. // April :: User-configurable parameters.
  162. CV_PROP_RW float aprilTagQuadDecimate;
  163. CV_PROP_RW float aprilTagQuadSigma;
  164. // April :: Internal variables
  165. CV_PROP_RW int aprilTagMinClusterPixels;
  166. CV_PROP_RW int aprilTagMaxNmaxima;
  167. CV_PROP_RW float aprilTagCriticalRad;
  168. CV_PROP_RW float aprilTagMaxLineFitMse;
  169. CV_PROP_RW int aprilTagMinWhiteBlackDiff;
  170. CV_PROP_RW int aprilTagDeglitch;
  171. // to detect white (inverted) markers
  172. CV_PROP_RW bool detectInvertedMarker;
  173. };
  174. /**
  175. * @brief Basic marker detection
  176. *
  177. * @param image input image
  178. * @param dictionary indicates the type of markers that will be searched
  179. * @param corners vector of detected marker corners. For each marker, its four corners
  180. * are provided, (e.g std::vector<std::vector<cv::Point2f> > ). For N detected markers,
  181. * the dimensions of this array is Nx4. The order of the corners is clockwise.
  182. * @param ids vector of identifiers of the detected markers. The identifier is of type int
  183. * (e.g. std::vector<int>). For N detected markers, the size of ids is also N.
  184. * The identifiers have the same order than the markers in the imgPoints array.
  185. * @param parameters marker detection parameters
  186. * @param rejectedImgPoints contains the imgPoints of those squares whose inner code has not a
  187. * correct codification. Useful for debugging purposes.
  188. *
  189. * Performs marker detection in the input image. Only markers included in the specific dictionary
  190. * are searched. For each detected marker, it returns the 2D position of its corner in the image
  191. * and its corresponding identifier.
  192. * Note that this function does not perform pose estimation.
  193. * @note The function does not correct lens distortion or takes it into account. It's recommended to undistort
  194. * input image with corresponging camera model, if camera parameters are known
  195. * @sa undistort, estimatePoseSingleMarkers, estimatePoseBoard
  196. *
  197. */
  198. CV_EXPORTS_W void detectMarkers(InputArray image, const Ptr<Dictionary> &dictionary, OutputArrayOfArrays corners,
  199. OutputArray ids, const Ptr<DetectorParameters> &parameters = DetectorParameters::create(),
  200. OutputArrayOfArrays rejectedImgPoints = noArray());
  201. /** @brief
  202. * rvec/tvec define the right handed coordinate system of the marker.
  203. * PatternPos defines center this system and axes direction.
  204. * Axis X (red color) - first coordinate, axis Y (green color) - second coordinate,
  205. * axis Z (blue color) - third coordinate.
  206. * @sa estimatePoseSingleMarkers(), @ref tutorial_aruco_detection
  207. */
  208. enum PatternPos {
  209. /** @brief The marker coordinate system is centered on the middle of the marker.
  210. * The coordinates of the four corners (CCW order) of the marker in its own coordinate system are:
  211. * (-markerLength/2, markerLength/2, 0), (markerLength/2, markerLength/2, 0),
  212. * (markerLength/2, -markerLength/2, 0), (-markerLength/2, -markerLength/2, 0).
  213. *
  214. * These pattern points define this coordinate system:
  215. * ![Image with axes drawn](images/singlemarkersaxes.jpg)
  216. */
  217. CCW_center,
  218. /** @brief The marker coordinate system is centered on the top-left corner of the marker.
  219. * The coordinates of the four corners (CW order) of the marker in its own coordinate system are:
  220. * (0, 0, 0), (markerLength, 0, 0),
  221. * (markerLength, markerLength, 0), (0, markerLength, 0).
  222. *
  223. * These pattern points define this coordinate system:
  224. * ![Image with axes drawn](images/singlemarkersaxes2.jpg)
  225. */
  226. CW_top_left_corner
  227. };
  228. /** @brief
  229. * Pose estimation parameters
  230. * @param pattern Defines center this system and axes direction (default PatternPos::CCW_center).
  231. * @param useExtrinsicGuess Parameter used for SOLVEPNP_ITERATIVE. If true (1), the function uses the provided
  232. * rvec and tvec values as initial approximations of the rotation and translation vectors, respectively, and further
  233. * optimizes them (default false).
  234. * @param solvePnPMethod Method for solving a PnP problem: see @ref calib3d_solvePnP_flags (default SOLVEPNP_ITERATIVE).
  235. * @sa PatternPos, solvePnP(), @ref tutorial_aruco_detection
  236. */
  237. struct CV_EXPORTS_W EstimateParameters {
  238. CV_PROP_RW PatternPos pattern;
  239. CV_PROP_RW bool useExtrinsicGuess;
  240. CV_PROP_RW SolvePnPMethod solvePnPMethod;
  241. EstimateParameters(): pattern(CCW_center), useExtrinsicGuess(false),
  242. solvePnPMethod(SOLVEPNP_ITERATIVE) {}
  243. CV_WRAP static Ptr<EstimateParameters> create() {
  244. return makePtr<EstimateParameters>();
  245. }
  246. };
  247. /**
  248. * @brief Pose estimation for single markers
  249. *
  250. * @param corners vector of already detected markers corners. For each marker, its four corners
  251. * are provided, (e.g std::vector<std::vector<cv::Point2f> > ). For N detected markers,
  252. * the dimensions of this array should be Nx4. The order of the corners should be clockwise.
  253. * @sa detectMarkers
  254. * @param markerLength the length of the markers' side. The returning translation vectors will
  255. * be in the same unit. Normally, unit is meters.
  256. * @param cameraMatrix input 3x3 floating-point camera matrix
  257. * \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$
  258. * @param distCoeffs vector of distortion coefficients
  259. * \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
  260. * @param rvecs array of output rotation vectors (@sa Rodrigues) (e.g. std::vector<cv::Vec3d>).
  261. * Each element in rvecs corresponds to the specific marker in imgPoints.
  262. * @param tvecs array of output translation vectors (e.g. std::vector<cv::Vec3d>).
  263. * Each element in tvecs corresponds to the specific marker in imgPoints.
  264. * @param _objPoints array of object points of all the marker corners
  265. * @param estimateParameters set the origin of coordinate system and the coordinates of the four corners of the marker
  266. * (default estimateParameters.pattern = PatternPos::CCW_center, estimateParameters.useExtrinsicGuess = false,
  267. * estimateParameters.solvePnPMethod = SOLVEPNP_ITERATIVE).
  268. *
  269. * This function receives the detected markers and returns their pose estimation respect to
  270. * the camera individually. So for each marker, one rotation and translation vector is returned.
  271. * The returned transformation is the one that transforms points from each marker coordinate system
  272. * to the camera coordinate system.
  273. * The marker coordinate system is centered on the middle (by default) or on the top-left corner of the marker,
  274. * with the Z axis perpendicular to the marker plane.
  275. * estimateParameters defines the coordinates of the four corners of the marker in its own coordinate system (by default) are:
  276. * (-markerLength/2, markerLength/2, 0), (markerLength/2, markerLength/2, 0),
  277. * (markerLength/2, -markerLength/2, 0), (-markerLength/2, -markerLength/2, 0)
  278. * @sa use cv::drawFrameAxes to get world coordinate system axis for object points
  279. * @sa @ref tutorial_aruco_detection
  280. * @sa EstimateParameters
  281. * @sa PatternPos
  282. */
  283. CV_EXPORTS_W void estimatePoseSingleMarkers(InputArrayOfArrays corners, float markerLength,
  284. InputArray cameraMatrix, InputArray distCoeffs,
  285. OutputArray rvecs, OutputArray tvecs, OutputArray _objPoints = noArray(),
  286. Ptr<EstimateParameters> estimateParameters = EstimateParameters::create());
  287. /**
  288. * @brief Board of markers
  289. *
  290. * A board is a set of markers in the 3D space with a common coordinate system.
  291. * The common form of a board of marker is a planar (2D) board, however any 3D layout can be used.
  292. * A Board object is composed by:
  293. * - The object points of the marker corners, i.e. their coordinates respect to the board system.
  294. * - The dictionary which indicates the type of markers of the board
  295. * - The identifier of all the markers in the board.
  296. */
  297. class CV_EXPORTS_W Board {
  298. public:
  299. /**
  300. * @brief Provide way to create Board by passing necessary data. Specially needed in Python.
  301. *
  302. * @param objPoints array of object points of all the marker corners in the board
  303. * @param dictionary the dictionary of markers employed for this board
  304. * @param ids vector of the identifiers of the markers in the board
  305. *
  306. */
  307. CV_WRAP static Ptr<Board> create(InputArrayOfArrays objPoints, const Ptr<Dictionary> &dictionary, InputArray ids);
  308. /**
  309. * @brief Set ids vector
  310. *
  311. * @param ids vector of the identifiers of the markers in the board (should be the same size
  312. * as objPoints)
  313. *
  314. * Recommended way to set ids vector, which will fail if the size of ids does not match size
  315. * of objPoints.
  316. */
  317. CV_WRAP void setIds(InputArray ids);
  318. /// array of object points of all the marker corners in the board
  319. /// each marker include its 4 corners in this order:
  320. ///- objPoints[i][0] - left-top point of i-th marker
  321. ///- objPoints[i][1] - right-top point of i-th marker
  322. ///- objPoints[i][2] - right-bottom point of i-th marker
  323. ///- objPoints[i][3] - left-bottom point of i-th marker
  324. ///
  325. /// Markers are placed in a certain order - row by row, left to right in every row.
  326. /// For M markers, the size is Mx4.
  327. CV_PROP std::vector< std::vector< Point3f > > objPoints;
  328. /// the dictionary of markers employed for this board
  329. CV_PROP Ptr<Dictionary> dictionary;
  330. /// vector of the identifiers of the markers in the board (same size than objPoints)
  331. /// The identifiers refers to the board dictionary
  332. CV_PROP_RW std::vector< int > ids;
  333. /// coordinate of the bottom right corner of the board, is set when calling the function create()
  334. CV_PROP Point3f rightBottomBorder;
  335. };
  336. /**
  337. * @brief Planar board with grid arrangement of markers
  338. * More common type of board. All markers are placed in the same plane in a grid arrangement.
  339. * The board can be drawn using drawPlanarBoard() function (@sa drawPlanarBoard)
  340. */
  341. class CV_EXPORTS_W GridBoard : public Board {
  342. public:
  343. /**
  344. * @brief Draw a GridBoard
  345. *
  346. * @param outSize size of the output image in pixels.
  347. * @param img output image with the board. The size of this image will be outSize
  348. * and the board will be on the center, keeping the board proportions.
  349. * @param marginSize minimum margins (in pixels) of the board in the output image
  350. * @param borderBits width of the marker borders.
  351. *
  352. * This function return the image of the GridBoard, ready to be printed.
  353. */
  354. CV_WRAP void draw(Size outSize, OutputArray img, int marginSize = 0, int borderBits = 1);
  355. /**
  356. * @brief Create a GridBoard object
  357. *
  358. * @param markersX number of markers in X direction
  359. * @param markersY number of markers in Y direction
  360. * @param markerLength marker side length (normally in meters)
  361. * @param markerSeparation separation between two markers (same unit as markerLength)
  362. * @param dictionary dictionary of markers indicating the type of markers
  363. * @param firstMarker id of first marker in dictionary to use on board.
  364. * @return the output GridBoard object
  365. *
  366. * This functions creates a GridBoard object given the number of markers in each direction and
  367. * the marker size and marker separation.
  368. */
  369. CV_WRAP static Ptr<GridBoard> create(int markersX, int markersY, float markerLength,
  370. float markerSeparation, const Ptr<Dictionary> &dictionary, int firstMarker = 0);
  371. /**
  372. *
  373. */
  374. CV_WRAP Size getGridSize() const { return Size(_markersX, _markersY); }
  375. /**
  376. *
  377. */
  378. CV_WRAP float getMarkerLength() const { return _markerLength; }
  379. /**
  380. *
  381. */
  382. CV_WRAP float getMarkerSeparation() const { return _markerSeparation; }
  383. private:
  384. // number of markers in X and Y directions
  385. int _markersX, _markersY;
  386. // marker side length (normally in meters)
  387. float _markerLength;
  388. // separation between markers in the grid
  389. float _markerSeparation;
  390. };
  391. /**
  392. * @brief Pose estimation for a board of markers
  393. *
  394. * @param corners vector of already detected markers corners. For each marker, its four corners
  395. * are provided, (e.g std::vector<std::vector<cv::Point2f> > ). For N detected markers, the
  396. * dimensions of this array should be Nx4. The order of the corners should be clockwise.
  397. * @param ids list of identifiers for each marker in corners
  398. * @param board layout of markers in the board. The layout is composed by the marker identifiers
  399. * and the positions of each marker corner in the board reference system.
  400. * @param cameraMatrix input 3x3 floating-point camera matrix
  401. * \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$
  402. * @param distCoeffs vector of distortion coefficients
  403. * \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
  404. * @param rvec Output vector (e.g. cv::Mat) corresponding to the rotation vector of the board
  405. * (see cv::Rodrigues). Used as initial guess if not empty.
  406. * @param tvec Output vector (e.g. cv::Mat) corresponding to the translation vector of the board.
  407. * @param useExtrinsicGuess defines whether initial guess for \b rvec and \b tvec will be used or not.
  408. * Used as initial guess if not empty.
  409. *
  410. * This function receives the detected markers and returns the pose of a marker board composed
  411. * by those markers.
  412. * A Board of marker has a single world coordinate system which is defined by the board layout.
  413. * The returned transformation is the one that transforms points from the board coordinate system
  414. * to the camera coordinate system.
  415. * Input markers that are not included in the board layout are ignored.
  416. * The function returns the number of markers from the input employed for the board pose estimation.
  417. * Note that returning a 0 means the pose has not been estimated.
  418. * @sa use cv::drawFrameAxes to get world coordinate system axis for object points
  419. */
  420. CV_EXPORTS_W int estimatePoseBoard(InputArrayOfArrays corners, InputArray ids, const Ptr<Board> &board,
  421. InputArray cameraMatrix, InputArray distCoeffs, OutputArray rvec,
  422. OutputArray tvec, bool useExtrinsicGuess = false);
  423. /**
  424. * @brief Refind not detected markers based on the already detected and the board layout
  425. *
  426. * @param image input image
  427. * @param board layout of markers in the board.
  428. * @param detectedCorners vector of already detected marker corners.
  429. * @param detectedIds vector of already detected marker identifiers.
  430. * @param rejectedCorners vector of rejected candidates during the marker detection process.
  431. * @param cameraMatrix optional input 3x3 floating-point camera matrix
  432. * \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$
  433. * @param distCoeffs optional vector of distortion coefficients
  434. * \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
  435. * @param minRepDistance minimum distance between the corners of the rejected candidate and the
  436. * reprojected marker in order to consider it as a correspondence.
  437. * @param errorCorrectionRate rate of allowed erroneous bits respect to the error correction
  438. * capability of the used dictionary. -1 ignores the error correction step.
  439. * @param checkAllOrders Consider the four posible corner orders in the rejectedCorners array.
  440. * If it set to false, only the provided corner order is considered (default true).
  441. * @param recoveredIdxs Optional array to returns the indexes of the recovered candidates in the
  442. * original rejectedCorners array.
  443. * @param parameters marker detection parameters
  444. *
  445. * This function tries to find markers that were not detected in the basic detecMarkers function.
  446. * First, based on the current detected marker and the board layout, the function interpolates
  447. * the position of the missing markers. Then it tries to find correspondence between the reprojected
  448. * markers and the rejected candidates based on the minRepDistance and errorCorrectionRate
  449. * parameters.
  450. * If camera parameters and distortion coefficients are provided, missing markers are reprojected
  451. * using projectPoint function. If not, missing marker projections are interpolated using global
  452. * homography, and all the marker corners in the board must have the same Z coordinate.
  453. */
  454. CV_EXPORTS_W void refineDetectedMarkers(
  455. InputArray image,const Ptr<Board> &board, InputOutputArrayOfArrays detectedCorners,
  456. InputOutputArray detectedIds, InputOutputArrayOfArrays rejectedCorners,
  457. InputArray cameraMatrix = noArray(), InputArray distCoeffs = noArray(),
  458. float minRepDistance = 10.f, float errorCorrectionRate = 3.f, bool checkAllOrders = true,
  459. OutputArray recoveredIdxs = noArray(), const Ptr<DetectorParameters> &parameters = DetectorParameters::create());
  460. /**
  461. * @brief Draw detected markers in image
  462. *
  463. * @param image input/output image. It must have 1 or 3 channels. The number of channels is not
  464. * altered.
  465. * @param corners positions of marker corners on input image.
  466. * (e.g std::vector<std::vector<cv::Point2f> > ). For N detected markers, the dimensions of
  467. * this array should be Nx4. The order of the corners should be clockwise.
  468. * @param ids vector of identifiers for markers in markersCorners .
  469. * Optional, if not provided, ids are not painted.
  470. * @param borderColor color of marker borders. Rest of colors (text color and first corner color)
  471. * are calculated based on this one to improve visualization.
  472. *
  473. * Given an array of detected marker corners and its corresponding ids, this functions draws
  474. * the markers in the image. The marker borders are painted and the markers identifiers if provided.
  475. * Useful for debugging purposes.
  476. *
  477. */
  478. CV_EXPORTS_W void drawDetectedMarkers(InputOutputArray image, InputArrayOfArrays corners,
  479. InputArray ids = noArray(),
  480. Scalar borderColor = Scalar(0, 255, 0));
  481. /**
  482. * @brief Draw a canonical marker image
  483. *
  484. * @param dictionary dictionary of markers indicating the type of markers
  485. * @param id identifier of the marker that will be returned. It has to be a valid id
  486. * in the specified dictionary.
  487. * @param sidePixels size of the image in pixels
  488. * @param img output image with the marker
  489. * @param borderBits width of the marker border.
  490. *
  491. * This function returns a marker image in its canonical form (i.e. ready to be printed)
  492. */
  493. CV_EXPORTS_W void drawMarker(const Ptr<Dictionary> &dictionary, int id, int sidePixels, OutputArray img,
  494. int borderBits = 1);
  495. /**
  496. * @brief Draw a planar board
  497. * @sa _drawPlanarBoardImpl
  498. *
  499. * @param board layout of the board that will be drawn. The board should be planar,
  500. * z coordinate is ignored
  501. * @param outSize size of the output image in pixels.
  502. * @param img output image with the board. The size of this image will be outSize
  503. * and the board will be on the center, keeping the board proportions.
  504. * @param marginSize minimum margins (in pixels) of the board in the output image
  505. * @param borderBits width of the marker borders.
  506. *
  507. * This function return the image of a planar board, ready to be printed. It assumes
  508. * the Board layout specified is planar by ignoring the z coordinates of the object points.
  509. */
  510. CV_EXPORTS_W void drawPlanarBoard(const Ptr<Board> &board, Size outSize, OutputArray img,
  511. int marginSize = 0, int borderBits = 1);
  512. /**
  513. * @brief Implementation of drawPlanarBoard that accepts a raw Board pointer.
  514. */
  515. void _drawPlanarBoardImpl(Board *board, Size outSize, OutputArray img,
  516. int marginSize = 0, int borderBits = 1);
  517. /**
  518. * @brief Calibrate a camera using aruco markers
  519. *
  520. * @param corners vector of detected marker corners in all frames.
  521. * The corners should have the same format returned by detectMarkers (see #detectMarkers).
  522. * @param ids list of identifiers for each marker in corners
  523. * @param counter number of markers in each frame so that corners and ids can be split
  524. * @param board Marker Board layout
  525. * @param imageSize Size of the image used only to initialize the intrinsic camera matrix.
  526. * @param cameraMatrix Output 3x3 floating-point camera matrix
  527. * \f$A = \vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ . If CV\_CALIB\_USE\_INTRINSIC\_GUESS
  528. * and/or CV_CALIB_FIX_ASPECT_RATIO are specified, some or all of fx, fy, cx, cy must be
  529. * initialized before calling the function.
  530. * @param distCoeffs Output vector of distortion coefficients
  531. * \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
  532. * @param rvecs Output vector of rotation vectors (see Rodrigues ) estimated for each board view
  533. * (e.g. std::vector<cv::Mat>>). That is, each k-th rotation vector together with the corresponding
  534. * k-th translation vector (see the next output parameter description) brings the board pattern
  535. * from the model coordinate space (in which object points are specified) to the world coordinate
  536. * space, that is, a real position of the board pattern in the k-th pattern view (k=0.. *M* -1).
  537. * @param tvecs Output vector of translation vectors estimated for each pattern view.
  538. * @param stdDeviationsIntrinsics Output vector of standard deviations estimated for intrinsic parameters.
  539. * Order of deviations values:
  540. * \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,
  541. * s_4, \tau_x, \tau_y)\f$ If one of parameters is not estimated, it's deviation is equals to zero.
  542. * @param stdDeviationsExtrinsics Output vector of standard deviations estimated for extrinsic parameters.
  543. * Order of deviations values: \f$(R_1, T_1, \dotsc , R_M, T_M)\f$ where M is number of pattern views,
  544. * \f$R_i, T_i\f$ are concatenated 1x3 vectors.
  545. * @param perViewErrors Output vector of average re-projection errors estimated for each pattern view.
  546. * @param flags flags Different flags for the calibration process (see #calibrateCamera for details).
  547. * @param criteria Termination criteria for the iterative optimization algorithm.
  548. *
  549. * This function calibrates a camera using an Aruco Board. The function receives a list of
  550. * detected markers from several views of the Board. The process is similar to the chessboard
  551. * calibration in calibrateCamera(). The function returns the final re-projection error.
  552. */
  553. CV_EXPORTS_AS(calibrateCameraArucoExtended) double calibrateCameraAruco(
  554. InputArrayOfArrays corners, InputArray ids, InputArray counter, const Ptr<Board> &board,
  555. Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
  556. OutputArrayOfArrays rvecs, OutputArrayOfArrays tvecs,
  557. OutputArray stdDeviationsIntrinsics, OutputArray stdDeviationsExtrinsics,
  558. OutputArray perViewErrors, int flags = 0,
  559. TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON));
  560. /** @brief It's the same function as #calibrateCameraAruco but without calibration error estimation.
  561. */
  562. CV_EXPORTS_W double calibrateCameraAruco(
  563. InputArrayOfArrays corners, InputArray ids, InputArray counter, const Ptr<Board> &board,
  564. Size imageSize, InputOutputArray cameraMatrix, InputOutputArray distCoeffs,
  565. OutputArrayOfArrays rvecs = noArray(), OutputArrayOfArrays tvecs = noArray(), int flags = 0,
  566. TermCriteria criteria = TermCriteria(TermCriteria::COUNT + TermCriteria::EPS, 30, DBL_EPSILON));
  567. /**
  568. * @brief Given a board configuration and a set of detected markers, returns the corresponding
  569. * image points and object points to call solvePnP
  570. *
  571. * @param board Marker board layout.
  572. * @param detectedCorners List of detected marker corners of the board.
  573. * @param detectedIds List of identifiers for each marker.
  574. * @param objPoints Vector of vectors of board marker points in the board coordinate space.
  575. * @param imgPoints Vector of vectors of the projections of board marker corner points.
  576. */
  577. CV_EXPORTS_W void getBoardObjectAndImagePoints(const Ptr<Board> &board, InputArrayOfArrays detectedCorners,
  578. InputArray detectedIds, OutputArray objPoints, OutputArray imgPoints);
  579. //! @}
  580. }
  581. }
  582. #endif