aruco_simple_markermap.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 <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <opencv2/core/core.hpp>
32 #include <opencv2/highgui/highgui.hpp>
33 #include "aruco.h"
34 #include "posetracker.h"
35 #include "cvdrawingutils.h"
36 using namespace cv;
37 using namespace aruco;
38 /************************************
39  *
40  *
41  *
42  *
43  ************************************/
44 int main(int argc, char **argv) {
45  try {
46  if (argc < 3) {
47  cerr << "Usage: (in_image|video.avi) markerSetConfig.yml [cameraParams.yml] [markerSize] [outImage]" << endl;
48  exit(0);
49  }
50  //open video
51  cv::Mat InImage;
52  VideoCapture vreader(argv[1]);
53  if (!vreader.isOpened()) throw std::runtime_error("Could not open input");
54  //read input image(or first image from video)
55  vreader>>InImage;
56 
57  //read marker map
58  MarkerMap TheMarkerMapConfig;//configuration of the map
59  TheMarkerMapConfig.readFromFile(argv[2]);
60 
61  //read camera params if indicated
62  aruco::CameraParameters CamParam;
63  if (argc >= 4) {
64  CamParam.readFromXMLFile(argv[3]);
65  // resizes the parameters to fit the size of the input image
66  CamParam.resize(InImage.size());
67  }
68  //read marker size if indicated
69  float MarkerSize = -1;
70  if (argc >= 5) MarkerSize = atof(argv[4]);
71  //transform the markersetconfig to meter if is in pixels and the markersize indicated
72  if ( TheMarkerMapConfig.isExpressedInPixels() && MarkerSize>0)
73  TheMarkerMapConfig=TheMarkerMapConfig.convertToMeters(MarkerSize);
74 
75 
76 
77  //Let go
79  //set the appropiate dictionary type so that the detector knows it
80  MDetector.setDictionary(TheMarkerMapConfig.getDictionary());
81  // detect markers without computing R and T information
82  vector< Marker > Markers=MDetector.detect(InImage);
83 
84  //print the markers detected that belongs to the markerset
85  vector<int> markers_from_set=TheMarkerMapConfig.getIndices(Markers);
86  for(auto idx:markers_from_set) Markers[idx].draw(InImage, Scalar(0, 0, 255), 2);
87 
88 
89  //detect the 3d camera location wrt the markerset (if possible)
90  if ( TheMarkerMapConfig.isExpressedInMeters() && CamParam.isValid()) {
91  MarkerMapPoseTracker MSPoseTracker;//tracks the pose of the marker map
92  MSPoseTracker.setParams(CamParam,TheMarkerMapConfig);
93  if ( MSPoseTracker.estimatePose(Markers))//if pose correctly computed, print the reference system
94  aruco::CvDrawingUtils::draw3dAxis(InImage,CamParam,MSPoseTracker.getRvec(),MSPoseTracker.getTvec(),TheMarkerMapConfig[0].getMarkerSize()*2);
95  }
96  // show input with augmented information
97  cv::imshow("in", InImage);
98  while ( char(cv::waitKey(0))!=27) ; // wait for esc to be pressed
99  //save output if indicated
100  if (argc >= 6) cv::imwrite(argv[5], InImage);
101 
102  } catch (std::exception &ex)
103 
104  {
105  cout << "Exception :" << ex.what() << endl;
106  }
107 }
const cv::Mat getTvec() const
Definition: posetracker.h:104
void resize(cv::Size size)
bool isExpressedInPixels() const
Definition: markermap.h:90
bool isExpressedInMeters() const
Definition: markermap.h:87
const cv::Mat getRvec() const
Definition: posetracker.h:102
void setDictionary(std::string dict_type, float error_correction_rate=0)
std::vector< aruco::Marker > detect(const cv::Mat &input)
void readFromFile(string sfile)
Definition: markermap.cpp:89
static void draw3dAxis(cv::Mat &Image, const CameraParameters &CP, const cv::Mat &Rvec, const cv::Mat &Tvec, float axis_size)
bool estimatePose(const vector< Marker > &v_m)
void readFromXMLFile(string filePath)
void setParams(const CameraParameters &cam_params, const MarkerMap &msconf, float markerSize=-1)
MarkerMap TheMarkerMapConfig
MarkerDetector MDetector
Definition: aruco_test.cpp:38
MarkerMap convertToMeters(float markerSize)
Definition: markermap.cpp:297
Main class for marker detection.
Parameters of the camera.
std::string getDictionary() const
Definition: markermap.h:132
std::vector< int > getIndices(vector< aruco::Marker > &markers)
Definition: markermap.cpp:364
This class defines a set of markers whose locations are attached to a common reference system...
Definition: markermap.h:71
int main(int argc, char **argv)


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