aruco_simple.cpp
Go to the documentation of this file.
1 /*****************************
2 Copyright 2011 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 
29 #include <string>
30 #include <iostream>
31 #include "aruco.h"
32 #include "cvdrawingutils.h"
33 #include <opencv2/highgui/highgui.hpp>
34 using namespace cv;
35 using namespace aruco;
36 //class for parsing command line
37 //operator [](string cmd) return whether cmd is present //string operator ()(string cmd) return the value as a string: -cmd value
38 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] ); }};
39 
40 int main(int argc, char **argv) {
41  try {
42  CmdLineParser cml(argc,argv);
43  if (argc ==1 || cml["-h"] ){
44  cerr << "Usage: (in_image|video.avi) [-c cameraParams.yml] [-s markerSize] [-d <dicionary>:ARUCO default] [-o <outImage>] " << endl;
45  cerr<<"\tDictionaries: "; for(auto dict:aruco::Dictionary::getDicTypes()) cerr<<dict<<" ";cerr<<endl;
46  cout<<"Example to work with apriltags dictionary : video.avi -d TAG36h11"<<endl<<endl;
47  return 0;
48  }
49 
50 
51  aruco::CameraParameters CamParam;
52 
53  // read the input image
54  cv::Mat InImage;
55  // Open input and read image
56  VideoCapture vreader(argv[1]);
57  if (vreader.isOpened()) vreader>>InImage;
58  else{cerr<<"Could not open input"<<endl;return -1;}
59  // read camera parameters if specifed
60  if (cml["-c"]) CamParam.readFromXMLFile(cml("-c"));
61  // read marker size if specified (default value -1)
62  float MarkerSize = std::stof(cml("-s","-1"));
63  //Create the detector
65  MDetector.setThresholdParams(7, 7);
66  MDetector.setThresholdParamRange(2, 0);
67  //Set the dictionary you want to work with, if you included option -d in command line
68  //see dictionary.h for all types
69  if (cml["-d"]) //if the -d is in the command line
70  MDetector.setDictionary( cml("-d"),0.f);//cml("-d") return the string after -d in the command line "example: ./program video.avi -d ARUCO", then, returns the string "ARUCO"
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  cout << Markers[i] << endl;
78  Markers[i].draw(InImage, Scalar(0, 0, 255), 2);
79  }
80  // draw a 3d cube in each marker if there is 3d info
81  if (CamParam.isValid() && MarkerSize != -1)
82  for (unsigned int i = 0; i < Markers.size(); i++) {
83  CvDrawingUtils::draw3dCube(InImage, Markers[i], CamParam);
84  }
85  // show input with augmented information
86  cv::namedWindow("in", 1);
87  cv::imshow("in", InImage);
88  while( char(cv::waitKey(0))!=27); // wait for esc to be pressed
89 
90 
91  if (cml["-o"]) cv::imwrite(cml("-o"), InImage);
92  } catch (std::exception &ex)
93 
94  {
95  cout << "Exception :" << ex.what() << endl;
96  }
97 }
98 
void setThresholdParams(double param1, double param2)
bool param(const std::string &param_name, T &param_val, const T &default_val)
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