59 landmark_points.clear();
62 for (TSequenceFeatureObservations::const_iterator it = observations.begin();
63 it != observations.end(); ++it)
68 frame_poses[frame_ID] =
CPose3D(0, 0, 0, 0, 0, 0);
69 landmark_points[feat_ID] =
TPoint3D(0, 0, 1);
86 landmark_points.clear();
88 if (observations.empty())
return;
95 for (TSequenceFeatureObservations::const_iterator it = observations.begin();
96 it != observations.end(); ++it)
105 frame_poses.assign(max_fr_id + 1,
CPose3D(0, 0, 0, 0, 0, 0));
106 landmark_points.assign(max_pt_id + 1,
TPoint3D(0, 0, 1));
115 template <
bool POSES_INVERSE>
118 std::array<double, 2>& out_residual,
119 const TFramePosesVec::value_type& frame,
120 const TLandmarkLocationsVec::value_type& point,
double&
sum,
121 const bool use_robust_kernel,
const double kernel_param,
122 double* out_kernel_1st_deriv)
125 mrpt::vision::pinhole::projectPoint_no_distortion<POSES_INVERSE>(
126 camera_params, frame, point);
129 out_residual[0] = z_meas.x - z_pred.
x;
130 out_residual[1] = z_meas.y - z_pred.
y;
132 const double sum_2 =
square(out_residual[0]) +
square(out_residual[1]);
134 if (use_robust_kernel)
137 kernel.param_sq =
square(kernel_param);
138 double kernel_1st_deriv, kernel_2nd_deriv;
140 sum += kernel.eval(sum_2, kernel_1st_deriv, kernel_2nd_deriv);
141 if (out_kernel_1st_deriv) *out_kernel_1st_deriv = kernel_1st_deriv;
159 std::vector<std::array<double, 2>>& out_residuals,
160 const bool frame_poses_are_inverse,
const bool use_robust_kernel,
161 const double kernel_param, std::vector<double>* out_kernel_1st_deriv)
167 const size_t N = observations.size();
168 out_residuals.resize(N);
169 if (out_kernel_1st_deriv) out_kernel_1st_deriv->resize(N);
171 for (
size_t i = 0; i < N; i++)
178 TFramePosesMap::const_iterator itF = frame_poses.find(i_f);
179 TLandmarkLocationsMap::const_iterator itP = landmark_points.find(i_p);
180 ASSERTMSG_(itF != frame_poses.end(),
"Frame ID is not in list!");
181 ASSERTMSG_(itP != landmark_points.end(),
"Landmark ID is not in list!");
183 const TFramePosesMap::mapped_type& frame = itF->second;
184 const TLandmarkLocationsMap::mapped_type& point = itP->second;
186 double* ptr_1st_deriv =
187 out_kernel_1st_deriv ? &((*out_kernel_1st_deriv)[i]) :
nullptr;
189 if (frame_poses_are_inverse)
190 reprojectionResidualsElement<true>(
191 camera_params, OBS, out_residuals[i], frame, point,
sum,
192 use_robust_kernel, kernel_param, ptr_1st_deriv);
194 reprojectionResidualsElement<false>(
195 camera_params, OBS, out_residuals[i], frame, point,
sum,
196 use_robust_kernel, kernel_param, ptr_1st_deriv);
208 std::vector<std::array<double, 2>>& out_residuals,
209 const bool frame_poses_are_inverse,
const bool use_robust_kernel,
210 const double kernel_param, std::vector<double>* out_kernel_1st_deriv)
216 const size_t N = observations.size();
217 out_residuals.resize(N);
218 if (out_kernel_1st_deriv) out_kernel_1st_deriv->resize(N);
220 for (
size_t i = 0; i < N; i++)
229 const TFramePosesVec::value_type& frame = frame_poses[i_f];
230 const TLandmarkLocationsVec::value_type& point = landmark_points[i_p];
232 double* ptr_1st_deriv =
233 out_kernel_1st_deriv ? &((*out_kernel_1st_deriv)[i]) :
nullptr;
235 if (frame_poses_are_inverse)
236 reprojectionResidualsElement<true>(
237 camera_params, OBS, out_residuals[i], frame, point,
sum,
238 use_robust_kernel, kernel_param, ptr_1st_deriv);
240 reprojectionResidualsElement<false>(
241 camera_params, OBS, out_residuals[i], frame, point,
sum,
242 use_robust_kernel, kernel_param, ptr_1st_deriv);
252 const vector<std::array<double, 2>>& residual_vec,
258 const size_t num_fix_points,
const vector<double>* kernel_1st_deriv)
262 const size_t N = observations.size();
263 const bool use_robust_kernel = (kernel_1st_deriv !=
nullptr);
265 for (
size_t i = 0; i < N; i++)
276 if (i_f >= num_fix_frames)
278 const size_t frame_id = i_f - num_fix_frames;
282 JtJ.matProductOf_AtA(JACOB.
J_frame);
287 if (!use_robust_kernel)
289 eps_frame[frame_id] += eps_delta;
293 const double rho_1st_der = (*kernel_1st_deriv)[i];
294 auto scaled_eps = eps_delta;
295 scaled_eps *= rho_1st_der;
296 eps_frame[frame_id] += scaled_eps;
300 if (i_p >= num_fix_points)
302 const size_t point_id = i_p - num_fix_points;
306 JtJ.matProductOf_AtA(JACOB.
J_point);
311 if (!use_robust_kernel)
313 eps_point[point_id] += eps_delta;
317 const double rho_1st_der = (*kernel_1st_deriv)[i];
318 auto scaled_eps = eps_delta;
319 scaled_eps *= rho_1st_der;
320 eps_point[point_id] += scaled_eps;
332 const size_t delta_first_idx,
const size_t delta_num_vals,
337 new_frame_poses.resize(frame_poses.size());
339 for (
size_t i = 0; i < num_fix_frames; ++i)
340 new_frame_poses[i] = frame_poses[i];
342 size_t delta_used_vals = 0;
343 const double* delta_val = &delta[delta_first_idx];
345 for (
size_t i = num_fix_frames; i < frame_poses.size(); i++)
347 const CPose3D& old_pose = frame_poses[i];
348 CPose3D& new_pose = new_frame_poses[i];
360 delta_used_vals += 6;
363 ASSERT_(delta_used_vals == delta_num_vals);
368 const size_t delta_first_idx,
const size_t delta_num_vals,
373 new_landmark_points.resize(landmark_points.size());
375 for (
size_t i = 0; i < num_fix_points; ++i)
376 new_landmark_points[i] = landmark_points[i];
378 size_t delta_used_vals = 0;
379 const double* delta_val = &delta[delta_first_idx];
381 for (
size_t i = num_fix_points; i < landmark_points.size(); i++)
383 const TPoint3D& old_point = landmark_points[i];
384 TPoint3D& new_point = new_landmark_points[i];
386 for (
size_t j = 0; j < 3; j++)
387 new_point[j] = old_point[j] + delta_val[j];
391 delta_used_vals += 3;
394 ASSERT_(delta_used_vals == delta_num_vals);
TLandmarkID id_feature
A unique ID of this feature.
A compile-time fixed-size numeric matrix container.
void reprojectionResidualsElement(const TCamera &camera_params, const TFeatureObservation &OBS, std::array< double, 2 > &out_residual, const TFramePosesVec::value_type &frame, const TLandmarkLocationsVec::value_type &point, double &sum, const bool use_robust_kernel, const double kernel_param, double *out_kernel_1st_deriv)
uint64_t TCameraPoseID
Unique IDs for camera frames (poses)
uint64_t TFeatureID
Definition of a feature ID.
mrpt::math::CMatrixFixed< double, ObsDim, PointDof > J_point
#define ASSERT_BELOW_(__A, __B)
One feature observation entry, used within sequences with TSequenceFeatureObservations.
A pair (x,y) of pixel coordinates (subpixel resolution).
void ba_initial_estimate(const mrpt::vision::TSequenceFeatureObservations &observations, const mrpt::img::TCamera &camera_params, mrpt::vision::TFramePosesVec &frame_poses, mrpt::vision::TLandmarkLocationsVec &landmark_points)
Fills the frames & landmark points maps with an initial gross estimate from the sequence observations...
void add_se3_deltas_to_frames(const mrpt::vision::TFramePosesVec &frame_poses, const mrpt::math::CVectorDouble &delta, const size_t delta_first_idx, const size_t delta_num_vals, mrpt::vision::TFramePosesVec &new_frame_poses, const size_t num_fix_frames)
For each pose in the vector frame_poses, adds a "delta" increment to the manifold, with the "delta" given in the se(3) Lie algebra:
A complete sequence of observations of features from different camera frames (poses).
#define ASSERT_(f)
Defines an assertion mechanism.
std::vector< mrpt::poses::CPose3D > TFramePosesVec
A list of camera frames (6D poses), which assumes indexes are unique, consecutive IDs...
This base provides a set of functions for maths stuff.
void composeFrom(const CPose3D &A, const CPose3D &B)
Makes "this = A (+) B"; this method is slightly more efficient than "this= A + B;" since it avoids th...
void add_3d_deltas_to_points(const mrpt::vision::TLandmarkLocationsVec &landmark_points, const mrpt::math::CVectorDouble &delta, const size_t delta_first_idx, const size_t delta_num_vals, mrpt::vision::TLandmarkLocationsVec &new_landmark_points, const size_t num_fix_points)
For each pose in the vector frame_poses, adds a "delta" increment to the manifold, with the "delta" given in the se(3) Lie algebra:
std::map< TLandmarkID, mrpt::math::TPoint3D > TLandmarkLocationsMap
A list of landmarks (3D points) indexed by unique IDs.
CONTAINER::Scalar sum(const CONTAINER &v)
Computes the sum of all the elements.
Classes for computer vision, detectors, features, etc.
Parameters for the Brown-Conrady camera lens distortion model.
TPoint3D_< double > TPoint3D
Lightweight 3D point.
#define ASSERTMSG_(f, __ERROR_MSG)
Defines an assertion mechanism.
double reprojectionResiduals(const mrpt::vision::TSequenceFeatureObservations &observations, const mrpt::img::TCamera &camera_params, const mrpt::vision::TFramePosesVec &frame_poses, const mrpt::vision::TLandmarkLocationsVec &landmark_points, std::vector< std::array< double, 2 >> &out_residuals, const bool frame_poses_are_inverse, const bool use_robust_kernel=true, const double kernel_param=3.0, std::vector< double > *out_kernel_1st_deriv=nullptr)
Compute reprojection error vector (used from within Bundle Adjustment methods, but can be used in gen...
Classes for 2D/3D geometry representation, both of single values and probability density distribution...
mrpt::img::TPixelCoordf px
The pixel coordinates of the observed feature.
void keep_max(T &var, const K test_val)
If the second argument is above the first one, set the first argument to this higher value...
Traits for SE(n), rigid-body transformations in R^n space.
return_t square(const num_t x)
Inline function for the square of a number.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
A class used to store a 3D pose (a 3D translation + a rotation in 3D).
std::map< TCameraPoseID, mrpt::poses::CPose3D > TFramePosesMap
A list of camera frames (6D poses) indexed by unique IDs.
#define ASSERTDEB_(f)
Defines an assertion mechanism - only when compiled in debug.
void ba_build_gradient_Hessians(const TSequenceFeatureObservations &observations, const std::vector< std::array< double, 2 >> &residual_vec, const std::vector< JacData< 6, 3, 2 >> &jac_data_vec, std::vector< mrpt::math::CMatrixFixed< double, 6, 6 >> &U, std::vector< CVectorFixedDouble< 6 >> &eps_frame, std::vector< mrpt::math::CMatrixFixed< double, 3, 3 >> &V, std::vector< CVectorFixedDouble< 3 >> &eps_point, const size_t num_fix_frames, const size_t num_fix_points, const vector< double > *kernel_1st_deriv)
Construct the BA linear system.
mrpt::math::CMatrixFixed< double, ObsDim, FrameDof > J_frame
TCameraPoseID id_frame
A unique ID of a "frame" (camera position) from where the feature was observed.
std::vector< mrpt::math::TPoint3D > TLandmarkLocationsVec
A list of landmarks (3D points), which assumes indexes are unique, consecutive IDs.