aruco_calibration_fromimages.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/highgui/highgui.hpp>
32 #include <opencv2/imgproc/imgproc.hpp>
33 #include <opencv2/calib3d/calib3d.hpp>
34 #include "aruco.h"
36 using namespace cv;
37 using namespace aruco;
38 
43 string TheOutFile;
45 float TheMarkerSize=-1;
47 
48 
49 
50 
51 //given the set of markers detected, the function determines the get the 2d-3d correspondes
52 void getMarker2d_3d(vector<cv::Point2f> &p2d, vector<cv::Point3f> &p3d, const vector< Marker >&markers_detected,const MarkerMap &bc){
53  p2d.clear();
54  p3d.clear();
55  //for each detected marker
56  for(size_t i=0;i<markers_detected.size();i++){
57  //find it in the bc
58  int fidx=-1;
59  for(size_t j=0;j<bc.size() &&fidx==-1;j++)
60  if (bc[j].id==markers_detected[i].id ) fidx=j;
61  if (fidx!=-1){
62  for(int j=0;j<4;j++){
63  p2d.push_back(markers_detected[i][j]);
64  p3d.push_back(bc[fidx][j]);
65  }
66  }
67  }
68  cout<<"points added"<<endl;
69 }
70 vector < vector<cv::Point2f> > calib_p2d;
71 vector < vector<cv::Point3f> > calib_p3d;
72 aruco::CameraParameters camp;//camera parameters estimated
73 
74 
75 int parseInput(int argc,char **argv){
76  TheOutFile=argv[1];
77  int lastOption=2;
78  for(int i=2;i<argc;i++){
79  if ( string(argv[i])=="-size" ){
80  TheMarkerSize=atof(argv[i+1]);
81  lastOption=i+2;
82  }
83  if ( string(argv[i])=="-m" ){
84  TheMarkerMapConfigFile=argv[i+1];
85  lastOption=i+2;
86  }
87  }
88  return lastOption;
89 }
90 /************************************
91  *
92  *
93  *
94  *
95  ************************************/
96 int main(int argc, char **argv) {
97  try {
98 
99  if (argc<4){
100  cerr << "Usage: out_camera_calibration.yml [options] image1 image2 ... " << endl;
101  cerr<<"options:"<<endl;
102  cerr<<"-size maker_size : Size of the markers in meters. "<<endl;
103  cerr<<"-m markersetconfig.yml : By default, the one in utils is assumed. Otherwise, set the file here "<<endl;
104  return -1;
105  }
106  // parse arguments
107  int firstImageIdx=parseInput(argc,argv);
108  //load board info
109  if ( TheMarkerMapConfigFile.empty()){
110  stringstream sstr;sstr.write((char*)default_a4_board,default_a4_board_size);
111  TheMarkerMapConfig.fromStream(sstr);
112  }else
113  TheMarkerMapConfig.readFromFile(TheMarkerMapConfigFile);
114  //is in meters
115  if (!TheMarkerMapConfig.isExpressedInMeters() ){
116  if (TheMarkerSize==-1){
117  cerr<<"Need to specify the length of the board with -size"<<endl;
118  return -1;
119  }
120  else TheMarkerMapConfig=TheMarkerMapConfig.convertToMeters(TheMarkerSize );
121  }
122  vector<string> images;
123  for(int i=firstImageIdx;i<argc;i++)images.push_back(argv[i]);
124  TheInputImage=cv::imread(images[0]);
125 
126  //set specific parameters for this configuration
127  MarkerDetector::Params params;
128  params._borderDistThres=.01;//acept markers near the borders
129  params._thresParam1=5;
130  params._thresParam1_range=10;//search in wide range of values for param1
131  params._cornerMethod=MarkerDetector::SUBPIX;//use subpixel corner refinement
132  params._subpix_wsize= (10./2000.)*float(TheInputImage.cols) ;//search corner subpix in a widow area
133  cout<<params._subpix_wsize<<" "<<float(TheInputImage.cols)<<endl;
134  TheMarkerDetector.setParams(params);//set the params above
135  TheMarkerDetector.setDictionary(TheMarkerMapConfig.getDictionary());
136 
137 
138  int currImage=0;
139  cv::Size imageSize(-1,-1);
140  // capture until press ESC or until the end of the video
141  do {
142  cout<<"reading "<<images[currImage]<<endl;
143  TheInputImage=cv::imread(images[currImage]);
144  if (!TheInputImage.empty()){
145  if (imageSize!=cv::Size(-1,-1) && imageSize!=TheInputImage.size()){
146  cerr<<"Input image sizes must be equal"<<endl;exit(0);
147  }
148  imageSize=TheInputImage.size();
149 
150  // detect and print
151  vector<aruco::Marker> detected_markers=TheMarkerDetector.detect(TheInputImage);
152  vector<int> markers_from_set=TheMarkerMapConfig.getIndices(detected_markers);
154  for(auto idx:markers_from_set) detected_markers[idx].draw(TheInputImageCopy, Scalar(0, 0, 255), 1);
155 
156 
157  if (TheInputImageCopy.cols>1280 )
158  cv::resize(TheInputImageCopy,TheInputImage,cv::Size(1280, 1280.*float(TheInputImageCopy.rows)/float(TheInputImageCopy.cols)));
159  else TheInputImageCopy.copyTo(TheInputImage);
160 
161 
162  vector<cv::Point2f> p2d;
163  vector<cv::Point3f> p3d;
164 
165  getMarker2d_3d(p2d,p3d,detected_markers,TheMarkerMapConfig);
166  if(p3d.size()>0){
167  calib_p2d.push_back(p2d);
168  calib_p3d.push_back(p3d);
169  }
170 
171  // show input with augmented information and the thresholded image
172  cv::imshow("in", TheInputImage);
173  // write to video if required
174  cv::waitKey(600); // wait for key to be pressed
175  }
176  else cerr<<"Could not read image "<<images[currImage]<<endl;
177  } while (++currImage<int(images.size()));
178 
179  cout<<"Starting calibration"<<endl;
180  vector<cv::Mat> vr,vt;
181  camp.CamSize=imageSize;
182  cout<<calib_p2d.size()<<endl;
183  double err=cv::calibrateCamera(calib_p3d,calib_p2d,imageSize,camp.CameraMatrix,camp.Distorsion,vr,vt);
184  cerr<<"repj error="<<err<<endl;
185  camp.saveToFile(TheOutFile);
186  cerr<<"File saved in :"<<TheOutFile<<endl;
187 
188  } catch (std::exception &ex)
189 
190  {
191  cout << "Exception :" << ex.what() << endl;
192  }
193 }
vector< vector< cv::Point2f > > calib_p2d
aruco::CameraParameters camp
void saveToFile(string path, bool inXML=true)
bool isExpressedInMeters() const
Definition: markermap.h:87
unsigned int default_a4_board_size
vector< vector< cv::Point3f > > calib_p3d
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
unsigned char default_a4_board[]
MarkerMap TheMarkerMapConfig
void setParams(Params p)
string TheMarkerMapConfigFile
MarkerMap convertToMeters(float markerSize)
Definition: markermap.cpp:297
CameraParameters TheCameraParameters
Main class for marker detection.
Parameters of the camera.
int parseInput(int argc, char **argv)
std::string getDictionary() const
Definition: markermap.h:132
int main(int argc, char **argv)
string TheOutCameraParams
void fromStream(std::istream &str)
Definition: markermap.cpp:384
MarkerDetector TheMarkerDetector
std::vector< int > getIndices(vector< aruco::Marker > &markers)
Definition: markermap.cpp:364
void getMarker2d_3d(vector< cv::Point2f > &p2d, vector< cv::Point3f > &p3d, const vector< Marker > &markers_detected, const MarkerMap &bc)
CornerRefinementMethod _cornerMethod
This class defines a set of markers whose locations are attached to a common reference system...
Definition: markermap.h:71


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