18 # include <vcl_msvc_warnings.h> 24 use_m_estimator_(false),
25 m_estimator_scale_(1.0),
27 self_calibrate_(false),
28 normalize_data_(true),
30 max_iterations_(1000),
48 double& nx,
double& ny,
double& ns)
51 for (
auto & image_point : image_points)
53 double x = image_point.x();
54 double y = image_point.y();
59 nx /= image_points.size();
60 ny /= image_points.size();
61 ns /= image_points.size();
65 for (
auto & image_point : image_points)
67 image_point.x() -= nx;
68 image_point.y() -= ny;
69 image_point.x() /= ns;
70 image_point.y() /= ns;
83 for (
auto & point : points)
96 vnl_double_3 r(axis.
x(), axis.
y(), axis.
z());
101 for (
auto & c : cameras)
103 c.set_camera_center(R*c.get_camera_center());
104 c.set_rotation(R2*c.get_rotation()*R);
119 vnl_double_3 pc(0.0,0.0,0.0), cc(0.0,0.0,0.0);
121 for (
auto & point : points)
123 pc += vnl_double_3(point.x(), point.y(), point.z());
129 for (
auto & camera : cameras)
132 cc += vnl_double_3(c.
x(), c.
y(), c.
z());
134 cc /= cameras.size();
152 const std::vector<std::vector<bool> >& mask)
156 double nx=0.0, ny=0.0, ns=1.0;
157 std::vector<vgl_point_2d<double> > norm_image_points(image_points);
165 vpgl_ba_shared_k_lsqr::create_param_vector(cameras,
a_,
c_);
167 b_ = vpgl_ba_shared_k_lsqr::create_param_vector(world_points);
170 for (
auto & camera : cameras){
176 K_vals[4] += Ki.
skew();
178 K_vals /= cameras.
size();
184 ba_func_ =
new vpgl_ba_shared_k_lsqr(K,norm_image_points,mask);
189 std::vector<vpgl_calibration_matrix<double> > K;
192 for (
auto & camera : cameras){
198 pp.
x() = (pp.
x()-nx)/ns;
199 pp.
y() = (pp.
y()-ny)/ns;
232 weights_.resize(image_points.size(),1.0);
236 std::cout <<
"final focal length = "<<
c_[0]*ns<<std::endl;
243 for (
unsigned int i=0; i<cameras.size(); ++i)
252 pp.
x() = ns*pp.
x() + nx;
253 pp.
y() = ns*pp.
y() + ny;
255 cameras[i].set_calibration(K);
259 for (
unsigned int j=0; j<world_points.size(); ++j)
272 std::ofstream os(filename.c_str());
273 os <<
"#VRML V2.0 utf8\n\n";
278 for (
unsigned int i=0; i<cameras.size(); ++i){
283 double fov = 2.0*std::max(std::atan(K[1][2]/K[1][1]), std::atan(K[0][2]/K[0][0]));
284 os <<
"Viewpoint {\n" 285 <<
" position "<< ctr.
x() <<
' ' << ctr.
y() <<
' ' << ctr.
z() <<
'\n' 286 <<
" orientation "<< R.
axis() <<
' '<< R.
angle() <<
'\n' 287 <<
" fieldOfView "<< fov <<
'\n' 288 <<
" description \"Camera" << i <<
"\"\n}\n";
291 os <<
"Shape {\n appearance NULL\n geometry PointSet {\n" 292 <<
" color Color { color [1 0 0] }\n coord Coordinate{\n" 295 for (
const auto & world_point : world_points){
296 os << world_point.x() <<
' ' 297 << world_point.y() <<
' ' 298 << world_point.z() <<
'\n';
300 os <<
" ]\n }\n }\n}\n";
void reflect_points(const vgl_plane_3d< double > &plane, std::vector< vgl_point_3d< double > > &points)
vgl_point_2d< T > principal_point() const
int get_num_iterations() const
double m_estimator_scale_
double get_end_error() const
void normalize_points(std::vector< vgl_point_2d< double > > &image_points, double &nx, double &ny, double &ns)
normalize image points to be mean centered with scale sqrt(2).
vpgl_bundle_adjust()
Constructor.
void set_verbose(bool verb)
bool optimize(std::vector< vpgl_perspective_camera< double > > &cameras, std::vector< vgl_point_3d< double > > &world_points, const std::vector< vgl_point_2d< double > > &image_points, const std::vector< std::vector< bool > > &mask)
Bundle Adjust.
vnl_vector< double > a_
The last camera parameters.
static void write_vrml(const std::string &filename, const std::vector< vpgl_perspective_camera< double > > &cameras, const std::vector< vgl_point_3d< double > > &world_points)
Write cameras and points to a file in VRML 2.0 for debugging.
void depth_reverse(std::vector< vpgl_perspective_camera< double > > &cameras, std::vector< vgl_point_3d< double > > &world_points)
Approximately depth invert the scene.
Bundle adjustment sparse least squares class for fixed intrinsics.
void set_g_tolerance(double v)
void set_principal_point(const vgl_point_2d< T > &new_principal_point)
std::vector< double > weights_
The last estimated weights.
vgl_homg_point_3d< double > param_to_point(int j, const vnl_vector< double > &b, const vnl_vector< double > &c) const
construct the
vnl_vector< double > b_
The last point parameters.
a class for bundle adjustment with fixed intrinsic parameters.
void set_max_function_evals(int v)
~vpgl_bundle_adjust()
Destructor.
This class implements the perspective camera class as described in Hartley & Zisserman as a finite ca...
void rotate_cameras(const vgl_vector_3d< double > &axis, std::vector< vpgl_perspective_camera< double > > &cameras)
void set_epsilon_function(double v)
static vnl_vector< double > create_param_vector(const std::vector< vpgl_perspective_camera< double > > &cameras)
Create the parameter vector a from a vector of cameras.
unsigned int max_iterations_
vnl_vector< double > c_
The last global parameters.
double get_start_error() const
Bundle adjustment sparse least squares functions.
vpgl_bundle_adjust_lsqr * ba_func_
The bundle adjustment error function.
const vnl_vector< double > & get_weights() const
void set_reflection_plane(vgl_plane_3d< double > const &p)
void set_focal_length(T new_focal_length)
Getters and setters for all of the parameters.
vnl_vector_fixed< T, 3 > axis() const
void set_residual_scale(double scale)
set the residual scale for the robust estimation.
bool minimize(vnl_vector< double > &a, vnl_vector< double > &b, vnl_vector< double > &c, bool use_gradient=true, bool use_weights=true)
vpgl_perspective_camera< double > param_to_cam(int i, const vnl_vector< double > &a, const vnl_vector< double > &c) const
construct the
void set_x_tolerance(double v)