53 void trim(std::string& s)
59 while (pos <
s.size() && isspace(s[pos]))
71 while (pos > 0 && isspace(s[pos - 1]))
86 bool loadFile(std::map<std::string, std::string>& data,
const char* name)
104 size_t pos = line.find(
'#');
106 if (pos != line.npos)
108 line = line.substr(0, pos);
113 pos = line.find(
'=');
115 if (pos != line.npos)
117 std::string key = line.substr(0, pos);
118 std::string value = line.substr(pos + 1);
123 data.insert(std::pair<std::string, std::string>(key, value));
141 inline std::string createKey(
const char *name,
int id)
146 if (
id >= 0) out <<
id <<
'.';
158 void getValue(
const std::map<std::string, std::string>& data,
const std::string &key, T& value,
const char* defvalue)
160 std::map<std::string, std::string>::const_iterator it =
data.find(key.c_str());
163 if (it !=
data.end())
172 std::istringstream in(v);
181 bool getMatrix33(
const std::map<std::string, std::string>& data,
const std::string &key,
184 std::map<std::string, std::string>::const_iterator it =
data.find(key.c_str());
187 if (it !=
data.end())
193 v =
"[1 0 0; 0 1 0; 0 0 1]";
196 std::istringstream in(v);
203 for (
int k = 0; k < 3 && in; k++)
205 for (
int i = 0; i < 3 && in; i++)
212 if (k + 1 < 3 && c !=
';')
236 bool getVector3(
const std::map<std::string, std::string>& data,
const std::string &key,
239 std::map<std::string, std::string>::const_iterator it =
data.find(key.c_str());
242 if (it !=
data.end())
251 std::istringstream in(v);
258 for (
int i = 0; i < 3 && in; i++)
278 inline void mulMatrix33Matrix33(
double ret[3][3],
double A[3][3],
double B[3][3])
280 for (
int k=0; k<3; k++)
282 for (
int i=0; i<3; i++)
286 for (
int j=0; j<3; j++)
288 ret[k][i] += A[k][j] * B[j][i];
294 inline void mulMatrix33Vector3(
double ret[3],
double M[3][3],
double V[3])
296 for (
int k=0; k<3; k++)
299 for (
int i=0; i<3; i++)
301 ret[k] += M[k][i] * V[i];
306 inline void transposeMatrix33(
double M[3][3])
325 inline void storeMatrix(boost::array<double, 9>& values,
double M[3][3])
328 for (
int k = 0; k < 3; k++)
330 for (
int i = 0; i < 3; i++)
332 values[j++] = M[k][i];
347 pub_ = nh.
advertise<sensor_msgs::CameraInfo>(
"camera_info", 1);
359 if (calib_file != 0 && calib_file[0] !=
'\0')
361 std::map<std::string, std::string>
data;
363 if (loadFile(
data, calib_file))
367 getValue(
data, createKey(
"width",
id),
info_.width,
"0");
368 getValue(
data, createKey(
"height",
id),
info_.height,
"0");
373 if (!getMatrix33(
data, createKey(
"A",
id), A))
375 ROS_ERROR(
"Getting camera.A from calibration file failed");
376 info_ = sensor_msgs::CameraInfo();
380 storeMatrix(
info_.K, A);
384 double e1, e2, e3, e4;
386 getValue(
data, createKey(
"e1",
id), e1,
"0");
387 getValue(
data, createKey(
"e2",
id), e2,
"0");
388 getValue(
data, createKey(
"e3",
id), e3,
"0");
389 getValue(
data, createKey(
"e4",
id), e4,
"0");
391 if (e1 != 0 || e2 != 0 || e3 != 0 || e4 != 0)
393 info_.distortion_model =
"equidistant";
403 double k1, k2, k3, p1, p2;
405 getValue(
data, createKey(
"k1",
id), k1,
"0");
406 getValue(
data, createKey(
"k2",
id), k2,
"0");
407 getValue(
data, createKey(
"k3",
id), k3,
"0");
408 getValue(
data, createKey(
"p1",
id), p1,
"0");
409 getValue(
data, createKey(
"p2",
id), p2,
"0");
411 info_.distortion_model =
"plumb_bob";
425 getValue(
data,
"rect.f",
f,
"0");
431 double A0[3][3], A1[3][3];
432 if (getMatrix33(
data, createKey(
"A", 0), A0) && getMatrix33(
data, createKey(
"A", 1), A1))
434 f = (A0[0][0] + A0[1][1] + A1[0][0] + A1[1][1])/4;
438 ROS_ERROR(
"Getting camera.A0 and camera.A1 from calibration file failed");
439 info_ = sensor_msgs::CameraInfo();
445 f = (A[0][0] + A[1][1])/2;
457 double R0[3][3], R1[3][3];
460 getMatrix33(
data, createKey(
"R", 0), R0);
461 getMatrix33(
data, createKey(
"R", 1), R1);
462 getVector3(
data, createKey(
"T", 0), T0);
463 getVector3(
data, createKey(
"T", 1), T1);
470 transposeMatrix33(R1);
471 mulMatrix33Matrix33(R, R1, R0);
477 transposeMatrix33(R0);
478 mulMatrix33Vector3(T, R0, T1);
482 t = std::sqrt(T[0] * T[0] + T[1] * T[1] + T[2] * T[2]);
483 double l = std::sqrt(T[0] * T[0] + T[1] * T[1]);
489 Rrect[0][0] = T[0]/t;
490 Rrect[1][0] = T[1]/t;
491 Rrect[2][0] = T[2]/t;
493 Rrect[0][1] = -T[1]/l;
494 Rrect[1][1] = T[0]/l;
497 Rrect[0][2] = Rrect[1][0] * Rrect[2][1] - Rrect[2][0] * Rrect[1][1];
498 Rrect[1][2] = Rrect[2][0] * Rrect[0][1] - Rrect[0][0] * Rrect[2][1];
499 Rrect[2][2] = Rrect[0][0] * Rrect[1][1] - Rrect[1][0] * Rrect[0][1];
503 transposeMatrix33(Rrect);
504 storeMatrix(
info_.R, Rrect);
508 double Rrect_right[3][3];
509 mulMatrix33Matrix33(Rrect_right, R, Rrect);
511 transposeMatrix33(Rrect_right);
512 storeMatrix(
info_.R, Rrect_right);
549 ROS_ERROR_STREAM(
"gc_genicam_camera: Cannot load camera calibration: " << calib_file);
563 info_.header = image->header;
567 info_.width = image->width;
568 info_.height = image->height;