charuco_detector.hpp 7.9 KB

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