aruco_test.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 #include <string>
29 #include <iostream>
30 #include <fstream>
31 #include <sstream>
32 #include "aruco.h"
33 #include "cvdrawingutils.h"
34 #include <opencv2/highgui/highgui.hpp>
35 using namespace cv;
36 using namespace aruco;
37 
39 VideoCapture TheVideoCapturer;
40 vector< Marker > TheMarkers;
43 void cvTackBarEvents(int pos, void *);
44 
45 pair< double, double > AvrgTime(0, 0); // determines the average time required for detection
47 int waitTime = 0;
48 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] ); }};
49 
50 
51 
52 /************************************
53  *
54  *
55  *
56  *
57  ************************************/
58 int main(int argc, char **argv) {
59  try {
60  CmdLineParser cml(argc,argv);
61  if (argc < 2 || cml["-h"]) {
62  cerr << "Invalid number of arguments" << endl;
63  cerr << "Usage: (in.avi|live[:idx_cam=0]) [-c camera_params.yml] [-s marker_size_in_meters] [-d dictionary:ARUCO by default] [-h]" << endl;
64  cerr<<"\tDictionaries: "; for(auto dict:aruco::Dictionary::getDicTypes()) cerr<<dict<<" ";cerr<<endl;
65  return false;
66  }
67 
69  string TheInputVideo = argv[1];
70  // read camera parameters if passed
71  if (cml["-c"] ) TheCameraParameters.readFromXMLFile(cml("-c"));
72  float TheMarkerSize = std::stof(cml("-s","-1"));
73  aruco::Dictionary::DICT_TYPES TheDictionary= Dictionary::getTypeFromString( cml("-d","ARUCO") );
74 
76  // read from camera or from file
77  if (TheInputVideo.find("live") != string::npos) {
78  int vIdx = 0;
79  // check if the :idx is here
80  char cad[100];
81  if (TheInputVideo.find(":") != string::npos) {
82  std::replace(TheInputVideo.begin(), TheInputVideo.end(), ':', ' ');
83  sscanf(TheInputVideo.c_str(), "%s %d", cad, &vIdx);
84  }
85  cout << "Opening camera index " << vIdx << endl;
86  TheVideoCapturer.open(vIdx);
87  waitTime = 10;
88  }
89  else TheVideoCapturer.open(TheInputVideo);
90  // check video is open
91  if (!TheVideoCapturer.isOpened()) throw std::runtime_error("Could not open video");
92 
93 
95  // read first image to get the dimensions
97  if (TheCameraParameters.isValid())
98  TheCameraParameters.resize(TheInputImage.size());
99 
100  MDetector.setDictionary(TheDictionary);//sets the dictionary to be employed (ARUCO,APRILTAGS,ARTOOLKIT,etc)
101  MDetector.setThresholdParams(7, 7);
102  MDetector.setThresholdParamRange(2, 0);
103  //gui requirements : the trackbars to change this parameters
104  iThresParam1 = MDetector.getParams()._thresParam1;
105  iThresParam2 = MDetector.getParams()._thresParam2;
106  cv::namedWindow("in");
107  cv::createTrackbar("ThresParam1", "in", &iThresParam1, 25, cvTackBarEvents);
108  cv::createTrackbar("ThresParam2", "in", &iThresParam2, 13, cvTackBarEvents);
109 
110  //go!
111  char key = 0;
112  int index = 0;
113  // capture until press ESC or until the end of the video
114  do {
115 
116  TheVideoCapturer.retrieve(TheInputImage);
117  // copy image
118  double tick = (double)getTickCount(); // for checking the speed
119  // Detection of markers in the image passed
120  TheMarkers= MDetector.detect(TheInputImage, TheCameraParameters, TheMarkerSize);
121  // chekc the speed by calculating the mean speed of all iterations
122  AvrgTime.first += ((double)getTickCount() - tick) / getTickFrequency();
123  AvrgTime.second++;
124  cout << "\rTime detection=" << 1000 * AvrgTime.first / AvrgTime.second << " milliseconds nmarkers=" << TheMarkers.size() << std::endl;
125 
126  // print marker info and draw the markers in image
127  TheInputImage.copyTo(TheInputImageCopy);
128 
129  for (unsigned int i = 0; i < TheMarkers.size(); i++) {
130  cout << TheMarkers[i]<<endl;
131  TheMarkers[i].draw(TheInputImageCopy, Scalar(0, 0, 255), 1);
132  }
133 
134  // draw a 3d cube in each marker if there is 3d info
135  if (TheCameraParameters.isValid() && TheMarkerSize>0)
136  for (unsigned int i = 0; i < TheMarkers.size(); i++) {
137  CvDrawingUtils::draw3dCube(TheInputImageCopy, TheMarkers[i], TheCameraParameters);
138  CvDrawingUtils::draw3dAxis(TheInputImageCopy, TheMarkers[i], TheCameraParameters);
139  }
140 
141  // DONE! Easy, right?
142  // show input with augmented information and the thresholded image
143  cv::imshow("in", TheInputImageCopy);
144  cv::imshow("thres", MDetector.getThresholdedImage());
145 
146 
147  key = cv::waitKey(waitTime); // wait for key to be pressed
148  if(key=='s') waitTime= waitTime==0?10:0;
149  index++; // number of images captured
150  } while (key != 27 && (TheVideoCapturer.grab() ));
151 
152  } catch (std::exception &ex)
153 
154  {
155  cout << "Exception :" << ex.what() << endl;
156  }
157 }
158 /************************************
159  *
160  *
161  *
162  *
163  ************************************/
164 
165 void cvTackBarEvents(int pos, void *) {
166  (void)(pos);
167  if (iThresParam1 < 3) iThresParam1 = 3;
168  if (iThresParam1 % 2 != 1) iThresParam1++;
169  if (iThresParam1 < 1) iThresParam1 = 1;
171  // recompute
172  MDetector.detect(TheInputImage, TheMarkers, TheCameraParameters);
174  for (unsigned int i = 0; i < TheMarkers.size(); i++)
175  TheMarkers[i].draw(TheInputImageCopy, Scalar(0, 0, 255), 1);
176 
177  // draw a 3d cube in each marker if there is 3d info
178  if (TheCameraParameters.isValid())
179  for (unsigned int i = 0; i < TheMarkers.size(); i++)
180  CvDrawingUtils::draw3dCube(TheInputImageCopy, TheMarkers[i], TheCameraParameters);
181 
182  cv::imshow("in", TheInputImageCopy);
183  cv::imshow("thres", MDetector.getThresholdedImage());
184 }
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)
Params getParams() const
int main(int argc, char **argv)
Definition: aruco_test.cpp:58
static std::vector< std::string > getDicTypes()
Definition: dictionary.cpp:270
int iThresParam1
Definition: aruco_test.cpp:46
void setDictionary(std::string dict_type, float error_correction_rate=0)
std::vector< aruco::Marker > detect(const cv::Mat &input)
void cvTackBarEvents(int pos, void *)
Definition: aruco_test.cpp:165
void readFromXMLFile(string filePath)
MarkerDetector MDetector
Definition: aruco_test.cpp:38
void setThresholdParamRange(size_t r1=0, size_t r2=0)
CameraParameters TheCameraParameters
Definition: aruco_test.cpp:42
vector< Marker > TheMarkers
Definition: aruco_test.cpp:40
float TheMarkerSize
Main class for marker detection.
const cv::Mat & getThresholdedImage()
Parameters of the camera.
pair< double, double > AvrgTime(0, 0)
string TheInputVideo
Mat TheInputImageCopy
Definition: aruco_test.cpp:41
VideoCapture TheVideoCapturer
Definition: aruco_test.cpp:39
int waitTime
Definition: aruco_test.cpp:47
int iThresParam2
Definition: aruco_test.cpp:46
Mat TheInputImage
Definition: aruco_test.cpp:41


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