aruco_tracker.cpp
Go to the documentation of this file.
1 /*****************************************************************************************
2 Copyright 2016 Rafael Muñoz Salinas. All rights reserved.
3 
4 Redistribution and use in source and binary forms, with or without modification, are
5 permitted provided that the following conditions are met:
6 
7  1. Redistributions of source code must retain the above copyright notice, this list of
8  conditions and the following disclaimer.
9 
10  2. Redistributions in binary form must reproduce the above copyright notice, this list
11  of conditions and the following disclaimer in the documentation and/or other materials
12  provided with the distribution.
13 
14 THIS SOFTWARE IS PROVIDED BY Rafael Muñoz Salinas ''AS IS'' AND ANY EXPRESS OR IMPLIED
15 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Rafael Muñoz Salinas OR
17 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 
24 The views and conclusions contained in the software and documentation are those of the
25 authors and should not be interpreted as representing official policies, either expressed
26 or implied, of Rafael Muñoz Salinas.
27 ********************************************************************************************/
28 
33 #include <string>
34 
35 #include <iostream>
36 #include "aruco.h"
37 #include "cvdrawingutils.h"
38 #include <opencv2/highgui/highgui.hpp>
39 using namespace cv;
40 using namespace aruco;
41 //class for parsing command line
42 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] ); }};
43 
44 
45 int main(int argc, char **argv) {
46  try {
47  CmdLineParser cml(argc,argv);
48  if (argc <4 || cml["-h"] ){
49  cerr << "Usage: video.avi cameraParams.yml markerSize [-d <dicionary>:ARUCO default] " << endl;
50  cerr<<"\tDictionaries: "; for(auto dict:aruco::Dictionary::getDicTypes()) cerr<<dict<<" ";cerr<<endl;
51  cout<<"Example to work with apriltags dictionary : video.avi -d TAG36h11"<<endl<<endl;
52  return 0;
53  }
54 
55 
56  aruco::CameraParameters CamParam;
57 
58  // read the input image
59  cv::Mat InImage;
60  // Open input and read image
61  VideoCapture vreader(argv[1]);
62  if (vreader.isOpened()) vreader>>InImage;
63  else{cerr<<"Could not open input"<<endl;return -1;}
64  // read camera parameters if specifed
65  CamParam.readFromXMLFile(argv[2]);
66  CamParam.resize(InImage.size());
67  // read marker size if specified (default value -1)
68  float MarkerSize = std::stof(argv[3]);
69  //Create the detector
71  MDetector.setThresholdParams(7, 7);
72  MDetector.setThresholdParamRange(2, 0);
73  std::map<uint32_t,MarkerPoseTracker> MTracker;//use a map so that for each id, we use a different pose tracker
74  //Set the dictionary you want to work with, if you included option -d in command line
75  //see dictionary.h for all types
76  if (cml["-d"]) MDetector.setDictionary( cml("-d"),0.f);
77 
78  do{
79  vreader.retrieve(InImage);
80  // Ok, let's detect
81  vector< Marker > Markers=MDetector.detect(InImage);
82  for(auto & marker:Markers)//for each marker
83  MTracker[marker.id].estimatePose(marker,CamParam,MarkerSize);//call its tracker and estimate the pose
84 
85  // for each marker, draw info and its boundaries in the image
86  for (unsigned int i = 0; i < Markers.size(); i++) {
87  cout << Markers[i] << endl;
88  Markers[i].draw(InImage, Scalar(0, 0, 255), 2);
89  }
90  // draw a 3d cube in each marker if there is 3d info
91  if (CamParam.isValid() && MarkerSize != -1){
92  for (unsigned int i = 0; i < Markers.size(); i++) {
93  CvDrawingUtils::draw3dCube(InImage, Markers[i], CamParam);
94  CvDrawingUtils::draw3dAxis(InImage, Markers[i], CamParam);
95  }
96  }
97  // show input with augmented information
98  cv::namedWindow("in", 1);
99  cv::imshow("in", InImage);
100  } while( char(cv::waitKey(0))!=27 && vreader.grab()); // wait for esc to be pressed
101 
102 
103  if (cml["-o"]) cv::imwrite(cml("-o"), InImage);
104  } catch (std::exception &ex)
105 
106  {
107  cout << "Exception :" << ex.what() << endl;
108  }
109 }
110 
void setThresholdParams(double param1, double param2)
bool param(const std::string &param_name, T &param_val, const T &default_val)
void resize(cv::Size size)
int main(int argc, char **argv)
f
static std::vector< std::string > getDicTypes()
Definition: dictionary.cpp:270
void setDictionary(std::string dict_type, float error_correction_rate=0)
std::vector< aruco::Marker > detect(const cv::Mat &input)
void readFromXMLFile(string filePath)
MarkerDetector MDetector
Definition: aruco_test.cpp:38
void setThresholdParamRange(size_t r1=0, size_t r2=0)
Main class for marker detection.
Parameters of the camera.


tuw_aruco
Author(s): Lukas Pfeifhofer
autogenerated on Mon Jun 10 2019 15:40:45