vgl_h_matrix_2d.h
Go to the documentation of this file.
1 // This is core/vgl/algo/vgl_h_matrix_2d.h
2 #ifndef vgl_h_matrix_2d_h_
3 #define vgl_h_matrix_2d_h_
4 //:
5 // \file
6 // \brief 3x3 plane-to-plane projectivity
7 //
8 // A class to hold a plane-to-plane projective transformation matrix
9 // and to perform common operations using it e.g. transfer point.
10 //
11 // \verbatim
12 // Modifications
13 // 16 Aug 2010 - Gamze Tunali - added is_identity()
14 // 22 Oct 2002 - Peter Vanroose - added vgl_homg_point_2d interface
15 // 23 Oct 2002 - Peter Vanroose - using fixed 3x3 matrices throughout
16 // 22 Mar 2003 - J.L. Mundy - preparing for upgrade to vgl
17 // 24 Jun 2003 - Peter Vanroose - added projective_basis() from 4 lines
18 // 31 Jul 2010 - Peter Vanroose - made more similar to 1d and 3d variants
19 // 24 Oct 2010 - Peter Vanroose - mutators and setters now return *this
20 // 27 Oct 2010 - Peter Vanroose - moved Doxygen docs from .hxx to .h
21 // \endverbatim
22 
23 #include <vector>
24 #include <iosfwd>
25 #include <vnl/vnl_fwd.h> // for vnl_vector_fixed<T,2>
26 #include <vnl/vnl_matrix_fixed.h>
27 #include <vgl/vgl_homg_point_2d.h>
28 #include <vgl/vgl_homg_line_2d.h>
29 #include <vgl/vgl_conic.h>
30 #ifdef _MSC_VER
31 # include <vcl_msvc_warnings.h>
32 #endif
33 
34 //:
35 // A class to hold a plane-to-plane projective transformation matrix
36 // and to perform common operations using it e.g. transfer point.
37 template <class T>
38 class vgl_h_matrix_2d
39 {
40  // Data Members--------------------------------------------------------------
41  protected:
42  vnl_matrix_fixed<T,3,3> t12_matrix_;
43 
44  public:
45 
46  // Constructors/Initializers/Destructors-------------------------------------
47 
48  vgl_h_matrix_2d() = default;
49  ~vgl_h_matrix_2d() = default;
50  //: Copy constructor
52  //: Constructor from a 3x3 matrix, and implicit cast from vnl_matrix_fixed<T,3,3>
53  vgl_h_matrix_2d(vnl_matrix_fixed<T,3,3> const& M) : t12_matrix_(M) {}
54  //: Construct an affine vgl_h_matrix_2d from 2x2 M and 2x1 m.
55  vgl_h_matrix_2d(vnl_matrix_fixed<T,2,2> const& M,
56  vnl_vector_fixed<T,2> const& m);
57  //: Constructor from 3x3 C-array
58  explicit vgl_h_matrix_2d(T const* M) : t12_matrix_(M) {}
59  //: Constructor from istream
60  explicit vgl_h_matrix_2d(std::istream& s);
61  //: Constructor from file
62  explicit vgl_h_matrix_2d(char const* filename);
63  //: Constructor - calculate homography between two sets of 2D points (minimum 4)
64  vgl_h_matrix_2d(std::vector<vgl_homg_point_2d<T> > const& points1,
65  std::vector<vgl_homg_point_2d<T> > const& points2);
66 
67  // Operations----------------------------------------------------------------
68 
69  //: Return the transformed point given by $q = {\tt H} p$
71  //: Return the transformed point given by $q = {\tt H} p$
72  vgl_homg_point_2d<T> operator*(vgl_homg_point_2d<T> const& p) const { return (*this)(p);}
73 
74  bool operator==(vgl_h_matrix_2d<T> const& M) const { return t12_matrix_ == M.get_matrix(); }
75 
76  //: Return the transformed line given by $m = {\tt H} l$
80 
81  //: assumed to be a point conic
82  vgl_conic<T> operator() (vgl_conic<T> const& C) const;
83 
84  // these operations require taking an inverse
85 
86  //: Return the transformed point given by $p = {\tt H}^{-1} q$
88  //: Return the transformed line given by $m = {\tt H}^{-1} l$
90  //: Return the transformed line given by $m = {\tt H}^{-1} l$
91  vgl_homg_line_2d<T> operator*(vgl_homg_line_2d<T> const& l) const { return (*this)(l);}
92  //: assumed to be a point conic
93  vgl_conic<T> preimage(vgl_conic<T> const& C) const;
94 
95  //:composition (*this) * H
98 
99  // Data Access---------------------------------------------------------------
100 
101  //: Return the 3x3 homography matrix
102  vnl_matrix_fixed<T,3,3> const& get_matrix() const { return t12_matrix_; }
103  //: Fill M with contents of the 3x3 homography matrix
104  void get(vnl_matrix_fixed<T,3,3>* M) const;
105  //:
106  // \deprecated use the vnl_matrix_fixed variant instead
107  void get(vnl_matrix<T>* M) const;
108  //: Fill M with contents of the 3x3 homography matrix
109  void get(T *M) const;
110  //: Return an element from the 3x3 homography matrix
111  T get(unsigned int row_index, unsigned int col_index) const;
112  //: Return the inverse homography
114 
115  //: Set an element of the 3x3 homography matrix
116  vgl_h_matrix_2d& set (unsigned int row_index, unsigned int col_index, T value)
117  { t12_matrix_[row_index][col_index]=value; return *this; }
118 
119  //: Set to 3x3 row-stored matrix
120  vgl_h_matrix_2d& set(T const* M);
121  //: Set to given 3x3 matrix
122  vgl_h_matrix_2d& set(vnl_matrix_fixed<T,3,3> const& M);
123 
124  // various affine transformations that set the corresponding parts of the matrix
125 
126  //: initialize the transformation to identity
128 
129  //: set H[0][2] = tx and H[1][2] = ty, other elements unaltered
130  vgl_h_matrix_2d& set_translation(T tx, T ty);
131 
132  //: the upper 2x2 part of the matrix is replaced by a rotation matrix.
133  // rotation angle theta is in radians
134  vgl_h_matrix_2d& set_rotation(T theta);
135 
136  //: compose the current transform with a uniform scaling transformation, S.
137  // $S = \left[ \begin{array}{ccc}
138  // s & 0 & 0 \\%
139  // 0 & s & 0 \\%
140  // 0 & 0 & 1
141  // \end{array}\right]$ , Ts = S*T.
142  vgl_h_matrix_2d& set_scale(T scale);
143 
144  //: set the transform to a similarity mapping
145  // Sim $ = \left[\begin{array}{ccc}
146  // \cos(\theta) & -\sin(\theta) & tx \\%
147  // \sin(\theta) & \cos(\theta) & ty \\%
148  // 0 & 0 & 1
149  // \end{array}\right]$
150  vgl_h_matrix_2d& set_similarity(T s, T theta, T tx, T ty);
151 
152  //: compose the transform with diagonal aspect ratio transform.
153  // $A = \left[ \begin{array}{ccc}
154  // 1 & 0 & 0 \\%
155  // 0 & a & 0 \\%
156  // 0 & 0 & 1
157  // \end{array}\right]$ , Ta = A*T.
158  vgl_h_matrix_2d& set_aspect_ratio(T aspect_ratio);
159 
160 
161  //: set the transform to a general affine transform matrix
162  // $A = \left[ \begin{array}{ccc}
163  // a00 & a01 & a02 \\%
164  // a10 & a11 & a12 \\%
165  // 0 & 0 & 1
166  // \end{array}\right]$
167  vgl_h_matrix_2d& set_affine(vnl_matrix_fixed<T,2,3> const& M23);
168  //:
169  // \deprecated use the vnl_matrix_fixed variant instead
170  vgl_h_matrix_2d& set_affine(vnl_matrix<T> const& M23);
171 
172  bool is_rotation() const;
173  bool is_euclidean() const;
174  bool is_identity() const;
175 
176  //: transformation to projective basis (canonical frame)
177  // Compute the homography that takes the input set of points to the
178  // canonical frame. The points act as the projective basis for
179  // the canonical coordinate system. In the canonical frame the points
180  // have coordinates:
181  // $\begin{array}{cccc}
182  // p[0] & p[1] & p[2] & p[3] \\%
183  // 1 & 0 & 0 & 1 \\%
184  // 0 & 1 & 0 & 1 \\%
185  // 0 & 0 & 1 & 1
186  // \end{array}$
187  bool projective_basis(std::vector<vgl_homg_point_2d<T> > const& four_points);
188 
189  //: transformation to projective basis (canonical frame)
190  // Compute the homography that takes the input set of lines to the canonical
191  // frame. The lines act as the dual projective basis for the canonical
192  // coordinate system. In the canonical frame the lines have equations:
193  // x=0; y=0; w=0; x+y+w=0. (The third line is the line at infinity.)
194  bool projective_basis(std::vector<vgl_homg_line_2d<T> > const& four_lines
195  );
196 
197  // ---------- extract components as transformations ----------
198 
199  //: corresponds to rotation for Euclidean transformations
201  //: corresponds to rotation for Euclidean transformations
202  vnl_matrix_fixed<T,2,2> get_upper_2x2_matrix() const;
203 
204  //: corresponds to translation for affine transformations
206  //: corresponds to translation for affine transformations
207  vnl_vector_fixed<T,2> get_translation_vector() const;
208 
209  //: Read H from std::istream
210  bool read(std::istream& s);
211  //: Read H from file
212  bool read(char const* filename);
213 };
214 
215 //: Print H on std::ostream
216 template <class T> std::ostream& operator<<(std::ostream& s, vgl_h_matrix_2d<T> const& H);
217 //: Read H from std::istream
218 template <class T> std::istream& operator>>(std::istream& s, vgl_h_matrix_2d<T>& H)
219 { H.read(s); return s; }
220 
221 #define VGL_H_MATRIX_2D_INSTANTIATE(T) extern "please include vgl/algo/vgl_h_matrix_2d.hxx first"
222 
223 #endif // vgl_h_matrix_2d_h_
A quadratic plane curve.
A class to hold a plane-to-plane projective transformation matrix and to perform common operations us...
Definition: vgl_algo_fwd.h:11
vnl_matrix_fixed< T, 3, 3 > const & get_matrix() const
Return the 3x3 homography matrix.
bool read(std::istream &s)
Read H from std::istream.
bool operator==(vgl_h_matrix_2d< T > const &M) const
Represents a homogeneous 2D line.
Definition: vgl_fwd.h:14
vgl_h_matrix_2d(vgl_h_matrix_2d< T > const &M)
Copy constructor.
vgl_h_matrix_2d & set_aspect_ratio(T aspect_ratio)
compose the transform with diagonal aspect ratio transform.
vgl_h_matrix_2d< T > get_upper_2x2() const
corresponds to rotation for Euclidean transformations.
bool is_identity() const
vgl_homg_point_2d< T > operator *(vgl_homg_point_2d< T > const &p) const
Return the transformed point given by $q = {\tt H} p$.
vgl_h_matrix_2d & set_affine(vnl_matrix_fixed< T, 2, 3 > const &M23)
set the transform to a general affine transform matrix.
vgl_h_matrix_2d & set(unsigned int row_index, unsigned int col_index, T value)
Set an element of the 3x3 homography matrix.
vnl_matrix_fixed< T, 3, 3 > t12_matrix_
vgl_h_matrix_2d & set_rotation(T theta)
the upper 2x2 part of the matrix is replaced by a rotation matrix.
bool is_euclidean() const
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
line in projective 2D space
vgl_homg_point_2d< T > operator()(vgl_homg_point_2d< T > const &p) const
Return the transformed point given by $q = {\tt H} p$.
vnl_matrix_fixed< T, 2, 2 > get_upper_2x2_matrix() const
corresponds to rotation for Euclidean transformations.
vgl_h_matrix_2d & set_similarity(T s, T theta, T tx, T ty)
set the transform to a similarity mapping.
A quadratic plane curve.
Definition: vgl_conic.h:70
point in projective 2D space
vgl_homg_line_2d< T > preimage(vgl_homg_line_2d< T > const &l) const
Return the transformed line given by $m = {\tt H} l$.
vnl_vector_fixed< T, 2 > get_translation_vector() const
corresponds to translation for affine transformations.
void get(vnl_matrix_fixed< T, 3, 3 > *M) const
Fill M with contents of the 3x3 homography matrix.
std::istream & operator>>(std::istream &is, vgl_orient_box_3d< Type > &p)
Read box from stream.
vgl_homg_line_2d< T > correlation(vgl_homg_point_2d< T > const &p) const
#define l
~vgl_h_matrix_2d()=default
vgl_h_matrix_2d & set_translation(T tx, T ty)
set H[0][2] = tx and H[1][2] = ty, other elements unaltered.
bool is_rotation() const
vgl_homg_point_2d< T > get_translation() const
corresponds to translation for affine transformations.
vgl_h_matrix_2d get_inverse() const
Return the inverse homography.
vgl_h_matrix_2d & set_scale(T scale)
compose the current transform with a uniform scaling transformation, S.
vgl_h_matrix_2d(T const *M)
Constructor from 3x3 C-array.
vgl_h_matrix_2d()=default
Represents a homogeneous 2D point.
Definition: vgl_fwd.h:8
bool projective_basis(std::vector< vgl_homg_point_2d< T > > const &four_points)
transformation to projective basis (canonical frame).
vgl_h_matrix_2d & set_identity()
initialize the transformation to identity.
vgl_h_matrix_2d(vnl_matrix_fixed< T, 3, 3 > const &M)
Constructor from a 3x3 matrix, and implicit cast from vnl_matrix_fixed<T,3,3>.