Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef CALIBRATION_H
00017 #define CALIBRATION_H
00018
00019 #include "options.h"
00020 #include "cxcore.h"
00021 #include <string>
00022
00023 class calibration
00024 {
00025 public:
00046 calibration (const options &opts)
00047 : intrinsic (opts.intrinsic_matrix ())
00048 {
00049 load_world (opts.external_view ());
00050 }
00051
00052 const matrix3& get_intrinsic_matrix () const
00053 {
00054 return intrinsic;
00055 }
00056
00071 CvPoint camera_to_pixel (const vector3 &v, double scale = 1.0) const
00072 {
00073 CvPoint retval;
00074
00075 const vector3 p = intrinsic * v;
00076
00077 retval.x = scale * p (0) / p (2) + 0.5;
00078 retval.y = scale * p (1) / p (2) + 0.5;
00079
00080 return retval;
00081 }
00082
00097 CvPoint world_to_pixel (const vector3 &v, double scale = 1.0) const
00098 {
00099 const vector3 c = world_to_camera (v);
00100 return camera_to_pixel (c, scale);
00101 }
00102
00111 vector3 pixel_to_camera (const CvPoint p, double scale = 1.0) const
00112 {
00113 vector3 retval;
00114 retval [0] = (((double)p.x) / scale - intrinsic (0, 2)) / intrinsic (0, 0);
00115 retval [1] = (((double)p.y) / scale - intrinsic (1, 2)) / intrinsic (1, 1);
00116 retval [2] = 1.0;
00117 return retval;
00118 }
00119
00127 vector3 pixel_to_world (const CvPoint p, double scale = 1.0) const
00128 {
00129 const vector3 c = pixel_to_camera (p, scale);
00130 return camera_to_world (c);
00131 }
00132
00142 vector3 world_to_camera (const vector3 &v) const
00143 {
00144
00145 const vector3 retval = 100.0 * (inv_world_R * v - world_translation);
00146 return retval;
00147 }
00148
00158 vector3 camera_to_world (const vector3 &v) const
00159 {
00160
00161 const vector3 retval = world_R * (v / 100.0 + world_translation);
00162 return retval;
00163 }
00164
00176 vector3 camera_centre () const
00177 {
00178 return camera_to_world (vector3 (0, 0, 0));
00179 }
00180
00189 void set_world_translation (const vector3 &t)
00190 {
00191 world_translation = t;
00192 };
00193
00202 vector3 get_world_translation () const
00203 {
00204 return world_translation;
00205 };
00206
00215 void set_world_rotation (const vector3 &R)
00216 {
00217 world_rot_vec = R;
00218
00219 const matrix3 rxm = OpenTissue::math::Rx (R [0]);
00220 const matrix3 rym = OpenTissue::math::Ry (R [1]);
00221 const matrix3 rzm = OpenTissue::math::Rz (R [2]);
00222
00223 world_R = rxm * rym * rzm;
00224
00225 const matrix3 irxm = OpenTissue::math::Rx (-R [0]);
00226 const matrix3 irym = OpenTissue::math::Ry (-R [1]);
00227 const matrix3 irzm = OpenTissue::math::Rz (-R [2]);
00228
00229 inv_world_R = irzm * irym * irxm;
00230 };
00231
00240 vector3 get_world_rotation () const
00241 {
00242 return world_rot_vec;
00243 };
00244
00255 bool save_world (const std::string filename) const
00256 {
00257 bool retval = false;
00258 std::ofstream rtfile (filename.c_str ());
00259 if (rtfile.is_open ())
00260 {
00261 rtfile << world_translation [0] << " " << world_translation [1] << " "
00262 << world_translation [2] << std::endl;
00263 rtfile << world_rot_vec [0] << " " << world_rot_vec [1] << " "
00264 << world_rot_vec [2] << std::endl;
00265 rtfile.close ();
00266 retval = true;
00267 }
00268 else
00269 std::cerr << "Could not open '" << filename << "' for writing" << std::endl;
00270
00271 return retval;
00272 };
00273
00294 bool load_world (const std::string filename)
00295 {
00296 bool retval = false;
00297 std::ifstream rtfile (filename.c_str ());
00298 if (rtfile.is_open ())
00299 {
00300 for (size_t k = 0; k < 3; k++)
00301 rtfile >> world_translation [k];
00302 for (size_t k = 0; k < 3; k++)
00303 rtfile >> world_rot_vec [k];
00304 rtfile.close ();
00305 set_world_rotation (world_rot_vec);
00306 retval = true;
00307 }
00308 else
00309 std::cerr << "Could not open '" << filename << "' for reading" << std::endl;
00310
00311 return retval;
00312 };
00313
00314 private:
00315 matrix3 intrinsic, world_R, inv_world_R;
00316 vector3 world_translation, world_rot_vec;
00317 };
00318
00319 #endif // CALIBRATION_H
00320