cudastereo.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
  14. // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
  15. // Third party copyrights are property of their respective owners.
  16. //
  17. // Redistribution and use in source and binary forms, with or without modification,
  18. // are permitted provided that the following conditions are met:
  19. //
  20. // * Redistribution's of source code must retain the above copyright notice,
  21. // this list of conditions and the following disclaimer.
  22. //
  23. // * Redistribution's in binary form must reproduce the above copyright notice,
  24. // this list of conditions and the following disclaimer in the documentation
  25. // and/or other materials provided with the distribution.
  26. //
  27. // * The name of the copyright holders may not be used to endorse or promote products
  28. // derived from this software without specific prior written permission.
  29. //
  30. // This software is provided by the copyright holders and contributors "as is" and
  31. // any express or implied warranties, including, but not limited to, the implied
  32. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  33. // In no event shall the Intel Corporation or contributors be liable for any direct,
  34. // indirect, incidental, special, exemplary, or consequential damages
  35. // (including, but not limited to, procurement of substitute goods or services;
  36. // loss of use, data, or profits; or business interruption) however caused
  37. // and on any theory of liability, whether in contract, strict liability,
  38. // or tort (including negligence or otherwise) arising in any way out of
  39. // the use of this software, even if advised of the possibility of such damage.
  40. //
  41. //M*/
  42. #ifndef OPENCV_CUDASTEREO_HPP
  43. #define OPENCV_CUDASTEREO_HPP
  44. #ifndef __cplusplus
  45. # error cudastereo.hpp header must be compiled as C++
  46. #endif
  47. #include "opencv2/core/cuda.hpp"
  48. #include "opencv2/calib3d.hpp"
  49. /**
  50. @addtogroup cuda
  51. @{
  52. @defgroup cudastereo Stereo Correspondence
  53. @}
  54. */
  55. namespace cv { namespace cuda {
  56. //! @addtogroup cudastereo
  57. //! @{
  58. /////////////////////////////////////////
  59. // StereoBM
  60. /** @brief Class computing stereo correspondence (disparity map) using the block matching algorithm. :
  61. @sa StereoBM
  62. */
  63. class CV_EXPORTS StereoBM : public cv::StereoBM
  64. {
  65. public:
  66. using cv::StereoBM::compute;
  67. virtual void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream) = 0;
  68. };
  69. /** @brief Creates StereoBM object.
  70. @param numDisparities the disparity search range. For each pixel algorithm will find the best
  71. disparity from 0 (default minimum disparity) to numDisparities. The search range can then be
  72. shifted by changing the minimum disparity.
  73. @param blockSize the linear size of the blocks compared by the algorithm. The size should be odd
  74. (as the block is centered at the current pixel). Larger block size implies smoother, though less
  75. accurate disparity map. Smaller block size gives more detailed disparity map, but there is higher
  76. chance for algorithm to find a wrong correspondence.
  77. */
  78. CV_EXPORTS Ptr<cuda::StereoBM> createStereoBM(int numDisparities = 64, int blockSize = 19);
  79. /////////////////////////////////////////
  80. // StereoBeliefPropagation
  81. /** @brief Class computing stereo correspondence using the belief propagation algorithm. :
  82. The class implements algorithm described in @cite Felzenszwalb2006 . It can compute own data cost
  83. (using a truncated linear model) or use a user-provided data cost.
  84. @note
  85. StereoBeliefPropagation requires a lot of memory for message storage:
  86. \f[width \_ step \cdot height \cdot ndisp \cdot 4 \cdot (1 + 0.25)\f]
  87. and for data cost storage:
  88. \f[width\_step \cdot height \cdot ndisp \cdot (1 + 0.25 + 0.0625 + \dotsm + \frac{1}{4^{levels}})\f]
  89. width_step is the number of bytes in a line including padding.
  90. StereoBeliefPropagation uses a truncated linear model for the data cost and discontinuity terms:
  91. \f[DataCost = data \_ weight \cdot \min ( \lvert Img_Left(x,y)-Img_Right(x-d,y) \rvert , max \_ data \_ term)\f]
  92. \f[DiscTerm = \min (disc \_ single \_ jump \cdot \lvert f_1-f_2 \rvert , max \_ disc \_ term)\f]
  93. For more details, see @cite Felzenszwalb2006 .
  94. By default, StereoBeliefPropagation uses floating-point arithmetics and the CV_32FC1 type for
  95. messages. But it can also use fixed-point arithmetics and the CV_16SC1 message type for better
  96. performance. To avoid an overflow in this case, the parameters must satisfy the following
  97. requirement:
  98. \f[10 \cdot 2^{levels-1} \cdot max \_ data \_ term < SHRT \_ MAX\f]
  99. @sa StereoMatcher
  100. */
  101. class CV_EXPORTS StereoBeliefPropagation : public cv::StereoMatcher
  102. {
  103. public:
  104. using cv::StereoMatcher::compute;
  105. /** @overload */
  106. virtual void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream) = 0;
  107. /** @brief Enables the stereo correspondence operator that finds the disparity for the specified data cost.
  108. @param data User-specified data cost, a matrix of msg_type type and
  109. Size(\<image columns\>\*ndisp, \<image rows\>) size.
  110. @param disparity Output disparity map. If disparity is empty, the output type is CV_16SC1 .
  111. Otherwise, the type is retained. In 16-bit signed format, the disparity values do not have
  112. fractional bits.
  113. @param stream Stream for the asynchronous version.
  114. */
  115. virtual void compute(InputArray data, OutputArray disparity, Stream& stream = Stream::Null()) = 0;
  116. //! number of BP iterations on each level
  117. virtual int getNumIters() const = 0;
  118. virtual void setNumIters(int iters) = 0;
  119. //! number of levels
  120. virtual int getNumLevels() const = 0;
  121. virtual void setNumLevels(int levels) = 0;
  122. //! truncation of data cost
  123. virtual double getMaxDataTerm() const = 0;
  124. virtual void setMaxDataTerm(double max_data_term) = 0;
  125. //! data weight
  126. virtual double getDataWeight() const = 0;
  127. virtual void setDataWeight(double data_weight) = 0;
  128. //! truncation of discontinuity cost
  129. virtual double getMaxDiscTerm() const = 0;
  130. virtual void setMaxDiscTerm(double max_disc_term) = 0;
  131. //! discontinuity single jump
  132. virtual double getDiscSingleJump() const = 0;
  133. virtual void setDiscSingleJump(double disc_single_jump) = 0;
  134. //! type for messages (CV_16SC1 or CV_32FC1)
  135. virtual int getMsgType() const = 0;
  136. virtual void setMsgType(int msg_type) = 0;
  137. /** @brief Uses a heuristic method to compute the recommended parameters ( ndisp, iters and levels ) for the
  138. specified image size ( width and height ).
  139. */
  140. static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels);
  141. };
  142. /** @brief Creates StereoBeliefPropagation object.
  143. @param ndisp Number of disparities.
  144. @param iters Number of BP iterations on each level.
  145. @param levels Number of levels.
  146. @param msg_type Type for messages. CV_16SC1 and CV_32FC1 types are supported.
  147. */
  148. CV_EXPORTS Ptr<cuda::StereoBeliefPropagation>
  149. createStereoBeliefPropagation(int ndisp = 64, int iters = 5, int levels = 5, int msg_type = CV_32F);
  150. /////////////////////////////////////////
  151. // StereoConstantSpaceBP
  152. /** @brief Class computing stereo correspondence using the constant space belief propagation algorithm. :
  153. The class implements algorithm described in @cite Yang2010 . StereoConstantSpaceBP supports both local
  154. minimum and global minimum data cost initialization algorithms. For more details, see the paper
  155. mentioned above. By default, a local algorithm is used. To enable a global algorithm, set
  156. use_local_init_data_cost to false .
  157. StereoConstantSpaceBP uses a truncated linear model for the data cost and discontinuity terms:
  158. \f[DataCost = data \_ weight \cdot \min ( \lvert I_2-I_1 \rvert , max \_ data \_ term)\f]
  159. \f[DiscTerm = \min (disc \_ single \_ jump \cdot \lvert f_1-f_2 \rvert , max \_ disc \_ term)\f]
  160. For more details, see @cite Yang2010 .
  161. By default, StereoConstantSpaceBP uses floating-point arithmetics and the CV_32FC1 type for
  162. messages. But it can also use fixed-point arithmetics and the CV_16SC1 message type for better
  163. performance. To avoid an overflow in this case, the parameters must satisfy the following
  164. requirement:
  165. \f[10 \cdot 2^{levels-1} \cdot max \_ data \_ term < SHRT \_ MAX\f]
  166. */
  167. class CV_EXPORTS StereoConstantSpaceBP : public cuda::StereoBeliefPropagation
  168. {
  169. public:
  170. //! number of active disparity on the first level
  171. virtual int getNrPlane() const = 0;
  172. virtual void setNrPlane(int nr_plane) = 0;
  173. virtual bool getUseLocalInitDataCost() const = 0;
  174. virtual void setUseLocalInitDataCost(bool use_local_init_data_cost) = 0;
  175. /** @brief Uses a heuristic method to compute parameters (ndisp, iters, levelsand nrplane) for the specified
  176. image size (widthand height).
  177. */
  178. static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane);
  179. };
  180. /** @brief Creates StereoConstantSpaceBP object.
  181. @param ndisp Number of disparities.
  182. @param iters Number of BP iterations on each level.
  183. @param levels Number of levels.
  184. @param nr_plane Number of disparity levels on the first level.
  185. @param msg_type Type for messages. CV_16SC1 and CV_32FC1 types are supported.
  186. */
  187. CV_EXPORTS Ptr<cuda::StereoConstantSpaceBP>
  188. createStereoConstantSpaceBP(int ndisp = 128, int iters = 8, int levels = 4, int nr_plane = 4, int msg_type = CV_32F);
  189. /////////////////////////////////////////
  190. // DisparityBilateralFilter
  191. /** @brief Class refining a disparity map using joint bilateral filtering. :
  192. The class implements @cite Yang2010 algorithm.
  193. */
  194. class CV_EXPORTS DisparityBilateralFilter : public cv::Algorithm
  195. {
  196. public:
  197. /** @brief Refines a disparity map using joint bilateral filtering.
  198. @param disparity Input disparity map. CV_8UC1 and CV_16SC1 types are supported.
  199. @param image Input image. CV_8UC1 and CV_8UC3 types are supported.
  200. @param dst Destination disparity map. It has the same size and type as disparity .
  201. @param stream Stream for the asynchronous version.
  202. */
  203. virtual void apply(InputArray disparity, InputArray image, OutputArray dst, Stream& stream = Stream::Null()) = 0;
  204. virtual int getNumDisparities() const = 0;
  205. virtual void setNumDisparities(int numDisparities) = 0;
  206. virtual int getRadius() const = 0;
  207. virtual void setRadius(int radius) = 0;
  208. virtual int getNumIters() const = 0;
  209. virtual void setNumIters(int iters) = 0;
  210. //! truncation of data continuity
  211. virtual double getEdgeThreshold() const = 0;
  212. virtual void setEdgeThreshold(double edge_threshold) = 0;
  213. //! truncation of disparity continuity
  214. virtual double getMaxDiscThreshold() const = 0;
  215. virtual void setMaxDiscThreshold(double max_disc_threshold) = 0;
  216. //! filter range sigma
  217. virtual double getSigmaRange() const = 0;
  218. virtual void setSigmaRange(double sigma_range) = 0;
  219. };
  220. /** @brief Creates DisparityBilateralFilter object.
  221. @param ndisp Number of disparities.
  222. @param radius Filter radius.
  223. @param iters Number of iterations.
  224. */
  225. CV_EXPORTS Ptr<cuda::DisparityBilateralFilter>
  226. createDisparityBilateralFilter(int ndisp = 64, int radius = 3, int iters = 1);
  227. /////////////////////////////////////////
  228. // Utility
  229. /** @brief Reprojects a disparity image to 3D space.
  230. @param disp Input single-channel 8-bit unsigned, 16-bit signed, 32-bit signed or 32-bit
  231. floating-point disparity image. If 16-bit signed format is used, the values are assumed to have no
  232. fractional bits.
  233. @param xyzw Output 3- or 4-channel floating-point image of the same size as disp . Each element of
  234. xyzw(x,y) contains 3D coordinates (x,y,z) or (x,y,z,1) of the point (x,y) , computed from the
  235. disparity map.
  236. @param Q \f$4 \times 4\f$ perspective transformation matrix that can be obtained via stereoRectify .
  237. @param dst_cn The number of channels for output image. Can be 3 or 4.
  238. @param stream Stream for the asynchronous version.
  239. @sa reprojectImageTo3D
  240. */
  241. CV_EXPORTS void reprojectImageTo3D(InputArray disp, OutputArray xyzw, InputArray Q, int dst_cn = 4, Stream& stream = Stream::Null());
  242. /** @brief Colors a disparity image.
  243. @param src_disp Input single-channel 8-bit unsigned, 16-bit signed, 32-bit signed or 32-bit
  244. floating-point disparity image. If 16-bit signed format is used, the values are assumed to have no
  245. fractional bits.
  246. @param dst_disp Output disparity image. It has the same size as src_disp. The type is CV_8UC4
  247. in BGRA format (alpha = 255).
  248. @param ndisp Number of disparities.
  249. @param stream Stream for the asynchronous version.
  250. This function draws a colored disparity map by converting disparity values from [0..ndisp) interval
  251. first to HSV color space (where different disparity values correspond to different hues) and then
  252. converting the pixels to RGB for visualization.
  253. */
  254. CV_EXPORTS void drawColorDisp(InputArray src_disp, OutputArray dst_disp, int ndisp, Stream& stream = Stream::Null());
  255. //! @}
  256. }} // namespace cv { namespace cuda {
  257. #endif /* OPENCV_CUDASTEREO_HPP */