detect_board_charuco.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. #include <opencv2/highgui.hpp>
  32. #include <opencv2/aruco/charuco.hpp>
  33. #include <vector>
  34. #include <iostream>
  35. #include "aruco_samples_utility.hpp"
  36. using namespace std;
  37. using namespace cv;
  38. namespace {
  39. const char* about = "Pose estimation using a ChArUco board";
  40. const char* keys =
  41. "{w | | Number of squares in X direction }"
  42. "{h | | Number of squares in Y direction }"
  43. "{sl | | Square side length (in meters) }"
  44. "{ml | | Marker side length (in meters) }"
  45. "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
  46. "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
  47. "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
  48. "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16}"
  49. "{cd | | Input file with custom dictionary }"
  50. "{c | | Output file with calibrated camera parameters }"
  51. "{v | | Input from video or image file, if ommited, input comes from camera }"
  52. "{ci | 0 | Camera id if input doesnt come from video (-v) }"
  53. "{dp | | File of marker detector parameters }"
  54. "{rs | | Apply refind strategy }"
  55. "{r | | show rejected candidates too }";
  56. }
  57. int main(int argc, char *argv[]) {
  58. CommandLineParser parser(argc, argv, keys);
  59. parser.about(about);
  60. if(argc < 6) {
  61. parser.printMessage();
  62. return 0;
  63. }
  64. int squaresX = parser.get<int>("w");
  65. int squaresY = parser.get<int>("h");
  66. float squareLength = parser.get<float>("sl");
  67. float markerLength = parser.get<float>("ml");
  68. bool showRejected = parser.has("r");
  69. bool refindStrategy = parser.has("rs");
  70. int camId = parser.get<int>("ci");
  71. String video;
  72. if(parser.has("v")) {
  73. video = parser.get<String>("v");
  74. }
  75. Mat camMatrix, distCoeffs;
  76. if(parser.has("c")) {
  77. bool readOk = readCameraParameters(parser.get<string>("c"), camMatrix, distCoeffs);
  78. if(!readOk) {
  79. cerr << "Invalid camera file" << endl;
  80. return 0;
  81. }
  82. }
  83. Ptr<aruco::DetectorParameters> detectorParams;
  84. if(parser.has("dp")) {
  85. FileStorage fs(parser.get<string>("dp"), FileStorage::READ);
  86. bool readOk = aruco::DetectorParameters::readDetectorParameters(fs.root(), detectorParams);
  87. if(!readOk) {
  88. cerr << "Invalid detector parameters file" << endl;
  89. return 0;
  90. }
  91. }
  92. if(!parser.check()) {
  93. parser.printErrors();
  94. return 0;
  95. }
  96. Ptr<aruco::Dictionary> dictionary;
  97. if (parser.has("d")) {
  98. int dictionaryId = parser.get<int>("d");
  99. dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
  100. }
  101. else if (parser.has("cd")) {
  102. FileStorage fs(parser.get<std::string>("cd"), FileStorage::READ);
  103. bool readOk = aruco::Dictionary::readDictionary(fs.root(), dictionary);
  104. if(!readOk) {
  105. cerr << "Invalid dictionary file" << endl;
  106. return 0;
  107. }
  108. }
  109. else {
  110. cerr << "Dictionary not specified" << endl;
  111. return 0;
  112. }
  113. VideoCapture inputVideo;
  114. int waitTime;
  115. if(!video.empty()) {
  116. inputVideo.open(video);
  117. waitTime = 0;
  118. } else {
  119. inputVideo.open(camId);
  120. waitTime = 10;
  121. }
  122. float axisLength = 0.5f * ((float)min(squaresX, squaresY) * (squareLength));
  123. // create charuco board object
  124. Ptr<aruco::CharucoBoard> charucoboard =
  125. aruco::CharucoBoard::create(squaresX, squaresY, squareLength, markerLength, dictionary);
  126. Ptr<aruco::Board> board = charucoboard.staticCast<aruco::Board>();
  127. double totalTime = 0;
  128. int totalIterations = 0;
  129. while(inputVideo.grab()) {
  130. Mat image, imageCopy;
  131. inputVideo.retrieve(image);
  132. double tick = (double)getTickCount();
  133. vector< int > markerIds, charucoIds;
  134. vector< vector< Point2f > > markerCorners, rejectedMarkers;
  135. vector< Point2f > charucoCorners;
  136. Vec3d rvec, tvec;
  137. // detect markers
  138. aruco::detectMarkers(image, dictionary, markerCorners, markerIds, detectorParams,
  139. rejectedMarkers);
  140. // refind strategy to detect more markers
  141. if(refindStrategy)
  142. aruco::refineDetectedMarkers(image, board, markerCorners, markerIds, rejectedMarkers,
  143. camMatrix, distCoeffs);
  144. // interpolate charuco corners
  145. int interpolatedCorners = 0;
  146. if(markerIds.size() > 0)
  147. interpolatedCorners =
  148. aruco::interpolateCornersCharuco(markerCorners, markerIds, image, charucoboard,
  149. charucoCorners, charucoIds, camMatrix, distCoeffs);
  150. // estimate charuco board pose
  151. bool validPose = false;
  152. if(camMatrix.total() != 0)
  153. validPose = aruco::estimatePoseCharucoBoard(charucoCorners, charucoIds, charucoboard,
  154. camMatrix, distCoeffs, rvec, tvec);
  155. double currentTime = ((double)getTickCount() - tick) / getTickFrequency();
  156. totalTime += currentTime;
  157. totalIterations++;
  158. if(totalIterations % 30 == 0) {
  159. cout << "Detection Time = " << currentTime * 1000 << " ms "
  160. << "(Mean = " << 1000 * totalTime / double(totalIterations) << " ms)" << endl;
  161. }
  162. // draw results
  163. image.copyTo(imageCopy);
  164. if(markerIds.size() > 0) {
  165. aruco::drawDetectedMarkers(imageCopy, markerCorners);
  166. }
  167. if(showRejected && rejectedMarkers.size() > 0)
  168. aruco::drawDetectedMarkers(imageCopy, rejectedMarkers, noArray(), Scalar(100, 0, 255));
  169. if(interpolatedCorners > 0) {
  170. Scalar color;
  171. color = Scalar(255, 0, 0);
  172. aruco::drawDetectedCornersCharuco(imageCopy, charucoCorners, charucoIds, color);
  173. }
  174. if(validPose)
  175. aruco::drawAxis(imageCopy, camMatrix, distCoeffs, rvec, tvec, axisLength);
  176. imshow("out", imageCopy);
  177. char key = (char)waitKey(waitTime);
  178. if(key == 27) break;
  179. }
  180. return 0;
  181. }