9 # include <vcl_msvc_warnings.h> 12 #include <vnl/vnl_inverse.h> 13 #include <vnl/algo/vnl_levenberg_marquardt.h> 31 vnl_vector<double> hv(9);
32 vnl_matrix_fixed<double,3,3> m = h_initial.
get_matrix();
34 for (
unsigned r=0; r<3; ++r)
35 for (
unsigned c=0; c<3; ++c, ++i)
37 vnl_levenberg_marquardt lm(lsq);
40 lm.set_x_tolerance(
htol_);
41 lm.set_f_tolerance(
ftol_);
42 lm.set_g_tolerance(
gtol_);
43 bool success = lm.minimize(hv);
46 lm.diagnose_outcome(std::cout);
50 h_optimized.
set(hv.data_block());
52 h_optimized = h_initial;
62 assert(points1.size() == points2.size());
63 int n = points1.size();
73 std::vector<vgl_homg_point_2d<double> > tpoints1, tpoints2;
74 for (
int i=0; i<n; ++i)
76 tpoints1.push_back(tr1(points1[i]));
77 tpoints2.push_back(tr2(points2[i]));
91 if (!
optimize_h(tpoints1, tpoints2, initial_h_norm, hopt))
102 H = tr2_inv*hopt*tr1;
112 assert(lines1.size() == lines2.size());
113 assert(lines1.size() > 4);
118 if (!tr1.compute_from_lines(lines1))
120 if (!tr2.compute_from_lines(lines2))
122 std::vector<vgl_homg_point_2d<double> > tlines1, tlines2;
123 for (
const auto & lit : lines1)
129 tlines1.push_back(p);
131 for (
const auto & lit : lines2)
137 tlines2.push_back(p);
149 vnl_matrix_fixed<double, 3, 3>
const & Mp_init =
151 vnl_matrix_fixed<double, 3, 3> Ml_init = vnl_inverse_transpose(Mp_init);
152 h_initial_line.
set(Ml_init);
155 if (!this->
optimize_h(tlines1, tlines2, h_initial_line, h_line_opt))
160 vnl_matrix_fixed<double, 3, 3>
const & Ml_opt = h_line_opt.
get_matrix();
161 vnl_matrix_fixed<double, 3, 3> Mp_opt = vnl_inverse_transpose(Ml_opt);
162 h_point_opt.
set(Mp_opt);
172 H = tr2inv*h_point_opt*tr1;
184 assert(points1.size() == points2.size());
185 int np = points1.size();
187 assert(lines1.size() == lines2.size());
188 int nl = lines1.size();
197 std::vector<vgl_homg_point_2d<double> > tpoints1, tpoints2;
198 for (
int i=0; i<np; ++i)
200 tpoints1.push_back(tr1(points1[i]));
201 tpoints2.push_back(tr2(points2[i]));
203 for (
int i=0; i<nl; ++i)
205 double a=lines1[i].a(), b=lines1[i].b(), c=lines1[i].c(), d=std::sqrt(a*a+b*b);
207 a=lines2[i].a(), b=lines2[i].b(), c=lines2[i].c(), d = std::sqrt(a*a+b*b);
216 if (!
optimize_h(tpoints1, tpoints2, initial_h_norm, hopt))
228 H = tr2_inv*hopt*tr1;
vnl_matrix_fixed< T, 3, 3 > const & get_matrix() const
Return the 3x3 homography matrix.
vgl_h_matrix_2d< double > initial_h_
contains class vgl_h_matrix_2d_optimize_lmq
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.
bool optimize_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.
vgl_h_matrix_2d_optimize_lmq(vgl_h_matrix_2d< double > const &initial_h)
Constructor from initial homography to be optimized.
The similarity transform that normalizes a point set.
bool optimize_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.
bool optimize_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.
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 compute_from_points(std::vector< vgl_homg_point_2d< T > > const &points, bool isotropic=true)
compute the normalizing transform.
vgl_h_matrix_2d get_inverse() const
Return the inverse homography.
Represents a homogeneous 2D point.
bool optimize_h(std::vector< vgl_homg_point_2d< double > > const &points1, std::vector< vgl_homg_point_2d< double > > const &points2, vgl_h_matrix_2d< double > const &h_initial, vgl_h_matrix_2d< double > &h_optimized)
the main routine for carrying out the optimization. (used by the others).