charuco_detector.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. #ifndef OPENCV_OBJDETECT_CHARUCO_DETECTOR_HPP
  5. #define OPENCV_OBJDETECT_CHARUCO_DETECTOR_HPP
  6. #include "opencv2/objdetect/aruco_detector.hpp"
  7. namespace cv {
  8. namespace aruco {
  9. //! @addtogroup objdetect_aruco
  10. //! @{
  11. struct CV_EXPORTS_W_SIMPLE CharucoParameters {
  12. CV_WRAP CharucoParameters() {
  13. minMarkers = 2;
  14. tryRefineMarkers = false;
  15. checkMarkers = true;
  16. }
  17. /// cameraMatrix optional 3x3 floating-point camera matrix
  18. CV_PROP_RW Mat cameraMatrix;
  19. /// distCoeffs optional vector of distortion coefficients
  20. CV_PROP_RW Mat distCoeffs;
  21. /// minMarkers number of adjacent markers that must be detected to return a charuco corner, default = 2
  22. CV_PROP_RW int minMarkers;
  23. /// try to use refine board, default false
  24. CV_PROP_RW bool tryRefineMarkers;
  25. /// run check to verify that markers belong to the same board, default true
  26. CV_PROP_RW bool checkMarkers;
  27. };
  28. class CV_EXPORTS_W CharucoDetector : public Algorithm {
  29. public:
  30. /** @brief Basic CharucoDetector constructor
  31. *
  32. * @param board ChAruco board
  33. * @param charucoParams charuco detection parameters
  34. * @param detectorParams marker detection parameters
  35. * @param refineParams marker refine detection parameters
  36. */
  37. CV_WRAP CharucoDetector(const CharucoBoard& board,
  38. const CharucoParameters& charucoParams = CharucoParameters(),
  39. const DetectorParameters &detectorParams = DetectorParameters(),
  40. const RefineParameters& refineParams = RefineParameters());
  41. CV_WRAP const CharucoBoard& getBoard() const;
  42. CV_WRAP void setBoard(const CharucoBoard& board);
  43. CV_WRAP const CharucoParameters& getCharucoParameters() const;
  44. CV_WRAP void setCharucoParameters(CharucoParameters& charucoParameters);
  45. CV_WRAP const DetectorParameters& getDetectorParameters() const;
  46. CV_WRAP void setDetectorParameters(const DetectorParameters& detectorParameters);
  47. CV_WRAP const RefineParameters& getRefineParameters() const;
  48. CV_WRAP void setRefineParameters(const RefineParameters& refineParameters);
  49. /**
  50. * @brief detect aruco markers and interpolate position of ChArUco board corners
  51. * @param image input image necesary for corner refinement. Note that markers are not detected and
  52. * should be sent in corners and ids parameters.
  53. * @param charucoCorners interpolated chessboard corners.
  54. * @param charucoIds interpolated chessboard corners identifiers.
  55. * @param markerCorners vector of already detected markers corners. For each marker, its four
  56. * corners are provided, (e.g std::vector<std::vector<cv::Point2f> > ). For N detected markers, the
  57. * dimensions of this array should be Nx4. The order of the corners should be clockwise.
  58. * If markerCorners and markerCorners are empty, the function detect aruco markers and ids.
  59. * @param markerIds list of identifiers for each marker in corners.
  60. * If markerCorners and markerCorners are empty, the function detect aruco markers and ids.
  61. *
  62. * This function receives the detected markers and returns the 2D position of the chessboard corners
  63. * from a ChArUco board using the detected Aruco markers.
  64. *
  65. * If markerCorners and markerCorners are empty, the detectMarkers() will run and detect aruco markers and ids.
  66. *
  67. * If camera parameters are provided, the process is based in an approximated pose estimation, else it is based on local homography.
  68. * Only visible corners are returned. For each corner, its corresponding identifier is also returned in charucoIds.
  69. * @sa findChessboardCorners
  70. * @note After OpenCV 4.6.0, there was an incompatible change in the ChArUco pattern generation algorithm for even row counts.
  71. * Use cv::aruco::CharucoBoard::setLegacyPattern() to ensure compatibility with patterns created using OpenCV versions prior to 4.6.0.
  72. * For more information, see the issue: https://github.com/opencv/opencv/issues/23152
  73. */
  74. CV_WRAP void detectBoard(InputArray image, OutputArray charucoCorners, OutputArray charucoIds,
  75. InputOutputArrayOfArrays markerCorners = noArray(),
  76. InputOutputArray markerIds = noArray()) const;
  77. /**
  78. * @brief Detect ChArUco Diamond markers
  79. *
  80. * @param image input image necessary for corner subpixel.
  81. * @param diamondCorners output list of detected diamond corners (4 corners per diamond). The order
  82. * is the same than in marker corners: top left, top right, bottom right and bottom left. Similar
  83. * format than the corners returned by detectMarkers (e.g std::vector<std::vector<cv::Point2f> > ).
  84. * @param diamondIds ids of the diamonds in diamondCorners. The id of each diamond is in fact of
  85. * type Vec4i, so each diamond has 4 ids, which are the ids of the aruco markers composing the
  86. * diamond.
  87. * @param markerCorners list of detected marker corners from detectMarkers function.
  88. * If markerCorners and markerCorners are empty, the function detect aruco markers and ids.
  89. * @param markerIds list of marker ids in markerCorners.
  90. * If markerCorners and markerCorners are empty, the function detect aruco markers and ids.
  91. *
  92. * This function detects Diamond markers from the previous detected ArUco markers. The diamonds
  93. * are returned in the diamondCorners and diamondIds parameters. If camera calibration parameters
  94. * are provided, the diamond search is based on reprojection. If not, diamond search is based on
  95. * homography. Homography is faster than reprojection, but less accurate.
  96. */
  97. CV_WRAP void detectDiamonds(InputArray image, OutputArrayOfArrays diamondCorners, OutputArray diamondIds,
  98. InputOutputArrayOfArrays markerCorners = noArray(),
  99. InputOutputArray markerIds = noArray()) const;
  100. protected:
  101. struct CharucoDetectorImpl;
  102. Ptr<CharucoDetectorImpl> charucoDetectorImpl;
  103. };
  104. /**
  105. * @brief Draws a set of Charuco corners
  106. * @param image input/output image. It must have 1 or 3 channels. The number of channels is not
  107. * altered.
  108. * @param charucoCorners vector of detected charuco corners
  109. * @param charucoIds list of identifiers for each corner in charucoCorners
  110. * @param cornerColor color of the square surrounding each corner
  111. *
  112. * This function draws a set of detected Charuco corners. If identifiers vector is provided, it also
  113. * draws the id of each corner.
  114. */
  115. CV_EXPORTS_W void drawDetectedCornersCharuco(InputOutputArray image, InputArray charucoCorners,
  116. InputArray charucoIds = noArray(), Scalar cornerColor = Scalar(255, 0, 0));
  117. /**
  118. * @brief Draw a set of detected ChArUco Diamond markers
  119. *
  120. * @param image input/output image. It must have 1 or 3 channels. The number of channels is not
  121. * altered.
  122. * @param diamondCorners positions of diamond corners in the same format returned by
  123. * detectCharucoDiamond(). (e.g std::vector<std::vector<cv::Point2f> > ). For N detected markers,
  124. * the dimensions of this array should be Nx4. The order of the corners should be clockwise.
  125. * @param diamondIds vector of identifiers for diamonds in diamondCorners, in the same format
  126. * returned by detectCharucoDiamond() (e.g. std::vector<Vec4i>).
  127. * Optional, if not provided, ids are not painted.
  128. * @param borderColor color of marker borders. Rest of colors (text color and first corner color)
  129. * are calculated based on this one.
  130. *
  131. * Given an array of detected diamonds, this functions draws them in the image. The marker borders
  132. * are painted and the markers identifiers if provided.
  133. * Useful for debugging purposes.
  134. */
  135. CV_EXPORTS_W void drawDetectedDiamonds(InputOutputArray image, InputArrayOfArrays diamondCorners,
  136. InputArray diamondIds = noArray(),
  137. Scalar borderColor = Scalar(0, 0, 255));
  138. //! @}
  139. }
  140. }
  141. #endif