vpgl_calibration_matrix.hxx
Go to the documentation of this file.
1 // This is core/vpgl/vpgl_calibration_matrix.hxx
2 #ifndef vpgl_calibration_matrix_hxx_
3 #define vpgl_calibration_matrix_hxx_
4 //:
5 // \file
6 
8 
9 #include <vgl/vgl_point_2d.h>
10 #include <vgl/io/vgl_io_point_2d.h>
11 #include <vnl/vnl_inverse.h>
12 #include <vnl/vnl_vector_fixed.h>
13 #include <vnl/vnl_matrix_fixed.h>
14 #include <cassert>
15 #ifdef _MSC_VER
16 # include <vcl_msvc_warnings.h>
17 #endif
18 
19 //--------------------------------------
20 template <class T>
22  focal_length_( (T)1 ),
23  principal_point_( vgl_point_2d<T>( (T)0, (T)0 ) ),
24  x_scale_( (T)1 ),
25  y_scale_( (T)1 ),
26  skew_( (T)0 )
27 {
28 }
29 
30 
31 //--------------------------------------
32 template <class T>
34  T focal_length, const vgl_point_2d<T>& principal_point, T x_scale, T y_scale, T skew ) :
35  focal_length_( focal_length ),
36  principal_point_( principal_point ),
37  x_scale_( x_scale ),
38  y_scale_( y_scale ),
39  skew_( skew )
40 {
41  // Make sure the inputs are valid.
42  assert( focal_length != 0 );
43  assert( x_scale > 0 );
44  assert( y_scale > 0 );
45 }
46 
47 
48 //--------------------------------------
49 template <class T>
51 {
52  // Put the supplied matrix into canonical form and check that it could be a
53  // calibration matrix.
54  assert( K(2,2) != (T)0 && K(1,0) == (T)0 && K(2,0) == (T)0 && K(2,1) == (T)0 );
55  double scale_factor = 1.0;
56  if ( K(2,2) != (T)1 ) scale_factor /= (double)K(2,2);
57 
58  focal_length_ = (T)1;
59  x_scale_ = T(scale_factor*K(0,0));
60  y_scale_ = T(scale_factor*K(1,1));
61  skew_ = T(scale_factor*K(0,1));
62  principal_point_.set( T(scale_factor*K(0,2)), T(scale_factor*K(1,2)) );
63 
64  assert( ( x_scale_ > 0 && y_scale_ > 0 ) || ( x_scale_ < 0 && y_scale_ < 0 ) );
65 }
66 
67 
68 //--------------------------------------
69 template <class T>
71 {
72  // Construct the matrix as in H&Z.
73  vnl_matrix_fixed<T,3,3> K( (T)0 );
74  K(0,0) = focal_length_*x_scale_;
75  K(1,1) = focal_length_*y_scale_;
76  K(2,2) = (T)1;
77  K(0,2) = principal_point_.x();
78  K(1,2) = principal_point_.y();
79  K(0,1) = skew_;
80  return K;
81 }
82 
83 
84 //--------------------------------------
85 template <class T>
87 {
88  assert( new_focal_length != (T)0 );
89  focal_length_ = new_focal_length;
90 }
91 
92 
93 //--------------------------------------
94 template <class T>
96  const vgl_point_2d<T>& new_principal_point )
97 {
98  assert( !new_principal_point.ideal() );
99  principal_point_ = new_principal_point;
100 }
101 
102 
103 //--------------------------------------
104 template <class T>
106 {
107  assert( new_x_scale > 0 );
108  x_scale_ = new_x_scale;
109 }
110 
111 
112 //--------------------------------------
113 template <class T>
115 {
116  assert( new_y_scale > 0 );
117  y_scale_ = new_y_scale;
118 }
119 
120 
121 //--------------------------------------
122 template <class T>
124 {
125  skew_ = new_skew;
126 }
127 
128 //: Equality test
129 template <class T> bool vpgl_calibration_matrix<T>::
131 {
132  if (this == &that) // same object => equal.
133  return true;
134 
135  return
136  this->focal_length_ == that.focal_length_ &&
137  this->principal_point_ == that.principal_point_ &&
138  this->x_scale_ == that.x_scale_ && this->y_scale_ == that.y_scale_ &&
139  this->skew_ == that.skew_;
140 }
141 
142 //: Map from image to focal plane.
143 // (Later may need to cache the inverse for efficiency)
144 template <class T>
146 map_to_focal_plane(vgl_point_2d<T> const& p_image) const
147 {
148  vnl_vector_fixed<T,3> p(p_image.x(), p_image.y(), 1);
149  vnl_matrix_fixed<T,3,3> Kinv = vnl_inverse(this->get_matrix());
150  vnl_vector_fixed<T,3> pf = Kinv*p;
151  return vgl_point_2d<T>(pf[0]/pf[2], pf[1]/pf[2]);
152 }
153 
154 template <class T>
156 map_to_image(vgl_point_2d<T> const& p_focal_plane) const
157 {
158  vnl_vector_fixed<T,3> p(p_focal_plane.x(), p_focal_plane.y(), 1);
159  vnl_matrix_fixed<T,3,3> K = this->get_matrix();
160  vnl_vector_fixed<T,3> pf = K*p;
161  return vgl_point_2d<T>(pf[0]/pf[2], pf[1]/pf[2]);
162 }
163 
164 // Code for easy instantiation.
165 #undef vpgl_CALIBRATION_MATRIX_INSTANTIATE
166 #define vpgl_CALIBRATION_MATRIX_INSTANTIATE(T) \
167 template class vpgl_calibration_matrix<T >
168 
169 #endif // vpgl_calibration_matrix_hxx_
vpgl_calibration_matrix()
Default constructor makes an identity matrix.
vgl_point_2d< T > map_to_image(vgl_point_2d< T > const &p_focal_plane) const
A class representing the "K" matrix of a perspective camera matrix as described in.
void set_principal_point(const vgl_point_2d< T > &new_principal_point)
T focal_length_
The following is a list of the parameters in the calibration matrix.
bool ideal(T=(T) 0) const
vnl_matrix_fixed< T, 1, 1 > vnl_inverse(vnl_matrix_fixed< T, 1, 1 > const &m)
vnl_matrix_fixed< T, 3, 3 > get_matrix() const
Get the calibration matrix.
vgl_point_2d< T > principal_point_
vgl_point_2d< T > map_to_focal_plane(vgl_point_2d< T > const &p_image) const
Maps to and from the focal plane.
void set_focal_length(T new_focal_length)
Getters and setters for all of the parameters.
bool operator==(vpgl_calibration_matrix< T > const &that) const
Equality tests.
A class for the calibration matrix component of a perspective camera matrix.