9 # include <vcl_msvc_warnings.h> 12 #include <vnl/vnl_inverse.h> 13 #include <vnl/vnl_transpose.h> 14 #include <vnl/algo/vnl_svd.h> 20 allow_ideal_points_(allow_ideal_points)
62 for (
int i = 0; i < n; i++) {
63 D(row, 0) = p1[i].x() * p2[i].w();
64 D(row, 1) = p1[i].y() * p2[i].w();
65 D(row, 2) = p1[i].w() * p2[i].w();
69 D(row, 6) = -p1[i].x() * p2[i].x();
70 D(row, 7) = -p1[i].y() * p2[i].x();
71 D(row, 8) = -p1[i].w() * p2[i].x();
77 D(row, 3) = p1[i].x() * p2[i].w();
78 D(row, 4) = p1[i].y() * p2[i].w();
79 D(row, 5) = p1[i].w() * p2[i].w();
80 D(row, 6) = -p1[i].x() * p2[i].y();
81 D(row, 7) = -p1[i].y() * p2[i].y();
82 D(row, 8) = -p1[i].w() * p2[i].y();
86 D(row, 0) = p1[i].x() * p2[i].y();
87 D(row, 1) = p1[i].y() * p2[i].y();
88 D(row, 2) = p1[i].w() * p2[i].y();
89 D(row, 3) = -p1[i].x() * p2[i].x();
90 D(row, 4) = -p1[i].y() * p2[i].x();
91 D(row, 5) = -p1[i].w() * p2[i].x();
100 vnl_svd<double> svd(D);
106 std::cerr <<
"vgl_h_matrix_2d_compute_linear : design matrix has rank < 8\n" 107 <<
"vgl_h_matrix_2d_compute_linear : probably due to degenerate point configuration\n";
111 H.
set(svd.nullvector().data_block());
121 assert(points1.size() == points2.size());
122 int n = points1.size();
126 std::cerr <<
"vgl_h_matrix_2d_compute_linear: Need at least 4 matches.\n";
127 if (n == 0) std::cerr <<
"Could be std::vector setlength idiosyncrasies!\n";
136 std::vector<vgl_homg_point_2d<double> > tpoints1, tpoints2;
137 for (
int i = 0; i<n; i++)
139 tpoints1.push_back(tr1(points1[i]));
140 tpoints2.push_back(tr2(points2[i]));
164 assert(lines1.size() == lines2.size());
165 int n = lines1.size();
174 std::vector<vgl_homg_point_2d<double> > tlines1, tlines2;
175 for (
const auto & lit : lines1)
181 tlines1.push_back(p);
183 for (
const auto & lit : lines2)
189 tlines2.push_back(p);
197 vnl_matrix_fixed<double, 3, 3>
const & Ml = hl.
get_matrix();
198 vnl_matrix_fixed<double, 3, 3> Mp = vnl_inverse_transpose(Ml);
221 assert(points1.size() == points2.size());
222 int np = points1.size();
224 assert(lines1.size() == lines2.size());
225 int nl = lines1.size();
230 std::cerr <<
"vgl_h_matrix_2d_compute_linear: Need at least 4 matches.\n";
231 if (np+nl == 0) std::cerr <<
"Could be std::vector setlength idiosyncrasies!\n";
240 std::vector<vgl_homg_point_2d<double> > tpoints1, tpoints2;
241 for (
int i = 0; i<np; i++)
243 tpoints1.push_back(tr1(points1[i]));
244 tpoints2.push_back(tr2(points2[i]));
246 for (
int i = 0; i<nl; i++)
248 double a=lines1[i].a(), b=lines1[i].b(), c=lines1[i].c(), d=std::sqrt(a*a+b*b);
250 a=lines2[i].a(), b=lines2[i].b(), c=lines2[i].c(), d = std::sqrt(a*a+b*b);
282 std::vector<double>
const& w,
288 vnl_vector<double> two_w(2*Nc);
290 for (
int i = 0; i<Nc; i++, j+=2)
295 vnl_diag_matrix<double> W(two_w);
302 for (
int i = 0; i < Nc; i++)
304 D(row, 0) = l1[i].a() * l2[i].c();
305 D(row, 1) = l1[i].b() * l2[i].c();
306 D(row, 2) = l1[i].c() * l2[i].c();
310 D(row, 6) = -l1[i].a() * l2[i].a();
311 D(row, 7) = -l1[i].b() * l2[i].a();
312 D(row, 8) = -l1[i].c() * l2[i].a();
318 D(row, 3) = l1[i].a() * l2[i].c();
319 D(row, 4) = l1[i].b() * l2[i].c();
320 D(row, 5) = l1[i].c() * l2[i].c();
321 D(row, 6) = -l1[i].a() * l2[i].b();
322 D(row, 7) = -l1[i].b() * l2[i].b();
323 D(row, 8) = -l1[i].c() * l2[i].b();
326 M = vnl_transpose(D)*W*D;
328 vnl_svd<double> svd(D);
334 std::cerr <<
"vgl_h_matrix_2d_compute_linear : design matrix has rank < 8\n" 335 <<
"vgl_h_matrix_2d_compute_linear : probably due to degenerate point configuration\n";
339 H.
set(svd.nullvector().data_block());
346 std::vector<double>
const & weights,
350 assert(lines1.size() == lines2.size());
355 if (!tr1.compute_from_lines(lines1))
357 if (!tr2.compute_from_lines(lines2))
359 std::vector<vgl_homg_line_2d<double> > tlines1, tlines2;
360 for (
const auto & lit : lines1)
364 tlines1.push_back(
l);
366 for (
const auto & lit : lines2)
370 tlines2.push_back(
l);
378 vnl_matrix_fixed<double, 3, 3>
const & Ml = hl.
get_matrix();
380 if( vnl_det(Ml) == 0.0 )
382 vnl_matrix_fixed<double, 3, 3> Mp = vnl_inverse_transpose(Ml);
vnl_matrix_fixed< T, 3, 3 > const & get_matrix() const
Return the 3x3 homography matrix.
constexpr double DEGENERACY_THRESHOLD
Represents a homogeneous 2D line.
vgl_h_matrix_2d & set(unsigned int row_index, unsigned int col_index, T value)
Set an element of the 3x3 homography matrix.
contains class vgl_h_matrix_2d_compute_linear
The similarity transform that normalizes a point set.
bool compute_p(std::vector< vgl_homg_point_2d< double > > const &points1, std::vector< vgl_homg_point_2d< double > > const &points2, vgl_h_matrix_2d< double > &H) override
compute from matched points.
vgl_h_matrix_2d_compute_linear(bool allow_ideal_points=false)
Construct a vgl_h_matrix_2d_compute_linear object.
bool compute_from_lines(std::vector< vgl_homg_line_2d< T > > const &lines, bool isotropic=true)
The normalizing transform for lines is computed from the set of points defined by the intersection of...
bool solve_weighted_least_squares(std::vector< vgl_homg_line_2d< double > > const &l1, std::vector< vgl_homg_line_2d< double > > const &l2, std::vector< double > const &w, vgl_h_matrix_2d< double > &H)
for lines, the solution should be weighted by line length.
bool compute_from_points_and_lines(std::vector< vgl_homg_point_2d< T > > const &pts, std::vector< vgl_homg_line_2d< T > > const &lines, bool isotropic=true)
The normalizing transform for points and lines is computed from the set of points used by compute_fro...
bool solve_linear_problem(int equ_count, std::vector< vgl_homg_point_2d< double > > const &p1, std::vector< vgl_homg_point_2d< double > > const &p2, vgl_h_matrix_2d< double > &H)
Assumes all corresponding points have equal weight.
bool compute_from_points(std::vector< vgl_homg_point_2d< T > > const &points, bool isotropic=true)
compute the normalizing transform.
constexpr int TM_UNKNOWNS_COUNT
vgl_h_matrix_2d get_inverse() const
Return the inverse homography.
bool compute_pl(std::vector< vgl_homg_point_2d< double > > const &points1, std::vector< vgl_homg_point_2d< double > > const &points2, std::vector< vgl_homg_line_2d< double > > const &lines1, std::vector< vgl_homg_line_2d< double > > const &lines2, vgl_h_matrix_2d< double > &H) override
compute from matched points and lines.
Represents a homogeneous 2D point.
bool compute_l(std::vector< vgl_homg_line_2d< double > > const &lines1, std::vector< vgl_homg_line_2d< double > > const &lines2, vgl_h_matrix_2d< double > &H) override
compute from matched lines.