detect_markers.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 <iostream>
  34. #include "aruco_samples_utility.hpp"
  35. using namespace std;
  36. using namespace cv;
  37. namespace {
  38. const char* about = "Basic marker detection";
  39. //! [aruco_detect_markers_keys]
  40. const char* keys =
  41. "{d | | dictionary: DICT_4X4_50=0, DICT_4X4_100=1, DICT_4X4_250=2,"
  42. "DICT_4X4_1000=3, DICT_5X5_50=4, DICT_5X5_100=5, DICT_5X5_250=6, DICT_5X5_1000=7, "
  43. "DICT_6X6_50=8, DICT_6X6_100=9, DICT_6X6_250=10, DICT_6X6_1000=11, DICT_7X7_50=12,"
  44. "DICT_7X7_100=13, DICT_7X7_250=14, DICT_7X7_1000=15, DICT_ARUCO_ORIGINAL = 16,"
  45. "DICT_APRILTAG_16h5=17, DICT_APRILTAG_25h9=18, DICT_APRILTAG_36h10=19, DICT_APRILTAG_36h11=20}"
  46. "{cd | | Input file with custom dictionary }"
  47. "{v | | Input from video or image file, if ommited, input comes from camera }"
  48. "{ci | 0 | Camera id if input doesnt come from video (-v) }"
  49. "{c | | Camera intrinsic parameters. Needed for camera pose }"
  50. "{l | 0.1 | Marker side length (in meters). Needed for correct scale in camera pose }"
  51. "{dp | | File of marker detector parameters }"
  52. "{r | | show rejected candidates too }"
  53. "{refine | | Corner refinement: CORNER_REFINE_NONE=0, CORNER_REFINE_SUBPIX=1,"
  54. "CORNER_REFINE_CONTOUR=2, CORNER_REFINE_APRILTAG=3}";
  55. }
  56. //! [aruco_detect_markers_keys]
  57. int main(int argc, char *argv[]) {
  58. CommandLineParser parser(argc, argv, keys);
  59. parser.about(about);
  60. if(argc < 2) {
  61. parser.printMessage();
  62. return 0;
  63. }
  64. bool showRejected = parser.has("r");
  65. bool estimatePose = parser.has("c");
  66. float markerLength = parser.get<float>("l");
  67. Ptr<aruco::DetectorParameters> detectorParams;
  68. if(parser.has("dp")) {
  69. FileStorage fs(parser.get<string>("dp"), FileStorage::READ);
  70. bool readOk = aruco::DetectorParameters::readDetectorParameters(fs.root(), detectorParams);
  71. if(!readOk) {
  72. cerr << "Invalid detector parameters file" << endl;
  73. return 0;
  74. }
  75. }
  76. if (parser.has("refine")) {
  77. //override cornerRefinementMethod read from config file
  78. detectorParams->cornerRefinementMethod = parser.get<int>("refine");
  79. }
  80. std::cout << "Corner refinement method (0: None, 1: Subpixel, 2:contour, 3: AprilTag 2): " << detectorParams->cornerRefinementMethod << std::endl;
  81. int camId = parser.get<int>("ci");
  82. String video;
  83. if(parser.has("v")) {
  84. video = parser.get<String>("v");
  85. }
  86. if(!parser.check()) {
  87. parser.printErrors();
  88. return 0;
  89. }
  90. Ptr<aruco::Dictionary> dictionary;
  91. if (parser.has("d")) {
  92. int dictionaryId = parser.get<int>("d");
  93. dictionary = aruco::getPredefinedDictionary(aruco::PREDEFINED_DICTIONARY_NAME(dictionaryId));
  94. }
  95. else if (parser.has("cd")) {
  96. FileStorage fs(parser.get<std::string>("cd"), FileStorage::READ);
  97. bool readOk = aruco::Dictionary::readDictionary(fs.root(), dictionary);
  98. if(!readOk) {
  99. std::cerr << "Invalid dictionary file" << std::endl;
  100. return 0;
  101. }
  102. }
  103. else {
  104. std::cerr << "Dictionary not specified" << std::endl;
  105. return 0;
  106. }
  107. Mat camMatrix, distCoeffs;
  108. if(estimatePose) {
  109. bool readOk = readCameraParameters(parser.get<string>("c"), camMatrix, distCoeffs);
  110. if(!readOk) {
  111. cerr << "Invalid camera file" << endl;
  112. return 0;
  113. }
  114. }
  115. VideoCapture inputVideo;
  116. int waitTime;
  117. if(!video.empty()) {
  118. inputVideo.open(video);
  119. waitTime = 0;
  120. } else {
  121. inputVideo.open(camId);
  122. waitTime = 10;
  123. }
  124. double totalTime = 0;
  125. int totalIterations = 0;
  126. while(inputVideo.grab()) {
  127. Mat image, imageCopy;
  128. inputVideo.retrieve(image);
  129. double tick = (double)getTickCount();
  130. vector< int > ids;
  131. vector< vector< Point2f > > corners, rejected;
  132. vector< Vec3d > rvecs, tvecs;
  133. // detect markers and estimate pose
  134. aruco::detectMarkers(image, dictionary, corners, ids, detectorParams, rejected);
  135. if(estimatePose && ids.size() > 0)
  136. aruco::estimatePoseSingleMarkers(corners, markerLength, camMatrix, distCoeffs, rvecs,
  137. tvecs);
  138. double currentTime = ((double)getTickCount() - tick) / getTickFrequency();
  139. totalTime += currentTime;
  140. totalIterations++;
  141. if(totalIterations % 30 == 0) {
  142. cout << "Detection Time = " << currentTime * 1000 << " ms "
  143. << "(Mean = " << 1000 * totalTime / double(totalIterations) << " ms)" << endl;
  144. }
  145. // draw results
  146. image.copyTo(imageCopy);
  147. if(ids.size() > 0) {
  148. aruco::drawDetectedMarkers(imageCopy, corners, ids);
  149. if(estimatePose) {
  150. for(unsigned int i = 0; i < ids.size(); i++)
  151. aruco::drawAxis(imageCopy, camMatrix, distCoeffs, rvecs[i], tvecs[i],
  152. markerLength * 0.5f);
  153. }
  154. }
  155. if(showRejected && rejected.size() > 0)
  156. aruco::drawDetectedMarkers(imageCopy, rejected, noArray(), Scalar(100, 0, 255));
  157. imshow("out", imageCopy);
  158. char key = (char)waitKey(waitTime);
  159. if(key == 27) break;
  160. }
  161. return 0;
  162. }