2 #ifndef vgl_h_matrix_1d_hxx_ 3 #define vgl_h_matrix_1d_hxx_ 10 #include <vnl/vnl_inverse.h> 11 #include <vnl/vnl_vector_fixed.h> 12 #include <vnl/algo/vnl_svd.h> 13 #include <vcl_compiler_detection.h> 15 # include <vcl_msvc_warnings.h> 18 # include <vcl_deprecated.h> 24 t12_matrix_.read_ascii(is);
32 assert(points1.size() == points2.size());
33 unsigned int numpoints = points1.size();
36 std::cerr <<
"\nvhl_h_matrix_1d - minimum of 3 points required\n";
40 W.set_size(numpoints, 4);
42 for (
unsigned int i = 0; i < numpoints; i++)
44 T x1 = points1[i].x(), w1 = points1[i].w();
45 T x2 = points2[i].x(), w2 = points2[i].w();
47 W[i][0]=x1*w2; W[i][1]=w1*w2;
48 W[i][2]=-x1*x2; W[i][3]=-w1*x2;
52 t12_matrix_ = vnl_matrix_fixed<T,2,2>(SVD.nullvector().data_block());
61 vnl_vector_fixed<T,2>
v = t12_matrix_ * vnl_vector_fixed<T,2>(p.
x(),p.
w());
69 vnl_vector_fixed<T,2>
v = vnl_inverse(t12_matrix_) * vnl_vector_fixed<T,2>(q.
x(),q.
w());
90 t12_matrix_.read_ascii(s);
91 return s.good() || s.eof();
98 std::ifstream f(filename);
100 std::cerr <<
"vgl_h_matrix_1d::read: Error opening " << filename << std::endl;
102 t12_matrix_.read_ascii(f);
108 std::ifstream f(filename);
110 std::cerr <<
"vgl_h_matrix_1d::read: Error opening " << filename << std::endl;
120 return t12_matrix_.get(row_index, col_index);
126 for (T
const* iter = t12_matrix_.begin(); iter < t12_matrix_.end(); ++iter)
139 VXL_DEPRECATED_MACRO(
"vgl_h_matrix_1d<T>::get(vnl_matrix<T>*) const");
140 *H = t12_matrix_.as_ref();
147 for (T* iter = t12_matrix_.begin(); iter < t12_matrix_.end(); ++iter)
172 t12_matrix_[0][0]*=scale;
173 t12_matrix_[0][1]*=scale;
181 t12_matrix_[0][1] = tx;
189 for (
unsigned r = 0; r<1; ++r)
190 for (
unsigned c = 0; c<2; ++c)
191 t12_matrix_[r][c] = M12[r][c];
192 t12_matrix_[1][0] = T(0); t12_matrix_[1][1] = T(1);
200 if (points.size()!=3)
202 vnl_vector_fixed<T,2> p0(points[0].x(), points[0].w());
203 vnl_vector_fixed<T,2> p1(points[1].x(), points[1].w());
204 vnl_vector_fixed<T,2> p2(points[2].x(), points[2].w());
205 vnl_matrix_fixed<T,2,3> point_matrix;
206 point_matrix.set_column(0, p0);
207 point_matrix.set_column(1, p1);
208 point_matrix.set_column(2, p2);
210 if (! point_matrix.is_finite() || point_matrix.has_nans())
212 std::cerr <<
"vgl_h_matrix_1d<T>::projective_basis():\n" 213 <<
" given points have infinite or NaN values\n";
214 this->set_identity();
217 vnl_svd<T> svd1(point_matrix.as_ref(), 1e-8);
220 std::cerr <<
"vgl_h_matrix_1d<T>::projective_basis():\n" 221 <<
" At least two out of the three points are nearly identical\n";
222 this->set_identity();
226 vnl_matrix_fixed<T,2,2> back_matrix;
227 back_matrix.set_column(0, p0);
228 back_matrix.set_column(1, p1);
230 vnl_vector_fixed<T,2> scales_vector = vnl_inverse(back_matrix) * p2;
232 back_matrix.set_column(0, scales_vector[0] * p0);
233 back_matrix.set_column(1, scales_vector[1] * p1);
235 if (! back_matrix.is_finite() || back_matrix.has_nans())
237 std::cerr <<
"vgl_h_matrix_1d<T>::projective_basis():\n" 238 <<
" back matrix has infinite or NaN values\n";
239 this->set_identity();
242 this->set(vnl_inverse(back_matrix));
249 return t12_matrix_.is_identity();
256 return t12_matrix_.is_identity();
264 return t12_matrix_.get(0,0) == (T)1 &&
265 t12_matrix_.get(1,0) == (T)0 &&
266 t12_matrix_.get(1,1) == (T)1 ;
271 #undef VGL_H_MATRIX_1D_INSTANTIATE 272 #define VGL_H_MATRIX_1D_INSTANTIATE(T) \ 273 template class vgl_h_matrix_1d<T >; \ 274 template std::ostream& operator << (std::ostream& s, vgl_h_matrix_1d<T > const& h); \ 275 template std::istream& operator >> (std::istream& s, vgl_h_matrix_1d<T >& h) 277 #endif // vgl_h_matrix_1d_hxx_
std::istream & read(std::istream &is)
Read from stream, possibly with formatting.
2x2 line-to-line projectivity
A class to hold a line-to-line projective transformation matrix and to perform common operations usin...
a point in homogeneous 1-D space, i.e., a homogeneous pair (x,w)
vgl_h_matrix_1d & set_translation(T tx)
set H[0][1] = tx, other elements unaltered.
vgl_h_matrix_1d & set_scale(T scale)
compose the current transform with a uniform scaling transformation, S.
bool is_euclidean() const
std::ostream & operator<<(std::ostream &s, vgl_orient_box_3d< Type > const &p)
Write box to stream.
void get(vnl_matrix_fixed< T, 2, 2 > *M) const
Fill M with contents of the 2x2 homography matrix.
bool projective_basis(std::vector< vgl_homg_point_1d< T > > const &three_points)
transformation to projective basis (canonical frame).
vgl_h_matrix_1d & set_affine(vnl_matrix_fixed< T, 1, 2 > const &M12)
set the transform to a general affine transform matrix.
vgl_homg_point_1d< T > operator()(vgl_homg_point_1d< T > const &p) const
Return the transformed point given by $q = {\tt H} p$.
vgl_h_matrix_1d & set(T const *M)
Set to 2x2 row-stored matrix.
vnl_matrix_fixed< T, 2, 2 > const & get_matrix() const
Return the 2x2 homography matrix.
bool read(char const *filename)
Read H from file.
vgl_h_matrix_1d & set_identity()
initialize the transformation to identity.
vgl_h_matrix_1d get_inverse() const
Return the inverse homography.
vgl_homg_point_1d< T > preimage(vgl_homg_point_1d< T > const &q) const
Return the transformed point given by $p = {\tt H}^{-1} q$.
Represents a homogeneous 1-D point, i.e., a homogeneous pair (x,w).
vgl_h_matrix_1d()=default