rapid.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #ifndef OPENCV_RAPID_HPP_
  5. #define OPENCV_RAPID_HPP_
  6. #include <opencv2/core.hpp>
  7. #include <opencv2/imgproc.hpp>
  8. /**
  9. @defgroup rapid silhouette based 3D object tracking
  10. implements "RAPID-a video rate object tracker" @cite harris1990rapid with the dynamic control point extraction of @cite drummond2002real
  11. */
  12. namespace cv
  13. {
  14. namespace rapid
  15. {
  16. //! @addtogroup rapid
  17. //! @{
  18. /**
  19. * Debug draw markers of matched correspondences onto a lineBundle
  20. * @param bundle the lineBundle
  21. * @param cols column coordinates in the line bundle
  22. * @param colors colors for the markers. Defaults to white.
  23. */
  24. CV_EXPORTS_W void drawCorrespondencies(InputOutputArray bundle, InputArray cols,
  25. InputArray colors = noArray());
  26. /**
  27. * Debug draw search lines onto an image
  28. * @param img the output image
  29. * @param locations the source locations of a line bundle
  30. * @param color the line color
  31. */
  32. CV_EXPORTS_W void drawSearchLines(InputOutputArray img, InputArray locations, const Scalar& color);
  33. /**
  34. * Draw a wireframe of a triangle mesh
  35. * @param img the output image
  36. * @param pts2d the 2d points obtained by @ref projectPoints
  37. * @param tris triangle face connectivity
  38. * @param color line color
  39. * @param type line type. See @ref LineTypes.
  40. * @param cullBackface enable back-face culling based on CCW order
  41. */
  42. CV_EXPORTS_W void drawWireframe(InputOutputArray img, InputArray pts2d, InputArray tris,
  43. const Scalar& color, int type = LINE_8, bool cullBackface = false);
  44. /**
  45. * Extract control points from the projected silhouette of a mesh
  46. *
  47. * see @cite drummond2002real Sec 2.1, Step b
  48. * @param num number of control points
  49. * @param len search radius (used to restrict the ROI)
  50. * @param pts3d the 3D points of the mesh
  51. * @param rvec rotation between mesh and camera
  52. * @param tvec translation between mesh and camera
  53. * @param K camera intrinsic
  54. * @param imsize size of the video frame
  55. * @param tris triangle face connectivity
  56. * @param ctl2d the 2D locations of the control points
  57. * @param ctl3d matching 3D points of the mesh
  58. */
  59. CV_EXPORTS_W void extractControlPoints(int num, int len, InputArray pts3d, InputArray rvec, InputArray tvec,
  60. InputArray K, const Size& imsize, InputArray tris, OutputArray ctl2d,
  61. OutputArray ctl3d);
  62. /**
  63. * Extract the line bundle from an image
  64. * @param len the search radius. The bundle will have `2*len + 1` columns.
  65. * @param ctl2d the search lines will be centered at this points and orthogonal to the contour defined by
  66. * them. The bundle will have as many rows.
  67. * @param img the image to read the pixel intensities values from
  68. * @param bundle line bundle image with size `ctl2d.rows() x (2 * len + 1)` and the same type as @p img
  69. * @param srcLocations the source pixel locations of @p bundle in @p img as CV_16SC2
  70. */
  71. CV_EXPORTS_W void extractLineBundle(int len, InputArray ctl2d, InputArray img, OutputArray bundle,
  72. OutputArray srcLocations);
  73. /**
  74. * Find corresponding image locations by searching for a maximal sobel edge along the search line (a single
  75. * row in the bundle)
  76. * @param bundle the line bundle
  77. * @param cols correspondence-position per line in line-bundle-space
  78. * @param response the sobel response for the selected point
  79. */
  80. CV_EXPORTS_W void findCorrespondencies(InputArray bundle, OutputArray cols,
  81. OutputArray response = noArray());
  82. /**
  83. * Collect corresponding 2d and 3d points based on correspondencies and mask
  84. * @param cols correspondence-position per line in line-bundle-space
  85. * @param srcLocations the source image location
  86. * @param pts2d 2d points
  87. * @param pts3d 3d points
  88. * @param mask mask containing non-zero values for the elements to be retained
  89. */
  90. CV_EXPORTS_W void convertCorrespondencies(InputArray cols, InputArray srcLocations, OutputArray pts2d,
  91. InputOutputArray pts3d = noArray(), InputArray mask = noArray());
  92. /**
  93. * High level function to execute a single rapid @cite harris1990rapid iteration
  94. *
  95. * 1. @ref extractControlPoints
  96. * 2. @ref extractLineBundle
  97. * 3. @ref findCorrespondencies
  98. * 4. @ref convertCorrespondencies
  99. * 5. @ref solvePnPRefineLM
  100. *
  101. * @param img the video frame
  102. * @param num number of search lines
  103. * @param len search line radius
  104. * @param pts3d the 3D points of the mesh
  105. * @param tris triangle face connectivity
  106. * @param K camera matrix
  107. * @param rvec rotation between mesh and camera. Input values are used as an initial solution.
  108. * @param tvec translation between mesh and camera. Input values are used as an initial solution.
  109. * @param rmsd the 2d reprojection difference
  110. * @return ratio of search lines that could be extracted and matched
  111. */
  112. CV_EXPORTS_W float rapid(InputArray img, int num, int len, InputArray pts3d, InputArray tris, InputArray K,
  113. InputOutputArray rvec, InputOutputArray tvec, CV_OUT double* rmsd = 0);
  114. /// Abstract base class for stateful silhouette trackers
  115. class CV_EXPORTS_W Tracker : public Algorithm
  116. {
  117. public:
  118. virtual ~Tracker();
  119. CV_WRAP virtual float
  120. compute(InputArray img, int num, int len, InputArray K, InputOutputArray rvec, InputOutputArray tvec,
  121. const TermCriteria& termcrit = TermCriteria(TermCriteria::MAX_ITER | TermCriteria::EPS, 5, 1.5)) = 0;
  122. CV_WRAP virtual void clearState() = 0;
  123. };
  124. /// wrapper around @ref rapid function for uniform access
  125. class CV_EXPORTS_W Rapid : public Tracker
  126. {
  127. public:
  128. CV_WRAP static Ptr<Rapid> create(InputArray pts3d, InputArray tris);
  129. };
  130. /** implements "Optimal local searching for fast and robust textureless 3D object tracking in highly
  131. * cluttered backgrounds" @cite seo2013optimal
  132. */
  133. class CV_EXPORTS_W OLSTracker : public Tracker
  134. {
  135. public:
  136. CV_WRAP static Ptr<OLSTracker> create(InputArray pts3d, InputArray tris, int histBins = 8, uchar sobelThesh = 10);
  137. };
  138. /** implements "Global optimal searching for textureless 3D object tracking" @cite wang2015global
  139. */
  140. class CV_EXPORTS_W GOSTracker : public Tracker
  141. {
  142. public:
  143. CV_WRAP static Ptr<OLSTracker> create(InputArray pts3d, InputArray tris, int histBins = 4, uchar sobelThesh = 10);
  144. };
  145. //! @}
  146. } /* namespace rapid */
  147. } /* namespace cv */
  148. #endif /* OPENCV_RAPID_HPP_ */