KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
calibrated_camera.h
Go to the documentation of this file.
1 /* $Id: calibrated_camera.h 18278 2014-11-25 01:42:10Z ksimek $ */
2 /* =========================================================================== *
3  |
4  | Copyright (c) 1994-2010 by Kobus Barnard (author)
5  |
6  | Personal and educational use of this code is granted, provided that this
7  | header is kept intact, and that the authorship is not misrepresented, that
8  | its use is acknowledged in publications, and relevant papers are cited.
9  |
10  | For other use contact the author (kobus AT cs DOT arizona DOT edu).
11  |
12  | Please note that the code in this file has not necessarily been adequately
13  | tested. Naturally, there is no guarantee of performance, support, or fitness
14  | for any particular task. Nonetheless, I am interested in hearing about
15  | problems that you encounter.
16  |
17  | Author: Kyle Simek
18  * =========================================================================== */
19 
20 #ifndef KJB_CAMERA_CPP_CAMERA_CALIBRATED_CAMERA
21 #define KJB_CAMERA_CPP_CAMERA_CALIBRATED_CAMERA
22 
23 // this should be first:
24 #ifdef KJB_HAVE_BST_SERIAL
25 #include <boost/archive/text_iarchive.hpp>
26 #include <boost/archive/text_oarchive.hpp>
27 #endif
28 
31 
32 #include <m_cpp/m_matrix.h>
33 #include <m_cpp/m_vector.h>
34 
35 #include <g_cpp/g_quaternion.h>
36 
37 #ifdef KJB_HAVE_BST_SERIAL
38 //#include <boost/serialization/nvp.hpp>
39 #include <boost/serialization/access.hpp>
40 #endif
41 
42 namespace kjb
43 {
44 
52 {
53 #ifdef KJB_HAVE_BST_SERIAL
54  friend class boost::serialization::access;
55 #endif
56 
57 typedef Perspective_camera Base;
58 typedef Calibrated_camera Self;
59 
60 public:
61  Calibrated_camera(double near = 10, double far = 10000) :
62  Base(near, far)
63  {}
64 
66  const Matrix& camera_matrix,
68  double near = 10,
69  double far = 10000) :
70  Base(near, far)
71  {
72  Matrix intrinsic, rotation;
73  Vector translation;
74 
75  // does simple decomposition
77  camera_matrix,
78  intrinsic,
79  rotation,
80  translation);
81 
82  // converts to "standard format" by moving principal point to center of image,
83  // enforcing an upward y-axis, flipping the z-axis if the origin maps behind the camera,
84  // etc.
85  cal.look_down_positive_z = false;
86  standardize_camera_matrices(intrinsic, rotation, translation, cal);
87 
88  // force intrinsic(2,2) to be -1
89 // intrinsic *= -1;
90 
91  initialize(
92  intrinsic,
93  rotation,
94  translation);
95  }
96 
97  Calibrated_camera(const Self& other) :
98  Base(other)
99  { }
100 
101  virtual Self* clone() const
102  {
103  return new Self(*this);
104  }
105 
106  virtual Self& operator=(const Self& other)
107  {
108  Base::operator=(other);
109  return *this;
110  }
111 
116  const Matrix& intrinsic,
117  Matrix rotation,
118  Vector translation)
119  {
120  double alpha_x = intrinsic(0,0);
121  double s = intrinsic(0,1);
122  double alpha_y = intrinsic(1,1);
123  double x0 = intrinsic(0,2);
124  double y0 = intrinsic(1,2);
125 
126  double theta = atan2(alpha_x, -s);
127  // TODO test this value
128  if(theta < 0) theta += M_PI;
129  if(theta > M_PI) theta -= M_PI;
130 
131  double tmp_aspect_ratio = alpha_y * sin(theta) / alpha_x;
132 
133  if(tmp_aspect_ratio < 0)
134  {
135  tmp_aspect_ratio *= -1;
136  y0 *= -1;
137  }
138 
139  // These three variables correspond to
140  // how the camera is parameterized in the
141  // Parametric_camera class. The choice of parameterization is
142  // a bit esoteric, which results in us using these strance expressions
143  // in order for the opengl matrix to be constructed right.
144  // TODO: have an alternative parameterization that abstracts away
145  // these confusing expressions.
146  double skew_ = acos(-s/alpha_y);
147  double focal = alpha_y * sin(skew_);
148  double ar = alpha_x / focal;
149  set_focal_length(focal);
150  set_aspect_ratio(ar);
151  set_skew(skew_);
152 // set_focal_length(alpha_x);
153 // set_aspect_ratio(tmp_aspect_ratio);
154 // set_skew(theta);
155 
156  // we've already flipped z-coordinate during
157  // camera matrix decomposition & standardization.
158  // Negating here counteracts the negating that happens
159  // in set_principal_point().
160  set_principal_point(Vector().set(-x0, -y0));
161 
162  set_orientation(Quaternion(rotation));
163 
164  // TODO: add set world origin() method to perspective camera so we don't need to manipulate this here
165  Vector camera_center = -rotation.transpose() * translation;
166  set_camera_centre(camera_center);
167  }
168 
169  void set_orientation(const Quaternion& orientation)
170  {
171  set_angles_from_quaternion(orientation);
172  }
173 
175  { return get_rotations_as_a_quaternion(); }
176 
177 private:
178 
179  template<class Archive>
180  void serialize(Archive &ar, const unsigned int /* version */)
181  {
182 #ifdef KJB_HAVE_BST_SERIAL
183  ar & ::boost::serialization::base_object<Base>(*this);
184 #else
185  KJB_THROW_2(Missing_dependency, "boost::serialization");
186 #endif
187  }
188 
189 };
190 
191 } // namespace kjb
192 
193 
194 #endif
virtual void set_aspect_ratio(double iar)
sets the aspect ratio
Definition: perspective_camera.cpp:956
virtual void set_camera_centre(const kjb::Vector &icentre)
sets the camera centre
Definition: perspective_camera.cpp:695
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
bool look_down_positive_z
Definition: g_camera_calibration.h:59
Definition: g_camera_calibration.h:30
theta
Definition: APPgetLargeConnectedEdges.m:108
virtual void set_focal_length(double ifocal)
sets the focal length
Definition: perspective_camera.cpp:872
void standardize_camera_matrices(Matrix &intrinsic, Matrix &rotation, Vector &translation, const Calibration_descriptor &cal)
Definition: g_camera_calibration.cpp:79
void decompose_camera_matrix(const Matrix &camera_matrix, Matrix &intrinsic, Matrix &rotation, Vector &translation)
Definition: g_camera_calibration.cpp:184
This class implements vectors, in the linear-algebra sense, with real-valued elements.
Definition: m_vector.h:87
Calibrated_camera(const Self &other)
Definition: calibrated_camera.h:97
virtual void set_angles_from_quaternion(const kjb::Quaternion &q)
sets the rotation angles from an input quaternion
Definition: perspective_camera.cpp:985
const kjb::Quaternion & get_rotations_as_a_quaternion() const
returns the rotations of this camera as a quaternion
Definition: perspective_camera.h:408
virtual Perspective_camera & operator=(const Perspective_camera &pc)
Definition: perspective_camera.cpp:313
void initialize(const Matrix &intrinsic, Matrix rotation, Vector translation)
Definition: calibrated_camera.h:115
St_perspective_camera for modeling a perspective camera using the classic Forsyth and Ponce parametri...
Definition: g_quaternion.h:40
virtual Self & operator=(const Self &other)
Definition: calibrated_camera.h:106
Definition: perspective_camera.h:93
const Quaternion & get_orientation() const
Definition: calibrated_camera.h:174
virtual void set_skew(double is)
sets the skew angle
Definition: perspective_camera.cpp:946
virtual void set_principal_point(const kjb::Vector &ip)
sets the principal point
Definition: perspective_camera.cpp:897
void set_orientation(const Quaternion &orientation)
Alias of set_angles_from_quaternion()
Definition: calibrated_camera.h:169
#define KJB_THROW_2(ex, msg)
Definition: l_exception.h:48
Calibrated_camera(const Matrix &camera_matrix, Calibration_descriptor cal, double near=10, double far=10000)
Definition: calibrated_camera.h:65
#define M_PI
Definition: fft.cpp:206
Matrix transpose() const
Transpose this matrix.
Definition: m_matrix.cpp:395
Calibrated_camera(double near=10, double far=10000)
Definition: calibrated_camera.h:61
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Object thrown when a program lacks required resources or libraries.
Definition: l_exception.h:539
Definition: calibrated_camera.h:51
Definition for the Vector class, a thin wrapper on the KJB Vector struct and its related functionalit...
virtual Self * clone() const
Definition: calibrated_camera.h:101