tutorial_charuco_create_detect.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //! [charucohdr]
  2. #include <opencv2/aruco/charuco.hpp>
  3. //! [charucohdr]
  4. #include <opencv2/highgui.hpp>
  5. #include <iostream>
  6. #include <string>
  7. #include "aruco_samples_utility.hpp"
  8. namespace {
  9. const char* about = "A tutorial code on charuco board creation and detection of charuco board with and without camera caliberation";
  10. const char* keys = "{c | | Put value of c=1 to create charuco board;\nc=2 to detect charuco board without camera calibration;\nc=3 to detect charuco board with camera calibration and Pose Estimation}";
  11. }
  12. static inline void createBoard()
  13. {
  14. cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
  15. //! [createBoard]
  16. cv::Ptr<cv::aruco::CharucoBoard> board = cv::aruco::CharucoBoard::create(5, 7, 0.04f, 0.02f, dictionary);
  17. cv::Mat boardImage;
  18. board->draw(cv::Size(600, 500), boardImage, 10, 1);
  19. //! [createBoard]
  20. cv::imwrite("BoardImage.jpg", boardImage);
  21. }
  22. //! [detwcp]
  23. static inline void detectCharucoBoardWithCalibrationPose()
  24. {
  25. cv::VideoCapture inputVideo;
  26. inputVideo.open(0);
  27. //! [matdiscoff]
  28. cv::Mat cameraMatrix, distCoeffs;
  29. std::string filename = "calib.txt";
  30. bool readOk = readCameraParameters(filename, cameraMatrix, distCoeffs);
  31. //! [matdiscoff]
  32. if (!readOk) {
  33. std::cerr << "Invalid camera file" << std::endl;
  34. } else {
  35. //! [dictboard]
  36. cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
  37. cv::Ptr<cv::aruco::CharucoBoard> board = cv::aruco::CharucoBoard::create(5, 7, 0.04f, 0.02f, dictionary);
  38. cv::Ptr<cv::aruco::DetectorParameters> params = cv::aruco::DetectorParameters::create();
  39. //! [dictboard]
  40. while (inputVideo.grab()) {
  41. //! [inputImg]
  42. cv::Mat image;
  43. //! [inputImg]
  44. cv::Mat imageCopy;
  45. inputVideo.retrieve(image);
  46. image.copyTo(imageCopy);
  47. //! [midcornerdet]
  48. std::vector<int> markerIds;
  49. std::vector<std::vector<cv::Point2f> > markerCorners;
  50. cv::aruco::detectMarkers(image, board->dictionary, markerCorners, markerIds, params);
  51. //! [midcornerdet]
  52. // if at least one marker detected
  53. if (markerIds.size() > 0) {
  54. cv::aruco::drawDetectedMarkers(imageCopy, markerCorners, markerIds);
  55. //! [charidcor]
  56. std::vector<cv::Point2f> charucoCorners;
  57. std::vector<int> charucoIds;
  58. cv::aruco::interpolateCornersCharuco(markerCorners, markerIds, image, board, charucoCorners, charucoIds, cameraMatrix, distCoeffs);
  59. //! [charidcor]
  60. // if at least one charuco corner detected
  61. if (charucoIds.size() > 0) {
  62. cv::Scalar color = cv::Scalar(255, 0, 0);
  63. //! [detcor]
  64. cv::aruco::drawDetectedCornersCharuco(imageCopy, charucoCorners, charucoIds, color);
  65. //! [detcor]
  66. cv::Vec3d rvec, tvec;
  67. //! [pose]
  68. bool valid = cv::aruco::estimatePoseCharucoBoard(charucoCorners, charucoIds, board, cameraMatrix, distCoeffs, rvec, tvec);
  69. //! [pose]
  70. // if charuco pose is valid
  71. if (valid)
  72. cv::aruco::drawAxis(imageCopy, cameraMatrix, distCoeffs, rvec, tvec, 0.1f);
  73. }
  74. }
  75. cv::imshow("out", imageCopy);
  76. char key = (char)cv::waitKey(30);
  77. if (key == 27)
  78. break;
  79. }
  80. }
  81. }
  82. //! [detwcp]
  83. //! [detwc]
  84. static inline void detectCharucoBoardWithoutCalibration()
  85. {
  86. cv::VideoCapture inputVideo;
  87. inputVideo.open(0);
  88. cv::Ptr<cv::aruco::Dictionary> dictionary = cv::aruco::getPredefinedDictionary(cv::aruco::DICT_6X6_250);
  89. cv::Ptr<cv::aruco::CharucoBoard> board = cv::aruco::CharucoBoard::create(5, 7, 0.04f, 0.02f, dictionary);
  90. cv::Ptr<cv::aruco::DetectorParameters> params = cv::aruco::DetectorParameters::create();
  91. params->cornerRefinementMethod = cv::aruco::CORNER_REFINE_NONE;
  92. while (inputVideo.grab()) {
  93. cv::Mat image, imageCopy;
  94. inputVideo.retrieve(image);
  95. image.copyTo(imageCopy);
  96. std::vector<int> markerIds;
  97. std::vector<std::vector<cv::Point2f> > markerCorners;
  98. cv::aruco::detectMarkers(image, board->dictionary, markerCorners, markerIds, params);
  99. //or
  100. //cv::aruco::detectMarkers(image, dictionary, markerCorners, markerIds, params);
  101. // if at least one marker detected
  102. if (markerIds.size() > 0) {
  103. cv::aruco::drawDetectedMarkers(imageCopy, markerCorners, markerIds);
  104. //! [charidcorwc]
  105. std::vector<cv::Point2f> charucoCorners;
  106. std::vector<int> charucoIds;
  107. cv::aruco::interpolateCornersCharuco(markerCorners, markerIds, image, board, charucoCorners, charucoIds);
  108. //! [charidcorwc]
  109. // if at least one charuco corner detected
  110. if (charucoIds.size() > 0)
  111. cv::aruco::drawDetectedCornersCharuco(imageCopy, charucoCorners, charucoIds, cv::Scalar(255, 0, 0));
  112. }
  113. cv::imshow("out", imageCopy);
  114. char key = (char)cv::waitKey(30);
  115. if (key == 27)
  116. break;
  117. }
  118. }
  119. //! [detwc]
  120. int main(int argc, char* argv[])
  121. {
  122. cv::CommandLineParser parser(argc, argv, keys);
  123. parser.about(about);
  124. if (argc < 2) {
  125. parser.printMessage();
  126. return 0;
  127. }
  128. int choose = parser.get<int>("c");
  129. switch (choose) {
  130. case 1:
  131. createBoard();
  132. std::cout << "An image named BoardImg.jpg is generated in folder containing this file" << std::endl;
  133. break;
  134. case 2:
  135. detectCharucoBoardWithoutCalibration();
  136. break;
  137. case 3:
  138. detectCharucoBoardWithCalibrationPose();
  139. break;
  140. default:
  141. break;
  142. }
  143. return 0;
  144. }