rgbd.hpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2009, Willow Garage, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. * * Neither the name of Willow Garage, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. #ifndef __OPENCV_RGBD_HPP__
  36. #define __OPENCV_RGBD_HPP__
  37. #ifdef __cplusplus
  38. #include <opencv2/core.hpp>
  39. #include <limits>
  40. /** @defgroup rgbd RGB-Depth Processing
  41. */
  42. namespace cv
  43. {
  44. namespace rgbd
  45. {
  46. //! @addtogroup rgbd
  47. //! @{
  48. /** Checks if the value is a valid depth. For CV_16U or CV_16S, the convention is to be invalid if it is
  49. * a limit. For a float/double, we just check if it is a NaN
  50. * @param depth the depth to check for validity
  51. */
  52. CV_EXPORTS
  53. inline bool
  54. isValidDepth(const float & depth)
  55. {
  56. return !cvIsNaN(depth);
  57. }
  58. CV_EXPORTS
  59. inline bool
  60. isValidDepth(const double & depth)
  61. {
  62. return !cvIsNaN(depth);
  63. }
  64. CV_EXPORTS
  65. inline bool
  66. isValidDepth(const short int & depth)
  67. {
  68. return (depth != std::numeric_limits<short int>::min()) && (depth != std::numeric_limits<short int>::max());
  69. }
  70. CV_EXPORTS
  71. inline bool
  72. isValidDepth(const unsigned short int & depth)
  73. {
  74. return (depth != std::numeric_limits<unsigned short int>::min())
  75. && (depth != std::numeric_limits<unsigned short int>::max());
  76. }
  77. CV_EXPORTS
  78. inline bool
  79. isValidDepth(const int & depth)
  80. {
  81. return (depth != std::numeric_limits<int>::min()) && (depth != std::numeric_limits<int>::max());
  82. }
  83. CV_EXPORTS
  84. inline bool
  85. isValidDepth(const unsigned int & depth)
  86. {
  87. return (depth != std::numeric_limits<unsigned int>::min()) && (depth != std::numeric_limits<unsigned int>::max());
  88. }
  89. /** Object that can compute the normals in an image.
  90. * It is an object as it can cache data for speed efficiency
  91. * The implemented methods are either:
  92. * - FALS (the fastest) and SRI from
  93. * ``Fast and Accurate Computation of Surface Normals from Range Images``
  94. * by H. Badino, D. Huber, Y. Park and T. Kanade
  95. * - the normals with bilateral filtering on a depth image from
  96. * ``Gradient Response Maps for Real-Time Detection of Texture-Less Objects``
  97. * by S. Hinterstoisser, C. Cagniart, S. Ilic, P. Sturm, N. Navab, P. Fua, and V. Lepetit
  98. */
  99. class CV_EXPORTS RgbdNormals: public Algorithm
  100. {
  101. public:
  102. enum RGBD_NORMALS_METHOD
  103. {
  104. RGBD_NORMALS_METHOD_FALS, RGBD_NORMALS_METHOD_LINEMOD, RGBD_NORMALS_METHOD_SRI
  105. };
  106. RgbdNormals()
  107. :
  108. rows_(0),
  109. cols_(0),
  110. depth_(0),
  111. K_(Mat()),
  112. window_size_(0),
  113. method_(RGBD_NORMALS_METHOD_FALS),
  114. rgbd_normals_impl_(0)
  115. {
  116. }
  117. /** Constructor
  118. * @param rows the number of rows of the depth image normals will be computed on
  119. * @param cols the number of cols of the depth image normals will be computed on
  120. * @param depth the depth of the normals (only CV_32F or CV_64F)
  121. * @param K the calibration matrix to use
  122. * @param window_size the window size to compute the normals: can only be 1,3,5 or 7
  123. * @param method one of the methods to use: RGBD_NORMALS_METHOD_SRI, RGBD_NORMALS_METHOD_FALS
  124. */
  125. RgbdNormals(int rows, int cols, int depth, InputArray K, int window_size = 5, int method =
  126. RGBD_NORMALS_METHOD_FALS);
  127. ~RgbdNormals();
  128. /** Given a set of 3d points in a depth image, compute the normals at each point.
  129. * @param points a rows x cols x 3 matrix of CV_32F/CV64F or a rows x cols x 1 CV_U16S
  130. * @param normals a rows x cols x 3 matrix
  131. */
  132. void
  133. operator()(InputArray points, OutputArray normals) const;
  134. /** Initializes some data that is cached for later computation
  135. * If that function is not called, it will be called the first time normals are computed
  136. */
  137. void
  138. initialize() const;
  139. int getRows() const
  140. {
  141. return rows_;
  142. }
  143. void setRows(int val)
  144. {
  145. rows_ = val;
  146. }
  147. int getCols() const
  148. {
  149. return cols_;
  150. }
  151. void setCols(int val)
  152. {
  153. cols_ = val;
  154. }
  155. int getWindowSize() const
  156. {
  157. return window_size_;
  158. }
  159. void setWindowSize(int val)
  160. {
  161. window_size_ = val;
  162. }
  163. int getDepth() const
  164. {
  165. return depth_;
  166. }
  167. void setDepth(int val)
  168. {
  169. depth_ = val;
  170. }
  171. cv::Mat getK() const
  172. {
  173. return K_;
  174. }
  175. void setK(const cv::Mat &val)
  176. {
  177. K_ = val;
  178. }
  179. int getMethod() const
  180. {
  181. return method_;
  182. }
  183. void setMethod(int val)
  184. {
  185. method_ = val;
  186. }
  187. protected:
  188. void
  189. initialize_normals_impl(int rows, int cols, int depth, const Mat & K, int window_size, int method) const;
  190. int rows_, cols_, depth_;
  191. Mat K_;
  192. int window_size_;
  193. int method_;
  194. mutable void* rgbd_normals_impl_;
  195. };
  196. /** Object that can clean a noisy depth image
  197. */
  198. class CV_EXPORTS DepthCleaner: public Algorithm
  199. {
  200. public:
  201. /** NIL method is from
  202. * ``Modeling Kinect Sensor Noise for Improved 3d Reconstruction and Tracking``
  203. * by C. Nguyen, S. Izadi, D. Lovel
  204. */
  205. enum DEPTH_CLEANER_METHOD
  206. {
  207. DEPTH_CLEANER_NIL
  208. };
  209. DepthCleaner()
  210. :
  211. depth_(0),
  212. window_size_(0),
  213. method_(DEPTH_CLEANER_NIL),
  214. depth_cleaner_impl_(0)
  215. {
  216. }
  217. /** Constructor
  218. * @param depth the depth of the normals (only CV_32F or CV_64F)
  219. * @param window_size the window size to compute the normals: can only be 1,3,5 or 7
  220. * @param method one of the methods to use: RGBD_NORMALS_METHOD_SRI, RGBD_NORMALS_METHOD_FALS
  221. */
  222. DepthCleaner(int depth, int window_size = 5, int method = DEPTH_CLEANER_NIL);
  223. ~DepthCleaner();
  224. /** Given a set of 3d points in a depth image, compute the normals at each point.
  225. * @param points a rows x cols x 3 matrix of CV_32F/CV64F or a rows x cols x 1 CV_U16S
  226. * @param depth a rows x cols matrix of the cleaned up depth
  227. */
  228. void
  229. operator()(InputArray points, OutputArray depth) const;
  230. /** Initializes some data that is cached for later computation
  231. * If that function is not called, it will be called the first time normals are computed
  232. */
  233. void
  234. initialize() const;
  235. int getWindowSize() const
  236. {
  237. return window_size_;
  238. }
  239. void setWindowSize(int val)
  240. {
  241. window_size_ = val;
  242. }
  243. int getDepth() const
  244. {
  245. return depth_;
  246. }
  247. void setDepth(int val)
  248. {
  249. depth_ = val;
  250. }
  251. int getMethod() const
  252. {
  253. return method_;
  254. }
  255. void setMethod(int val)
  256. {
  257. method_ = val;
  258. }
  259. protected:
  260. void
  261. initialize_cleaner_impl() const;
  262. int depth_;
  263. int window_size_;
  264. int method_;
  265. mutable void* depth_cleaner_impl_;
  266. };
  267. /** Registers depth data to an external camera
  268. * Registration is performed by creating a depth cloud, transforming the cloud by
  269. * the rigid body transformation between the cameras, and then projecting the
  270. * transformed points into the RGB camera.
  271. *
  272. * uv_rgb = K_rgb * [R | t] * z * inv(K_ir) * uv_ir
  273. *
  274. * Currently does not check for negative depth values.
  275. *
  276. * @param unregisteredCameraMatrix the camera matrix of the depth camera
  277. * @param registeredCameraMatrix the camera matrix of the external camera
  278. * @param registeredDistCoeffs the distortion coefficients of the external camera
  279. * @param Rt the rigid body transform between the cameras. Transforms points from depth camera frame to external camera frame.
  280. * @param unregisteredDepth the input depth data
  281. * @param outputImagePlaneSize the image plane dimensions of the external camera (width, height)
  282. * @param registeredDepth the result of transforming the depth into the external camera
  283. * @param depthDilation whether or not the depth is dilated to avoid holes and occlusion errors (optional)
  284. */
  285. CV_EXPORTS
  286. void
  287. registerDepth(InputArray unregisteredCameraMatrix, InputArray registeredCameraMatrix, InputArray registeredDistCoeffs,
  288. InputArray Rt, InputArray unregisteredDepth, const Size& outputImagePlaneSize,
  289. OutputArray registeredDepth, bool depthDilation=false);
  290. /**
  291. * @param depth the depth image
  292. * @param in_K
  293. * @param in_points the list of xy coordinates
  294. * @param points3d the resulting 3d points
  295. */
  296. CV_EXPORTS
  297. void
  298. depthTo3dSparse(InputArray depth, InputArray in_K, InputArray in_points, OutputArray points3d);
  299. /** Converts a depth image to an organized set of 3d points.
  300. * The coordinate system is x pointing left, y down and z away from the camera
  301. * @param depth the depth image (if given as short int CV_U, it is assumed to be the depth in millimeters
  302. * (as done with the Microsoft Kinect), otherwise, if given as CV_32F or CV_64F, it is assumed in meters)
  303. * @param K The calibration matrix
  304. * @param points3d the resulting 3d points. They are of depth the same as `depth` if it is CV_32F or CV_64F, and the
  305. * depth of `K` if `depth` is of depth CV_U
  306. * @param mask the mask of the points to consider (can be empty)
  307. */
  308. CV_EXPORTS
  309. void
  310. depthTo3d(InputArray depth, InputArray K, OutputArray points3d, InputArray mask = noArray());
  311. /** If the input image is of type CV_16UC1 (like the Kinect one), the image is converted to floats, divided
  312. * by 1000 to get a depth in meters, and the values 0 are converted to std::numeric_limits<float>::quiet_NaN()
  313. * Otherwise, the image is simply converted to floats
  314. * @param in the depth image (if given as short int CV_U, it is assumed to be the depth in millimeters
  315. * (as done with the Microsoft Kinect), it is assumed in meters)
  316. * @param depth the desired output depth (floats or double)
  317. * @param out The rescaled float depth image
  318. */
  319. CV_EXPORTS
  320. void
  321. rescaleDepth(InputArray in, int depth, OutputArray out);
  322. /** Object that can compute planes in an image
  323. */
  324. class CV_EXPORTS RgbdPlane: public Algorithm
  325. {
  326. public:
  327. enum RGBD_PLANE_METHOD
  328. {
  329. RGBD_PLANE_METHOD_DEFAULT
  330. };
  331. RgbdPlane(RGBD_PLANE_METHOD method = RGBD_PLANE_METHOD_DEFAULT)
  332. :
  333. method_(method),
  334. block_size_(40),
  335. min_size_(block_size_*block_size_),
  336. threshold_(0.01),
  337. sensor_error_a_(0),
  338. sensor_error_b_(0),
  339. sensor_error_c_(0)
  340. {
  341. }
  342. /** Find The planes in a depth image
  343. * @param points3d the 3d points organized like the depth image: rows x cols with 3 channels
  344. * @param normals the normals for every point in the depth image
  345. * @param mask An image where each pixel is labeled with the plane it belongs to
  346. * and 255 if it does not belong to any plane
  347. * @param plane_coefficients the coefficients of the corresponding planes (a,b,c,d) such that ax+by+cz+d=0, norm(a,b,c)=1
  348. * and c < 0 (so that the normal points towards the camera)
  349. */
  350. void
  351. operator()(InputArray points3d, InputArray normals, OutputArray mask,
  352. OutputArray plane_coefficients);
  353. /** Find The planes in a depth image but without doing a normal check, which is faster but less accurate
  354. * @param points3d the 3d points organized like the depth image: rows x cols with 3 channels
  355. * @param mask An image where each pixel is labeled with the plane it belongs to
  356. * and 255 if it does not belong to any plane
  357. * @param plane_coefficients the coefficients of the corresponding planes (a,b,c,d) such that ax+by+cz+d=0
  358. */
  359. void
  360. operator()(InputArray points3d, OutputArray mask, OutputArray plane_coefficients);
  361. int getBlockSize() const
  362. {
  363. return block_size_;
  364. }
  365. void setBlockSize(int val)
  366. {
  367. block_size_ = val;
  368. }
  369. int getMinSize() const
  370. {
  371. return min_size_;
  372. }
  373. void setMinSize(int val)
  374. {
  375. min_size_ = val;
  376. }
  377. int getMethod() const
  378. {
  379. return method_;
  380. }
  381. void setMethod(int val)
  382. {
  383. method_ = val;
  384. }
  385. double getThreshold() const
  386. {
  387. return threshold_;
  388. }
  389. void setThreshold(double val)
  390. {
  391. threshold_ = val;
  392. }
  393. double getSensorErrorA() const
  394. {
  395. return sensor_error_a_;
  396. }
  397. void setSensorErrorA(double val)
  398. {
  399. sensor_error_a_ = val;
  400. }
  401. double getSensorErrorB() const
  402. {
  403. return sensor_error_b_;
  404. }
  405. void setSensorErrorB(double val)
  406. {
  407. sensor_error_b_ = val;
  408. }
  409. double getSensorErrorC() const
  410. {
  411. return sensor_error_c_;
  412. }
  413. void setSensorErrorC(double val)
  414. {
  415. sensor_error_c_ = val;
  416. }
  417. private:
  418. /** The method to use to compute the planes */
  419. int method_;
  420. /** The size of the blocks to look at for a stable MSE */
  421. int block_size_;
  422. /** The minimum size of a cluster to be considered a plane */
  423. int min_size_;
  424. /** How far a point can be from a plane to belong to it (in meters) */
  425. double threshold_;
  426. /** coefficient of the sensor error with respect to the. All 0 by default but you want a=0.0075 for a Kinect */
  427. double sensor_error_a_, sensor_error_b_, sensor_error_c_;
  428. };
  429. /** Object that contains a frame data.
  430. */
  431. struct CV_EXPORTS RgbdFrame
  432. {
  433. RgbdFrame();
  434. RgbdFrame(const Mat& image, const Mat& depth, const Mat& mask=Mat(), const Mat& normals=Mat(), int ID=-1);
  435. virtual ~RgbdFrame();
  436. virtual void
  437. release();
  438. int ID;
  439. Mat image;
  440. Mat depth;
  441. Mat mask;
  442. Mat normals;
  443. };
  444. /** Object that contains a frame data that is possibly needed for the Odometry.
  445. * It's used for the efficiency (to pass precomputed/cached data of the frame that participates
  446. * in the Odometry processing several times).
  447. */
  448. struct CV_EXPORTS OdometryFrame : public RgbdFrame
  449. {
  450. /** These constants are used to set a type of cache which has to be prepared depending on the frame role:
  451. * srcFrame or dstFrame (see compute method of the Odometry class). For the srcFrame and dstFrame different cache data may be required,
  452. * some part of a cache may be common for both frame roles.
  453. * @param CACHE_SRC The cache data for the srcFrame will be prepared.
  454. * @param CACHE_DST The cache data for the dstFrame will be prepared.
  455. * @param CACHE_ALL The cache data for both srcFrame and dstFrame roles will be computed.
  456. */
  457. enum
  458. {
  459. CACHE_SRC = 1, CACHE_DST = 2, CACHE_ALL = CACHE_SRC + CACHE_DST
  460. };
  461. OdometryFrame();
  462. OdometryFrame(const Mat& image, const Mat& depth, const Mat& mask=Mat(), const Mat& normals=Mat(), int ID=-1);
  463. virtual void
  464. release();
  465. void
  466. releasePyramids();
  467. std::vector<Mat> pyramidImage;
  468. std::vector<Mat> pyramidDepth;
  469. std::vector<Mat> pyramidMask;
  470. std::vector<Mat> pyramidCloud;
  471. std::vector<Mat> pyramid_dI_dx;
  472. std::vector<Mat> pyramid_dI_dy;
  473. std::vector<Mat> pyramidTexturedMask;
  474. std::vector<Mat> pyramidNormals;
  475. std::vector<Mat> pyramidNormalsMask;
  476. };
  477. /** Base class for computation of odometry.
  478. */
  479. class CV_EXPORTS Odometry: public Algorithm
  480. {
  481. public:
  482. /** A class of transformation*/
  483. enum
  484. {
  485. ROTATION = 1, TRANSLATION = 2, RIGID_BODY_MOTION = 4
  486. };
  487. static inline float
  488. DEFAULT_MIN_DEPTH()
  489. {
  490. return 0.f; // in meters
  491. }
  492. static inline float
  493. DEFAULT_MAX_DEPTH()
  494. {
  495. return 4.f; // in meters
  496. }
  497. static inline float
  498. DEFAULT_MAX_DEPTH_DIFF()
  499. {
  500. return 0.07f; // in meters
  501. }
  502. static inline float
  503. DEFAULT_MAX_POINTS_PART()
  504. {
  505. return 0.07f; // in [0, 1]
  506. }
  507. static inline float
  508. DEFAULT_MAX_TRANSLATION()
  509. {
  510. return 0.15f; // in meters
  511. }
  512. static inline float
  513. DEFAULT_MAX_ROTATION()
  514. {
  515. return 15; // in degrees
  516. }
  517. /** Method to compute a transformation from the source frame to the destination one.
  518. * Some odometry algorithms do not used some data of frames (eg. ICP does not use images).
  519. * In such case corresponding arguments can be set as empty Mat.
  520. * The method returns true if all internal computions were possible (e.g. there were enough correspondences,
  521. * system of equations has a solution, etc) and resulting transformation satisfies some test if it's provided
  522. * by the Odometry inheritor implementation (e.g. thresholds for maximum translation and rotation).
  523. * @param srcImage Image data of the source frame (CV_8UC1)
  524. * @param srcDepth Depth data of the source frame (CV_32FC1, in meters)
  525. * @param srcMask Mask that sets which pixels have to be used from the source frame (CV_8UC1)
  526. * @param dstImage Image data of the destination frame (CV_8UC1)
  527. * @param dstDepth Depth data of the destination frame (CV_32FC1, in meters)
  528. * @param dstMask Mask that sets which pixels have to be used from the destination frame (CV_8UC1)
  529. * @param Rt Resulting transformation from the source frame to the destination one (rigid body motion):
  530. dst_p = Rt * src_p, where dst_p is a homogeneous point in the destination frame and src_p is
  531. homogeneous point in the source frame,
  532. Rt is 4x4 matrix of CV_64FC1 type.
  533. * @param initRt Initial transformation from the source frame to the destination one (optional)
  534. */
  535. bool
  536. compute(const Mat& srcImage, const Mat& srcDepth, const Mat& srcMask, const Mat& dstImage, const Mat& dstDepth,
  537. const Mat& dstMask, Mat& Rt, const Mat& initRt = Mat()) const;
  538. /** One more method to compute a transformation from the source frame to the destination one.
  539. * It is designed to save on computing the frame data (image pyramids, normals, etc.).
  540. */
  541. bool
  542. compute(Ptr<OdometryFrame>& srcFrame, Ptr<OdometryFrame>& dstFrame, Mat& Rt, const Mat& initRt = Mat()) const;
  543. /** Prepare a cache for the frame. The function checks the precomputed/passed data (throws the error if this data
  544. * does not satisfy) and computes all remaining cache data needed for the frame. Returned size is a resolution
  545. * of the prepared frame.
  546. * @param frame The odometry which will process the frame.
  547. * @param cacheType The cache type: CACHE_SRC, CACHE_DST or CACHE_ALL.
  548. */
  549. virtual Size prepareFrameCache(Ptr<OdometryFrame>& frame, int cacheType) const;
  550. static Ptr<Odometry> create(const String & odometryType);
  551. /** @see setCameraMatrix */
  552. virtual cv::Mat getCameraMatrix() const = 0;
  553. /** @copybrief getCameraMatrix @see getCameraMatrix */
  554. virtual void setCameraMatrix(const cv::Mat &val) = 0;
  555. /** @see setTransformType */
  556. virtual int getTransformType() const = 0;
  557. /** @copybrief getTransformType @see getTransformType */
  558. virtual void setTransformType(int val) = 0;
  559. protected:
  560. virtual void
  561. checkParams() const = 0;
  562. virtual bool
  563. computeImpl(const Ptr<OdometryFrame>& srcFrame, const Ptr<OdometryFrame>& dstFrame, Mat& Rt,
  564. const Mat& initRt) const = 0;
  565. };
  566. /** Odometry based on the paper "Real-Time Visual Odometry from Dense RGB-D Images",
  567. * F. Steinbucker, J. Strum, D. Cremers, ICCV, 2011.
  568. */
  569. class CV_EXPORTS RgbdOdometry: public Odometry
  570. {
  571. public:
  572. RgbdOdometry();
  573. /** Constructor.
  574. * @param cameraMatrix Camera matrix
  575. * @param minDepth Pixels with depth less than minDepth will not be used (in meters)
  576. * @param maxDepth Pixels with depth larger than maxDepth will not be used (in meters)
  577. * @param maxDepthDiff Correspondences between pixels of two given frames will be filtered out
  578. * if their depth difference is larger than maxDepthDiff (in meters)
  579. * @param iterCounts Count of iterations on each pyramid level.
  580. * @param minGradientMagnitudes For each pyramid level the pixels will be filtered out
  581. * if they have gradient magnitude less than minGradientMagnitudes[level].
  582. * @param maxPointsPart The method uses a random pixels subset of size frameWidth x frameHeight x pointsPart
  583. * @param transformType Class of transformation
  584. */
  585. RgbdOdometry(const Mat& cameraMatrix, float minDepth = DEFAULT_MIN_DEPTH(), float maxDepth = DEFAULT_MAX_DEPTH(),
  586. float maxDepthDiff = DEFAULT_MAX_DEPTH_DIFF(), const std::vector<int>& iterCounts = std::vector<int>(),
  587. const std::vector<float>& minGradientMagnitudes = std::vector<float>(), float maxPointsPart = DEFAULT_MAX_POINTS_PART(),
  588. int transformType = RIGID_BODY_MOTION);
  589. virtual Size prepareFrameCache(Ptr<OdometryFrame>& frame, int cacheType) const;
  590. cv::Mat getCameraMatrix() const
  591. {
  592. return cameraMatrix;
  593. }
  594. void setCameraMatrix(const cv::Mat &val)
  595. {
  596. cameraMatrix = val;
  597. }
  598. double getMinDepth() const
  599. {
  600. return minDepth;
  601. }
  602. void setMinDepth(double val)
  603. {
  604. minDepth = val;
  605. }
  606. double getMaxDepth() const
  607. {
  608. return maxDepth;
  609. }
  610. void setMaxDepth(double val)
  611. {
  612. maxDepth = val;
  613. }
  614. double getMaxDepthDiff() const
  615. {
  616. return maxDepthDiff;
  617. }
  618. void setMaxDepthDiff(double val)
  619. {
  620. maxDepthDiff = val;
  621. }
  622. cv::Mat getIterationCounts() const
  623. {
  624. return iterCounts;
  625. }
  626. void setIterationCounts(const cv::Mat &val)
  627. {
  628. iterCounts = val;
  629. }
  630. cv::Mat getMinGradientMagnitudes() const
  631. {
  632. return minGradientMagnitudes;
  633. }
  634. void setMinGradientMagnitudes(const cv::Mat &val)
  635. {
  636. minGradientMagnitudes = val;
  637. }
  638. double getMaxPointsPart() const
  639. {
  640. return maxPointsPart;
  641. }
  642. void setMaxPointsPart(double val)
  643. {
  644. maxPointsPart = val;
  645. }
  646. int getTransformType() const
  647. {
  648. return transformType;
  649. }
  650. void setTransformType(int val)
  651. {
  652. transformType = val;
  653. }
  654. double getMaxTranslation() const
  655. {
  656. return maxTranslation;
  657. }
  658. void setMaxTranslation(double val)
  659. {
  660. maxTranslation = val;
  661. }
  662. double getMaxRotation() const
  663. {
  664. return maxRotation;
  665. }
  666. void setMaxRotation(double val)
  667. {
  668. maxRotation = val;
  669. }
  670. protected:
  671. virtual void
  672. checkParams() const;
  673. virtual bool
  674. computeImpl(const Ptr<OdometryFrame>& srcFrame, const Ptr<OdometryFrame>& dstFrame, Mat& Rt,
  675. const Mat& initRt) const;
  676. // Some params have commented desired type. It's due to AlgorithmInfo::addParams does not support it now.
  677. /*float*/
  678. double minDepth, maxDepth, maxDepthDiff;
  679. /*vector<int>*/
  680. Mat iterCounts;
  681. /*vector<float>*/
  682. Mat minGradientMagnitudes;
  683. double maxPointsPart;
  684. Mat cameraMatrix;
  685. int transformType;
  686. double maxTranslation, maxRotation;
  687. };
  688. /** Odometry based on the paper "KinectFusion: Real-Time Dense Surface Mapping and Tracking",
  689. * Richard A. Newcombe, Andrew Fitzgibbon, at al, SIGGRAPH, 2011.
  690. */
  691. class ICPOdometry: public Odometry
  692. {
  693. public:
  694. ICPOdometry();
  695. /** Constructor.
  696. * @param cameraMatrix Camera matrix
  697. * @param minDepth Pixels with depth less than minDepth will not be used
  698. * @param maxDepth Pixels with depth larger than maxDepth will not be used
  699. * @param maxDepthDiff Correspondences between pixels of two given frames will be filtered out
  700. * if their depth difference is larger than maxDepthDiff
  701. * @param maxPointsPart The method uses a random pixels subset of size frameWidth x frameHeight x pointsPart
  702. * @param iterCounts Count of iterations on each pyramid level.
  703. * @param transformType Class of trasformation
  704. */
  705. ICPOdometry(const Mat& cameraMatrix, float minDepth = DEFAULT_MIN_DEPTH(), float maxDepth = DEFAULT_MAX_DEPTH(),
  706. float maxDepthDiff = DEFAULT_MAX_DEPTH_DIFF(), float maxPointsPart = DEFAULT_MAX_POINTS_PART(),
  707. const std::vector<int>& iterCounts = std::vector<int>(), int transformType = RIGID_BODY_MOTION);
  708. virtual Size prepareFrameCache(Ptr<OdometryFrame>& frame, int cacheType) const;
  709. cv::Mat getCameraMatrix() const
  710. {
  711. return cameraMatrix;
  712. }
  713. void setCameraMatrix(const cv::Mat &val)
  714. {
  715. cameraMatrix = val;
  716. }
  717. double getMinDepth() const
  718. {
  719. return minDepth;
  720. }
  721. void setMinDepth(double val)
  722. {
  723. minDepth = val;
  724. }
  725. double getMaxDepth() const
  726. {
  727. return maxDepth;
  728. }
  729. void setMaxDepth(double val)
  730. {
  731. maxDepth = val;
  732. }
  733. double getMaxDepthDiff() const
  734. {
  735. return maxDepthDiff;
  736. }
  737. void setMaxDepthDiff(double val)
  738. {
  739. maxDepthDiff = val;
  740. }
  741. cv::Mat getIterationCounts() const
  742. {
  743. return iterCounts;
  744. }
  745. void setIterationCounts(const cv::Mat &val)
  746. {
  747. iterCounts = val;
  748. }
  749. double getMaxPointsPart() const
  750. {
  751. return maxPointsPart;
  752. }
  753. void setMaxPointsPart(double val)
  754. {
  755. maxPointsPart = val;
  756. }
  757. int getTransformType() const
  758. {
  759. return transformType;
  760. }
  761. void setTransformType(int val)
  762. {
  763. transformType = val;
  764. }
  765. double getMaxTranslation() const
  766. {
  767. return maxTranslation;
  768. }
  769. void setMaxTranslation(double val)
  770. {
  771. maxTranslation = val;
  772. }
  773. double getMaxRotation() const
  774. {
  775. return maxRotation;
  776. }
  777. void setMaxRotation(double val)
  778. {
  779. maxRotation = val;
  780. }
  781. Ptr<RgbdNormals> getNormalsComputer() const
  782. {
  783. return normalsComputer;
  784. }
  785. protected:
  786. virtual void
  787. checkParams() const;
  788. virtual bool
  789. computeImpl(const Ptr<OdometryFrame>& srcFrame, const Ptr<OdometryFrame>& dstFrame, Mat& Rt,
  790. const Mat& initRt) const;
  791. // Some params have commented desired type. It's due to AlgorithmInfo::addParams does not support it now.
  792. /*float*/
  793. double minDepth, maxDepth, maxDepthDiff;
  794. /*float*/
  795. double maxPointsPart;
  796. /*vector<int>*/
  797. Mat iterCounts;
  798. Mat cameraMatrix;
  799. int transformType;
  800. double maxTranslation, maxRotation;
  801. mutable Ptr<RgbdNormals> normalsComputer;
  802. };
  803. /** Odometry that merges RgbdOdometry and ICPOdometry by minimize sum of their energy functions.
  804. */
  805. class RgbdICPOdometry: public Odometry
  806. {
  807. public:
  808. RgbdICPOdometry();
  809. /** Constructor.
  810. * @param cameraMatrix Camera matrix
  811. * @param minDepth Pixels with depth less than minDepth will not be used
  812. * @param maxDepth Pixels with depth larger than maxDepth will not be used
  813. * @param maxDepthDiff Correspondences between pixels of two given frames will be filtered out
  814. * if their depth difference is larger than maxDepthDiff
  815. * @param maxPointsPart The method uses a random pixels subset of size frameWidth x frameHeight x pointsPart
  816. * @param iterCounts Count of iterations on each pyramid level.
  817. * @param minGradientMagnitudes For each pyramid level the pixels will be filtered out
  818. * if they have gradient magnitude less than minGradientMagnitudes[level].
  819. * @param transformType Class of trasformation
  820. */
  821. RgbdICPOdometry(const Mat& cameraMatrix, float minDepth = DEFAULT_MIN_DEPTH(), float maxDepth = DEFAULT_MAX_DEPTH(),
  822. float maxDepthDiff = DEFAULT_MAX_DEPTH_DIFF(), float maxPointsPart = DEFAULT_MAX_POINTS_PART(),
  823. const std::vector<int>& iterCounts = std::vector<int>(),
  824. const std::vector<float>& minGradientMagnitudes = std::vector<float>(),
  825. int transformType = RIGID_BODY_MOTION);
  826. virtual Size prepareFrameCache(Ptr<OdometryFrame>& frame, int cacheType) const;
  827. cv::Mat getCameraMatrix() const
  828. {
  829. return cameraMatrix;
  830. }
  831. void setCameraMatrix(const cv::Mat &val)
  832. {
  833. cameraMatrix = val;
  834. }
  835. double getMinDepth() const
  836. {
  837. return minDepth;
  838. }
  839. void setMinDepth(double val)
  840. {
  841. minDepth = val;
  842. }
  843. double getMaxDepth() const
  844. {
  845. return maxDepth;
  846. }
  847. void setMaxDepth(double val)
  848. {
  849. maxDepth = val;
  850. }
  851. double getMaxDepthDiff() const
  852. {
  853. return maxDepthDiff;
  854. }
  855. void setMaxDepthDiff(double val)
  856. {
  857. maxDepthDiff = val;
  858. }
  859. double getMaxPointsPart() const
  860. {
  861. return maxPointsPart;
  862. }
  863. void setMaxPointsPart(double val)
  864. {
  865. maxPointsPart = val;
  866. }
  867. cv::Mat getIterationCounts() const
  868. {
  869. return iterCounts;
  870. }
  871. void setIterationCounts(const cv::Mat &val)
  872. {
  873. iterCounts = val;
  874. }
  875. cv::Mat getMinGradientMagnitudes() const
  876. {
  877. return minGradientMagnitudes;
  878. }
  879. void setMinGradientMagnitudes(const cv::Mat &val)
  880. {
  881. minGradientMagnitudes = val;
  882. }
  883. int getTransformType() const
  884. {
  885. return transformType;
  886. }
  887. void setTransformType(int val)
  888. {
  889. transformType = val;
  890. }
  891. double getMaxTranslation() const
  892. {
  893. return maxTranslation;
  894. }
  895. void setMaxTranslation(double val)
  896. {
  897. maxTranslation = val;
  898. }
  899. double getMaxRotation() const
  900. {
  901. return maxRotation;
  902. }
  903. void setMaxRotation(double val)
  904. {
  905. maxRotation = val;
  906. }
  907. Ptr<RgbdNormals> getNormalsComputer() const
  908. {
  909. return normalsComputer;
  910. }
  911. protected:
  912. virtual void
  913. checkParams() const;
  914. virtual bool
  915. computeImpl(const Ptr<OdometryFrame>& srcFrame, const Ptr<OdometryFrame>& dstFrame, Mat& Rt,
  916. const Mat& initRt) const;
  917. // Some params have commented desired type. It's due to AlgorithmInfo::addParams does not support it now.
  918. /*float*/
  919. double minDepth, maxDepth, maxDepthDiff;
  920. /*float*/
  921. double maxPointsPart;
  922. /*vector<int>*/
  923. Mat iterCounts;
  924. /*vector<float>*/
  925. Mat minGradientMagnitudes;
  926. Mat cameraMatrix;
  927. int transformType;
  928. double maxTranslation, maxRotation;
  929. mutable Ptr<RgbdNormals> normalsComputer;
  930. };
  931. /** Warp the image: compute 3d points from the depth, transform them using given transformation,
  932. * then project color point cloud to an image plane.
  933. * This function can be used to visualize results of the Odometry algorithm.
  934. * @param image The image (of CV_8UC1 or CV_8UC3 type)
  935. * @param depth The depth (of type used in depthTo3d fuction)
  936. * @param mask The mask of used pixels (of CV_8UC1), it can be empty
  937. * @param Rt The transformation that will be applied to the 3d points computed from the depth
  938. * @param cameraMatrix Camera matrix
  939. * @param distCoeff Distortion coefficients
  940. * @param warpedImage The warped image.
  941. * @param warpedDepth The warped depth.
  942. * @param warpedMask The warped mask.
  943. */
  944. CV_EXPORTS
  945. void
  946. warpFrame(const Mat& image, const Mat& depth, const Mat& mask, const Mat& Rt, const Mat& cameraMatrix,
  947. const Mat& distCoeff, Mat& warpedImage, Mat* warpedDepth = 0, Mat* warpedMask = 0);
  948. // TODO Depth interpolation
  949. // Curvature
  950. // Get rescaleDepth return dubles if asked for
  951. //! @}
  952. } /* namespace rgbd */
  953. } /* namespace cv */
  954. #include "opencv2/rgbd/linemod.hpp"
  955. #endif /* __cplusplus */
  956. #endif
  957. /* End of file. */