ImageCalUtility.cc
Go to the documentation of this file.
1 
37 #ifdef WIN32
38 #ifndef WIN32_LEAN_AND_MEAN
39 #define WIN32_LEAN_AND_MEAN 1
40 #endif
41 
42 #include <windows.h>
43 #include <winsock2.h>
44 #else
45 #include <unistd.h>
46 #endif
47 
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <sys/types.h>
51 #include <sys/stat.h>
52 #include <fstream>
53 #include <map>
54 #include <string.h>
55 
59 
60 using namespace crl::multisense;
61 
62 namespace { // anonymous
63 
64 void usage(const char *programNameP)
65 {
66  fprintf(stderr,
67  "USAGE: %s -e <extrinisics_file> -i <intrinsics_file> [<options>]\n",
68  programNameP);
69  fprintf(stderr, "Where <options> are:\n");
70  fprintf(stderr, "\t-a <ip_address> : ip address (default=10.66.171.21)\n");
71  fprintf(stderr, "\t-s : set the calibration (default is query)\n");
72  fprintf(stderr, "\t-y : disable confirmation prompts\n");
73 
74  exit(-1);
75 }
76 
77 bool fileExists(const std::string& name)
78 {
79  struct stat sbuf;
80  return (0 == stat(name.c_str(), &sbuf));
81 }
82 
83 std::ostream& writeImageIntrinics (std::ostream& stream, image::Calibration const& calibration)
84 {
85  stream << "%YAML:1.0\n";
86  writeMatrix (stream, "M1", 3, 3, &calibration.left.M[0][0]);
87  writeMatrix (stream, "D1", 1, 8, &calibration.left.D[0]);
88  writeMatrix (stream, "M2", 3, 3, &calibration.right.M[0][0]);
89  writeMatrix (stream, "D2", 1, 8, &calibration.right.D[0]);
90  return stream;
91 }
92 
93 std::ostream& writeImageExtrinics (std::ostream& stream, image::Calibration const& calibration)
94 {
95  stream << "%YAML:1.0\n";
96  writeMatrix (stream, "R1", 3, 3, &calibration.left.R[0][0]);
97  writeMatrix (stream, "P1", 3, 4, &calibration.left.P[0][0]);
98  writeMatrix (stream, "R2", 3, 3, &calibration.right.R[0][0]);
99  writeMatrix (stream, "P2", 3, 4, &calibration.right.P[0][0]);
100  return stream;
101 }
102 
103 }; // anonymous
104 
105 int main(int argc,
106  char **argvPP)
107 {
108  std::string ipAddress = "10.66.171.21";
109  std::string intrinsicsFile;
110  std::string extrinsicsFile;
111  bool setCal=false;
112  bool prompt=true;
113 
114  //
115  // Parse args
116 
117  int c;
118 
119  while(-1 != (c = getopt(argc, argvPP, "a:e:i:sy")))
120  switch(c) {
121  case 'a': ipAddress = std::string(optarg); break;
122  case 'i': intrinsicsFile = std::string(optarg); break;
123  case 'e': extrinsicsFile = std::string(optarg); break;
124  case 's': setCal = true; break;
125  case 'y': prompt = false; break;
126  default: usage(*argvPP); break;
127  }
128 
129  //
130  // Verify options
131 
132  if (intrinsicsFile.empty() || extrinsicsFile.empty()) {
133  fprintf(stderr, "Both intrinsics and extrinsics files must be set\n");
134  usage(*argvPP);
135  }
136 
137  if (true == setCal &&
138  (false == fileExists(intrinsicsFile) ||
139  false == fileExists(extrinsicsFile))) {
140 
141  fprintf(stderr, "intrinsics or extrinsics file not found\n");
142  usage(*argvPP);
143  }
144 
145  if (false == setCal && true == prompt &&
146  (true == fileExists(intrinsicsFile) ||
147  true == fileExists(extrinsicsFile))) {
148 
149  fprintf(stdout,
150  "One or both of \"%s\" and \"%s\" already exists.\n\n"
151  "Really overwrite these files? (y/n): ",
152  intrinsicsFile.c_str(),
153  extrinsicsFile.c_str());
154  fflush(stdout);
155 
156  int c = getchar();
157  if ('Y' != c && 'y' != c) {
158  fprintf(stdout, "Aborting\n");
159  return 0;
160  }
161  }
162 
163  //
164  // Initialize communications.
165 
166  Channel *channelP = Channel::Create(ipAddress);
167  if (NULL == channelP) {
168  fprintf(stderr, "Failed to establish communications with \"%s\"\n",
169  ipAddress.c_str());
170  return -1;
171  }
172 
173  //
174  // Query version
175 
176  Status status;
177  VersionType version;
178 
179  status = channelP->getSensorVersion(version);
180  if (Status_Ok != status) {
181  fprintf(stderr, "failed to query sensor version: %s\n",
182  Channel::statusString(status));
183  goto clean_out;
184  }
185 
186  //
187  // Query
188 
189  if (false == setCal) {
190 
192 
193  status = channelP->getImageCalibration(c);
194  if (Status_Ok != status) {
195  fprintf(stderr, "failed to query image calibration: %s\n",
196  Channel::statusString(status));
197  goto clean_out;
198  }
199 
200  std::ofstream inFile, exFile;
201 
202  inFile.open (intrinsicsFile.c_str (), std::ios_base::out | std::ios_base::trunc);
203 
204  if (!inFile) {
205  fprintf(stderr, "failed to open '%s' for writing\n",
206  intrinsicsFile.c_str());
207  goto clean_out;
208  }
209 
210  exFile.open (extrinsicsFile.c_str (), std::ios_base::out | std::ios_base::trunc);
211 
212  if (!exFile) {
213  fprintf(stderr, "failed to open '%s' for writing\n",
214  extrinsicsFile.c_str());
215  goto clean_out;
216  }
217 
218  writeImageIntrinics (inFile, c);
219  writeImageExtrinics (exFile, c);
220 
221  inFile.flush ();
222  exFile.flush ();
223 
224  } else {
225 
226  std::ifstream inFile, exFile;
227  std::map<std::string, std::vector<float> > data;
228 
229  inFile.open (intrinsicsFile.c_str ());
230 
231  if (!inFile) {
232  fprintf(stderr, "failed to open '%s' for reading\n",
233  intrinsicsFile.c_str());
234  goto clean_out;
235  }
236 
237  parseYaml (inFile, data);
238 
239  inFile.close ();
240 
241  if (data["M1"].size () != 3 * 3 ||
242  (data["D1"].size () != 5 && data["D1"].size () != 8) ||
243  data["M2"].size () != 3 * 3 ||
244  (data["D2"].size () != 5 && data["D2"].size () != 8)) {
245  fprintf(stderr, "intrinsic matrices incomplete in %s\n",
246  intrinsicsFile.c_str());
247  goto clean_out;
248  }
249 
250  exFile.open (extrinsicsFile.c_str ());
251 
252  if (!exFile) {
253  fprintf(stderr, "failed to open '%s' for reading\n",
254  extrinsicsFile.c_str());
255  goto clean_out;
256  }
257 
258  parseYaml (exFile, data);
259 
260  exFile.close ();
261 
262  if (data["R1"].size () != 3 * 3 ||
263  data["P1"].size () != 3 * 4 ||
264  data["R2"].size () != 3 * 3 ||
265  data["P2"].size () != 3 * 4) {
266  fprintf(stderr, "extrinsic matrices incomplete in %s\n",
267  extrinsicsFile.c_str());
268  goto clean_out;
269  }
270 
272 
273  memcpy (&c.left.M[0][0], &data["M1"].front (), data["M1"].size () * sizeof (float));
274  memset (&c.left.D[0], 0, sizeof (c.left.D));
275  memcpy (&c.left.D[0], &data["D1"].front (), data["D1"].size () * sizeof (float));
276  memcpy (&c.left.R[0][0], &data["R1"].front (), data["R1"].size () * sizeof (float));
277  memcpy (&c.left.P[0][0], &data["P1"].front (), data["P1"].size () * sizeof (float));
278 
279  memcpy (&c.right.M[0][0], &data["M2"].front (), data["M2"].size () * sizeof (float));
280  memset (&c.right.D[0], 0, sizeof (c.right.D));
281  memcpy (&c.right.D[0], &data["D2"].front (), data["D2"].size () * sizeof (float));
282  memcpy (&c.right.R[0][0], &data["R2"].front (), data["R2"].size () * sizeof (float));
283  memcpy (&c.right.P[0][0], &data["P2"].front (), data["P2"].size () * sizeof (float));
284 
285  status = channelP->setImageCalibration(c);
286  if (Status_Ok != status) {
287  fprintf(stderr, "failed to set image calibration: %s\n",
288  Channel::statusString(status));
289  goto clean_out;
290  }
291 
292  fprintf(stdout, "Image calibration successfully updated\n");
293  }
294 
295 clean_out:
296 
297  Channel::Destroy(channelP);
298  return 0;
299 }
static const char * statusString(Status status)
Definition: channel.cc:609
static Channel * Create(const std::string &sensorAddress)
Definition: channel.cc:583
std::ostream & writeMatrix(std::ostream &stream, std::string const &name, uint32_t rows, uint32_t columns, T const *data)
int main(int argc, char **argvPP)
int getopt(int argc, char **argv, char *opts)
Definition: getopt.c:34
virtual Status getImageCalibration(image::Calibration &c)=0
static CRL_CONSTEXPR Status Status_Ok
virtual Status setImageCalibration(const image::Calibration &c)=0
def usage(progname)
std::istream & parseYaml(std::istream &stream, std::map< std::string, std::vector< float > > &data)
static void Destroy(Channel *instanceP)
Definition: channel.cc:596
char * optarg
Definition: getopt.c:32
virtual Status getSensorVersion(VersionType &version)=0


multisense_lib
Author(s):
autogenerated on Sat Apr 6 2019 02:16:46