Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
biasextractcheckerboard.cpp
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #include <Base/Image/Image.hh>
26 #include <Base/Image/ImageIO.hh>
27 #include <FeatureDetector/CheckerboardDetector.hh>
28 #include <iostream>
29 #include <fstream>
30 
31 #ifndef WIN32
32 #include <unistd.h>
33 #else //WIN32
34 #include "Base/Common/getopt_W32.h"
35 #endif //WIN32
36 
37 using namespace std;
38 using namespace BIAS;
39 
40 
41 /**
42  @file
43  @ingroup g_tools
44  @brief The tool tries to find checkerboards in the input images finds corners
45  also in fisheye distorted images andwrites these to the Matlab file 'Filename.m', see biasextractcheckerboard.cpp
46  @author streckel
47 */
48 
49 void usage()
50 {
51  cout <<"Usage: biasextractcheckerboard -f Filename.m -x xnum -y ynum -d image1 image2 ..."<<endl
52  <<endl;
53  cout <<" The tool tries to find checkerboards in the input images" << endl
54  <<" finds corners also in fisheye distorted images and " << endl
55  <<" writes these to the Matlab file 'Filename.m'." << endl
56  <<" xnum and ynum are the number of the checkerboards inner corners."
57  << endl
58  <<" -d writes debug images." << endl
59  <<" Do not use .mip images since Matlab is unable to read them." << endl
60  <<endl<<endl;
61 }
62 
63 
64 int main(int argc, char *argv[])
65 {
66 
67  string MatlabFilename = "ExtractedCorners.m";
68  int x_corners = 7;
69  int y_corners = 4;
70  bool writedebug = true;
71 
72  int c = 1;
73  while(c >0) {
74  c = char(getopt(argc,argv,"f:x:y:hd"));
75  switch (c) {
76  case 'h':
77  case ':':
78  case '?':
79  usage();
80  break;
81  case 'x':
82  x_corners = atoi(optarg);
83  break;
84  case 'y':
85  y_corners = atoi(optarg);
86  break;
87  case 'f':
88  MatlabFilename = optarg;
89  break;
90  case 'd':
91  writedebug=true;
92  break;
93  default:
94  if (c>0){
95  usage();
96  exit(1);
97  }
98  }
99  }
100 
101  if (argc-optind < 1) {
102  cout <<"==> To few arguments."<<endl;
103  usage();
104  exit(1);
105  }
106 
108  vector<string> filenames, bad_filenames;
109  vector<vector<pair<float,float> > > all_coords;
110  for (int i=optind; i<argc; i++) {
111 
112  if (ImageIO::Load(argv[i], img)!=0) {
113  BIASERR("Could not load "<<argv[i]); BIASBREAK;
114  return -1;
115  }
116  std::vector< std::pair<float,float> > coords;
117  int result = CheckerboardDetector::Compute(img, x_corners, y_corners,
118  coords, writedebug);
119  if (result==0) {
120  cout << "Sucess: " << argv[i]<< endl;
121  all_coords.push_back(coords);
122  filenames.push_back(argv[i]);
123  } else {
124  cout << "Failure: " << argv[i]<< endl;
125  bad_filenames.push_back(argv[i]);
126  }
127 
128  }
129  ofstream ofs(MatlabFilename.c_str());
130 
131  ofs << "numpoints = [" << x_corners << " " << y_corners << "];" << endl;
132 
133  ofs << "imagenames = {";
134  for (unsigned int i=0; i<filenames.size(); i++) {
135  ofs << "'" << filenames[i] << "' ";
136  }
137  ofs << "};" << endl;
138 
139  if (bad_filenames.size()>0) {
140  ofs << "bad_imagenames = {";
141  for (unsigned int i=0; i<bad_filenames.size(); i++) {
142  ofs << "'" << bad_filenames[i] << "' ";
143  }
144  ofs << "};" << endl;
145  }
146 
147  ofs << "xpos = [";
148  for (unsigned int i=0; i<all_coords.size(); i++) {
149  for (unsigned int j=0; j<all_coords[i].size(); j++) {
150  ofs << (all_coords[i][j]).first << " ";
151  }
152  ofs << ";" << endl;
153  }
154  ofs << "];" << endl;
155 
156  ofs << "ypos = [";
157  for (unsigned int i=0; i<all_coords.size(); i++) {
158  for (unsigned int j=0; j<all_coords[i].size(); j++) {
159  ofs << (all_coords[i][j]).second << " ";
160  }
161  ofs << ";" << endl;
162  }
163  ofs << "];" << endl;
164 
165  ofs.close();
166 
167  return 0;
168 }
169