Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. #include "aruco.h"
  2. #include <iostream>
  3. #include <opencv2/highgui/highgui.hpp>
  4. #include <opencv2/imgproc/imgproc.hpp>
  5. #include <string>
  6. #include <stdexcept>
  7.  
  8. using namespace cv;
  9. using namespace std;
  10. using namespace aruco;
  11. // class for parsing command line
  12. // operator [](string cmd) return whether cmd is present //string operator ()(string cmd) return the value as a string:
  13. // -cmd value
  14. class CmdLineParser{int argc;char** argv;public:CmdLineParser(int _argc, char** _argv): argc(_argc), argv(_argv){} bool operator[](string param) {int idx = -1; for (int i = 0; i < argc && idx == -1; i++)if (string(argv[i]) == param)idx = i;return (idx != -1);} string operator()(string param, string defvalue = "-1") {int idx = -1;for (int i = 0; i < argc && idx == -1; i++)if (string(argv[i]) == param)idx = i;if (idx == -1)return defvalue;else return (argv[idx + 1]);}};
  15.  
  16. cv::Mat __resize(const cv::Mat& in, int width)
  17. {
  18. if (in.size().width <= width)
  19. return in;
  20. float yf = float(width) / float(in.size().width);
  21. cv::Mat im2;
  22. cv::resize(in, im2, cv::Size(width, static_cast<int>(in.size().height * yf)));
  23. return im2;
  24. }
  25.  
  26. int main(int argc, char** argv)
  27. {
  28. try
  29. {
  30. CmdLineParser cml(argc, argv);
  31. if (argc == 1 || cml["-h"])
  32. {
  33. cerr << "Usage: (in_image|video.avi) [-c cameraParams.yml] [-s markerSize] [-d <dicionary>:ALL_DICTS default] [-f arucoConfig.yml] " << endl;
  34. cerr << "\tDictionaries: ";
  35. for (auto dict : aruco::Dictionary::getDicTypes())
  36. cerr << dict << " ";
  37. cerr << endl;
  38. cerr << "\t Instead of these, you can directly indicate the path to a file with your own generated "
  39. "dictionary"
  40. << endl;
  41. cout << "Example to work with apriltags dictionary : video.avi -d TAG36h11" << endl << endl;
  42. return 0;
  43. }
  44.  
  45. aruco::CameraParameters CamParam;
  46. // read the input image
  47. cv::Mat InImage;
  48. // Open input and read image
  49. VideoCapture vreader(argv[1]);
  50. if (vreader.isOpened()) vreader >> InImage;
  51. else throw std::runtime_error("Could not open input");
  52.  
  53. // read camera parameters if specifed
  54. if (cml["-c"])
  55. CamParam.readFromXMLFile(cml("-c"));
  56.  
  57. // read marker size if specified (default value -1)
  58. float MarkerSize = std::stof(cml("-s", "-1"));
  59. // Create the detector
  60. MarkerDetector MDetector;
  61. if(cml["-f"]){//uses a configuration file. YOu can create it from aruco_test application
  62. MDetector.loadParamsFromFile(cml("-f"));
  63. }
  64. else{
  65.  
  66. // Set the dictionary you want to work with, if you included option -d in command line
  67. //By default, all valid dictionaries are examined
  68. if (cml["-d"])
  69. MDetector.setDictionary(cml("-d"), 0.f);
  70.  
  71. }
  72. // Ok, let's detect
  73. vector<Marker> Markers = MDetector.detect(InImage, CamParam, MarkerSize);
  74.  
  75. // for each marker, draw info and its boundaries in the image
  76. for (unsigned int i = 0; i < Markers.size(); i++)
  77. {
  78. cout << Markers[i] << endl;
  79. Markers[i].draw(InImage, Scalar(0, 0, 255), 2);
  80. }
  81. // draw a 3d cube in each marker if there is 3d info
  82. if (CamParam.isValid() && MarkerSize != -1)
  83. for (unsigned int i = 0; i < Markers.size(); i++)
  84. {
  85. if(Markers[i].id==229 || Markers[i].id==161)
  86. cout<< "Camera Location= "<<Markers[i].id<<" "<<CamParam.getCameraLocation(Markers[i].Rvec,Markers[i].Tvec)<<endl;
  87. CvDrawingUtils::draw3dAxis(InImage, Markers[i], CamParam);
  88. // CvDrawingUtils::draw3dCube(InImage, Markers[i], CamParam);
  89. }
  90. // show input with augmented information
  91. cv::namedWindow("in", 1);
  92. cv::imshow("in", __resize(InImage,1280));
  93. while (char(cv::waitKey(0)) != 27)
  94. ; // wait for esc to be pressed
  95.  
  96. }
  97. catch (std::exception& ex)
  98.  
  99. {
  100. cout << "Exception :" << ex.what() << endl;
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement