ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
odometry.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
9 
10 #include <Image.h>
11 #include <RGBDImage.h>
12 
16 #include "pybind/docstring.h"
17 
18 namespace cloudViewer {
19 namespace pipelines {
20 namespace odometry {
21 
22 template <class RGBDOdometryJacobianBase = RGBDOdometryJacobian>
23 class PyRGBDOdometryJacobian : public RGBDOdometryJacobianBase {
24 public:
25  using RGBDOdometryJacobianBase::RGBDOdometryJacobianBase;
27  int row,
28  std::vector<Eigen::Vector6d, utility::Vector6d_allocator> &J_r,
29  std::vector<double> &r,
30  std::vector<double> &w,
31  const geometry::RGBDImage &source,
32  const geometry::RGBDImage &target,
33  const geometry::Image &source_xyz,
34  const geometry::RGBDImage &target_dx,
35  const geometry::RGBDImage &target_dy,
36  const Eigen::Matrix3d &intrinsic,
37  const Eigen::Matrix4d &extrinsic,
38  const CorrespondenceSetPixelWise &corresps) const override {
39  PYBIND11_OVERLOAD_PURE(void, RGBDOdometryJacobianBase, row, J_r, r,
40  source, target, source_xyz, target_dx, target_dy,
41  extrinsic, corresps, intrinsic);
42  }
43 };
44 
45 void pybind_odometry_classes(py::module &m) {
46  // cloudViewer.odometry.OdometryOption
47  py::class_<OdometryOption> odometry_option(
48  m, "OdometryOption", "Class that defines Odometry options.");
49  odometry_option
50  .def(py::init(
51  [](std::vector<int> iteration_number_per_pyramid_level,
52  double max_depth_diff, double min_depth,
53  double max_depth) {
54  return new OdometryOption(
55  iteration_number_per_pyramid_level,
56  max_depth_diff, min_depth, max_depth);
57  }),
58  "iteration_number_per_pyramid_level"_a =
59  std::vector<int>{20, 10, 5},
60  "max_depth_diff"_a = 0.03, "min_depth"_a = 0.0,
61  "max_depth"_a = 4.0)
62  .def_readwrite("iteration_number_per_pyramid_level",
64  "List(int): Iteration number per image pyramid "
65  "level, typically larger image in the pyramid have "
66  "lower interation number to reduce computation "
67  "time.")
68  .def_readwrite("max_depth_diff", &OdometryOption::max_depth_diff_,
69  "Maximum depth difference to be considered as "
70  "correspondence. In depth image domain, if two "
71  "aligned pixels have a depth difference less than "
72  "specified value, they are considered as a "
73  "correspondence. Larger value induce more "
74  "aggressive search, but it is prone to unstable "
75  "result.")
76  .def_readwrite("min_depth", &OdometryOption::min_depth_,
77  "Pixels that has smaller than specified depth "
78  "values are ignored.")
79  .def_readwrite("max_depth", &OdometryOption::max_depth_,
80  "Pixels that has larger than specified depth values "
81  "are ignored.")
82  .def("__repr__", [](const OdometryOption &c) {
83  int num_pyramid_level =
85  std::string str_iteration_number_per_pyramid_level_ = "[ ";
86  for (int i = 0; i < num_pyramid_level; i++)
87  str_iteration_number_per_pyramid_level_ +=
90  ", ";
91  str_iteration_number_per_pyramid_level_ += "] ";
92  return std::string("OdometryOption class.") +
93  /*std::string("\nodo_init = ") +
94  std::to_string(c.odo_init_) +*/
95  std::string("\niteration_number_per_pyramid_level = ") +
96  str_iteration_number_per_pyramid_level_ +
97  std::string("\nmax_depth_diff = ") +
99  std::string("\nmin_depth = ") +
101  std::string("\nmax_depth = ") +
103  });
104 
105  // cloudViewer.odometry.RGBDOdometryJacobian
106  py::class_<RGBDOdometryJacobian,
108  jacobian(
109  m, "RGBDOdometryJacobian",
110  "Base class that computes Jacobian from two RGB-D images.");
111 
112  // cloudViewer.odometry.RGBDOdometryJacobianFromColorTerm:
113  // RGBDOdometryJacobian
117  jacobian_color(m, "RGBDOdometryJacobianFromColorTerm",
118  R"(Class to Compute Jacobian using color term.
119 
120 Energy: :math:`(I_p-I_q)^2.`
121 
122 Reference:
123 
124 F. Steinbrucker, J. Sturm, and D. Cremers.
125 
126 Real-time visual odometry from dense RGB-D images.
127 
128 In ICCV Workshops, 2011.)");
129  py::detail::bind_default_constructor<RGBDOdometryJacobianFromColorTerm>(
130  jacobian_color);
131  py::detail::bind_copy_functions<RGBDOdometryJacobianFromColorTerm>(
132  jacobian_color);
133  jacobian_color.def(
134  "__repr__", [](const RGBDOdometryJacobianFromColorTerm &te) {
135  return std::string("RGBDOdometryJacobianFromColorTerm");
136  });
137 
138  // cloudViewer.odometry.RGBDOdometryJacobianFromHybridTerm:
139  // RGBDOdometryJacobian
143  jacobian_hybrid(m, "RGBDOdometryJacobianFromHybridTerm",
144  R"(Class to compute Jacobian using hybrid term
145 
146 Energy: :math:`(I_p-I_q)^2 + \lambda(D_p-D_q')^2`
147 
148 Reference:
149 
150 J. Park, Q.-Y. Zhou, and V. Koltun
151 
152 Anonymous submission.)");
153  py::detail::bind_default_constructor<RGBDOdometryJacobianFromHybridTerm>(
154  jacobian_hybrid);
155  py::detail::bind_copy_functions<RGBDOdometryJacobianFromHybridTerm>(
156  jacobian_hybrid);
157  jacobian_hybrid.def(
158  "__repr__", [](const RGBDOdometryJacobianFromHybridTerm &te) {
159  return std::string("RGBDOdometryJacobianFromHybridTerm");
160  });
161 }
162 
163 void pybind_odometry_methods(py::module &m) {
164  m.def("compute_rgbd_odometry", &ComputeRGBDOdometry,
165  py::call_guard<py::gil_scoped_release>(),
166  "Function to estimate 6D rigid motion from two RGBD image pairs. "
167  "Output: (is_success, 4x4 motion matrix, 6x6 information matrix).",
168  "rgbd_source"_a, "rgbd_target"_a,
169  "pinhole_camera_intrinsic"_a = camera::PinholeCameraIntrinsic(),
170  "odo_init"_a = Eigen::Matrix4d::Identity(),
171  "jacobian"_a = RGBDOdometryJacobianFromHybridTerm(),
172  "option"_a = OdometryOption());
174  m, "compute_rgbd_odometry",
175  {
176  {"rgbd_source", "Source RGBD image."},
177  {"rgbd_target", "Target RGBD image."},
178  {"pinhole_camera_intrinsic", "Camera intrinsic parameters"},
179  {"odo_init", "Initial 4x4 motion matrix estimation."},
180  {"jacobian",
181  "The odometry Jacobian method to use. Can be "
182  "``"
183  "RGBDOdometryJacobianFromHybridTerm()`` or "
184  "``RGBDOdometryJacobianFromColorTerm("
185  ").``"},
186  {"option", "Odometry hyper parameteres."},
187  });
188 }
189 
190 void pybind_odometry(py::module &m) {
191  py::module m_submodule = m.def_submodule("odometry", "Odometry pipeline.");
192  pybind_odometry_classes(m_submodule);
193  pybind_odometry_methods(m_submodule);
194 }
195 
196 } // namespace odometry
197 } // namespace pipelines
198 } // namespace cloudViewer
Contains the pinhole camera intrinsic parameters.
The Image class stores image with customizable width, height, num of channels and bytes per channel.
Definition: Image.h:33
RGBDImage is for a pair of registered color and depth images,.
Definition: RGBDImage.h:27
double min_depth_
Pixels that has larger than specified depth values are ignored.
double max_depth_
Pixels that has larger than specified depth values are ignored.
void ComputeJacobianAndResidual(int row, std::vector< Eigen::Vector6d, utility::Vector6d_allocator > &J_r, std::vector< double > &r, std::vector< double > &w, const geometry::RGBDImage &source, const geometry::RGBDImage &target, const geometry::Image &source_xyz, const geometry::RGBDImage &target_dx, const geometry::RGBDImage &target_dy, const Eigen::Matrix3d &intrinsic, const Eigen::Matrix4d &extrinsic, const CorrespondenceSetPixelWise &corresps) const override
Definition: odometry.cpp:26
Base class that computes Jacobian from two RGB-D images.
void FunctionDocInject(py::module &pybind_module, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:76
std::tuple< bool, Eigen::Matrix4d, Eigen::Matrix6d > ComputeRGBDOdometry(const geometry::RGBDImage &source, const geometry::RGBDImage &target, const camera::PinholeCameraIntrinsic &pinhole_camera_intrinsic, const Eigen::Matrix4d &odo_init, const RGBDOdometryJacobian &jacobian_method, const OdometryOption &option)
Function to estimate 6D rigid motion from two RGBD image pairs.
Definition: Odometry.cpp:501
void pybind_odometry(py::module &m)
Definition: odometry.cpp:172
void pybind_odometry_classes(py::module &m)
Definition: odometry.cpp:45
std::vector< Eigen::Vector4i, utility::Vector4i_allocator > CorrespondenceSetPixelWise
void pybind_odometry_methods(py::module &m)
Definition: odometry.cpp:145
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20