kinfu.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html
  4. // This code is also subject to the license terms in the LICENSE_KinectFusion.md file found in this module's directory
  5. #ifndef __OPENCV_RGBD_KINFU_HPP__
  6. #define __OPENCV_RGBD_KINFU_HPP__
  7. #include "opencv2/core.hpp"
  8. #include "opencv2/core/affine.hpp"
  9. #include <opencv2/rgbd/volume.hpp>
  10. namespace cv {
  11. namespace kinfu {
  12. //! @addtogroup kinect_fusion
  13. //! @{
  14. struct CV_EXPORTS_W Params
  15. {
  16. CV_WRAP Params(){}
  17. /**
  18. * @brief Constructor for Params
  19. * Sets the initial pose of the TSDF volume.
  20. * @param volumeInitialPoseRot rotation matrix
  21. * @param volumeInitialPoseTransl translation vector
  22. */
  23. CV_WRAP Params(Matx33f volumeInitialPoseRot, Vec3f volumeInitialPoseTransl)
  24. {
  25. setInitialVolumePose(volumeInitialPoseRot,volumeInitialPoseTransl);
  26. }
  27. /**
  28. * @brief Constructor for Params
  29. * Sets the initial pose of the TSDF volume.
  30. * @param volumeInitialPose 4 by 4 Homogeneous Transform matrix to set the intial pose of TSDF volume
  31. */
  32. CV_WRAP Params(Matx44f volumeInitialPose)
  33. {
  34. setInitialVolumePose(volumeInitialPose);
  35. }
  36. /**
  37. * @brief Set Initial Volume Pose
  38. * Sets the initial pose of the TSDF volume.
  39. * @param R rotation matrix
  40. * @param t translation vector
  41. */
  42. CV_WRAP void setInitialVolumePose(Matx33f R, Vec3f t);
  43. /**
  44. * @brief Set Initial Volume Pose
  45. * Sets the initial pose of the TSDF volume.
  46. * @param homogen_tf 4 by 4 Homogeneous Transform matrix to set the intial pose of TSDF volume
  47. */
  48. CV_WRAP void setInitialVolumePose(Matx44f homogen_tf);
  49. /**
  50. * @brief Default parameters
  51. * A set of parameters which provides better model quality, can be very slow.
  52. */
  53. CV_WRAP static Ptr<Params> defaultParams();
  54. /** @brief Coarse parameters
  55. A set of parameters which provides better speed, can fail to match frames
  56. in case of rapid sensor motion.
  57. */
  58. CV_WRAP static Ptr<Params> coarseParams();
  59. /** @brief HashTSDF parameters
  60. A set of parameters suitable for use with HashTSDFVolume
  61. */
  62. CV_WRAP static Ptr<Params> hashTSDFParams(bool isCoarse);
  63. /** @brief ColoredTSDF parameters
  64. A set of parameters suitable for use with ColoredTSDFVolume
  65. */
  66. CV_WRAP static Ptr<Params> coloredTSDFParams(bool isCoarse);
  67. /** @brief frame size in pixels */
  68. CV_PROP_RW Size frameSize;
  69. /** @brief rgb frame size in pixels */
  70. CV_PROP_RW kinfu::VolumeType volumeType;
  71. /** @brief camera intrinsics */
  72. CV_PROP_RW Matx33f intr;
  73. /** @brief rgb camera intrinsics */
  74. CV_PROP_RW Matx33f rgb_intr;
  75. /** @brief pre-scale per 1 meter for input values
  76. Typical values are:
  77. * 5000 per 1 meter for the 16-bit PNG files of TUM database
  78. * 1000 per 1 meter for Kinect 2 device
  79. * 1 per 1 meter for the 32-bit float images in the ROS bag files
  80. */
  81. CV_PROP_RW float depthFactor;
  82. /** @brief Depth sigma in meters for bilateral smooth */
  83. CV_PROP_RW float bilateral_sigma_depth;
  84. /** @brief Spatial sigma in pixels for bilateral smooth */
  85. CV_PROP_RW float bilateral_sigma_spatial;
  86. /** @brief Kernel size in pixels for bilateral smooth */
  87. CV_PROP_RW int bilateral_kernel_size;
  88. /** @brief Number of pyramid levels for ICP */
  89. CV_PROP_RW int pyramidLevels;
  90. /** @brief Resolution of voxel space
  91. Number of voxels in each dimension.
  92. */
  93. CV_PROP_RW Vec3i volumeDims;
  94. /** @brief Size of voxel in meters */
  95. CV_PROP_RW float voxelSize;
  96. /** @brief Minimal camera movement in meters
  97. Integrate new depth frame only if camera movement exceeds this value.
  98. */
  99. CV_PROP_RW float tsdf_min_camera_movement;
  100. /** @brief initial volume pose in meters */
  101. Affine3f volumePose;
  102. /** @brief distance to truncate in meters
  103. Distances to surface that exceed this value will be truncated to 1.0.
  104. */
  105. CV_PROP_RW float tsdf_trunc_dist;
  106. /** @brief max number of frames per voxel
  107. Each voxel keeps running average of distances no longer than this value.
  108. */
  109. CV_PROP_RW int tsdf_max_weight;
  110. /** @brief A length of one raycast step
  111. How much voxel sizes we skip each raycast step
  112. */
  113. CV_PROP_RW float raycast_step_factor;
  114. // gradient delta in voxel sizes
  115. // fixed at 1.0f
  116. // float gradient_delta_factor;
  117. /** @brief light pose for rendering in meters */
  118. CV_PROP_RW Vec3f lightPose;
  119. /** @brief distance theshold for ICP in meters */
  120. CV_PROP_RW float icpDistThresh;
  121. /** angle threshold for ICP in radians */
  122. CV_PROP_RW float icpAngleThresh;
  123. /** number of ICP iterations for each pyramid level */
  124. CV_PROP_RW std::vector<int> icpIterations;
  125. /** @brief Threshold for depth truncation in meters
  126. All depth values beyond this threshold will be set to zero
  127. */
  128. CV_PROP_RW float truncateThreshold;
  129. };
  130. /** @brief KinectFusion implementation
  131. This class implements a 3d reconstruction algorithm described in
  132. @cite kinectfusion paper.
  133. It takes a sequence of depth images taken from depth sensor
  134. (or any depth images source such as stereo camera matching algorithm or even raymarching renderer).
  135. The output can be obtained as a vector of points and their normals
  136. or can be Phong-rendered from given camera pose.
  137. An internal representation of a model is a voxel cuboid that keeps TSDF values
  138. which are a sort of distances to the surface (for details read the @cite kinectfusion article about TSDF).
  139. There is no interface to that representation yet.
  140. KinFu uses OpenCL acceleration automatically if available.
  141. To enable or disable it explicitly use cv::setUseOptimized() or cv::ocl::setUseOpenCL().
  142. This implementation is based on [kinfu-remake](https://github.com/Nerei/kinfu_remake).
  143. Note that the KinectFusion algorithm was patented and its use may be restricted by
  144. the list of patents mentioned in README.md file in this module directory.
  145. That's why you need to set the OPENCV_ENABLE_NONFREE option in CMake to use KinectFusion.
  146. */
  147. class CV_EXPORTS_W KinFu
  148. {
  149. public:
  150. CV_WRAP static Ptr<KinFu> create(const Ptr<Params>& _params);
  151. virtual ~KinFu();
  152. /** @brief Get current parameters */
  153. virtual const Params& getParams() const = 0;
  154. /** @brief Renders a volume into an image
  155. Renders a 0-surface of TSDF using Phong shading into a CV_8UC4 Mat.
  156. Light pose is fixed in KinFu params.
  157. @param image resulting image
  158. */
  159. CV_WRAP virtual void render(OutputArray image) const = 0;
  160. /** @brief Renders a volume into an image
  161. Renders a 0-surface of TSDF using Phong shading into a CV_8UC4 Mat.
  162. Light pose is fixed in KinFu params.
  163. @param image resulting image
  164. @param cameraPose pose of camera to render from. If empty then render from current pose
  165. which is a last frame camera pose.
  166. */
  167. CV_WRAP virtual void render(OutputArray image, const Matx44f& cameraPose) const = 0;
  168. /** @brief Gets points and normals of current 3d mesh
  169. The order of normals corresponds to order of points.
  170. The order of points is undefined.
  171. @param points vector of points which are 4-float vectors
  172. @param normals vector of normals which are 4-float vectors
  173. */
  174. CV_WRAP virtual void getCloud(OutputArray points, OutputArray normals) const = 0;
  175. /** @brief Gets points of current 3d mesh
  176. The order of points is undefined.
  177. @param points vector of points which are 4-float vectors
  178. */
  179. CV_WRAP virtual void getPoints(OutputArray points) const = 0;
  180. /** @brief Calculates normals for given points
  181. @param points input vector of points which are 4-float vectors
  182. @param normals output vector of corresponding normals which are 4-float vectors
  183. */
  184. CV_WRAP virtual void getNormals(InputArray points, OutputArray normals) const = 0;
  185. /** @brief Resets the algorithm
  186. Clears current model and resets a pose.
  187. */
  188. CV_WRAP virtual void reset() = 0;
  189. /** @brief Get current pose in voxel space */
  190. virtual const Affine3f getPose() const = 0;
  191. /** @brief Process next depth frame
  192. Integrates depth into voxel space with respect to its ICP-calculated pose.
  193. Input image is converted to CV_32F internally if has another type.
  194. @param depth one-channel image which size and depth scale is described in algorithm's parameters
  195. @return true if succeeded to align new frame with current scene, false if opposite
  196. */
  197. CV_WRAP virtual bool update(InputArray depth) = 0;
  198. };
  199. //! @}
  200. }
  201. }
  202. #endif