detect_board.cpp 7.6 KB

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