cudastereo.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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_W StereoBM : public cv::StereoBM
  64. {
  65. public:
  66. using cv::StereoBM::compute;
  67. CV_WRAP 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_W 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_W StereoBeliefPropagation : public cv::StereoMatcher
  102. {
  103. public:
  104. using cv::StereoMatcher::compute;
  105. /** @overload */
  106. CV_WRAP 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. CV_WRAP virtual void compute(InputArray data, OutputArray disparity, Stream& stream = Stream::Null()) = 0;
  116. //! number of BP iterations on each level
  117. CV_WRAP virtual int getNumIters() const = 0;
  118. CV_WRAP virtual void setNumIters(int iters) = 0;
  119. //! number of levels
  120. CV_WRAP virtual int getNumLevels() const = 0;
  121. CV_WRAP virtual void setNumLevels(int levels) = 0;
  122. //! truncation of data cost
  123. CV_WRAP virtual double getMaxDataTerm() const = 0;
  124. CV_WRAP virtual void setMaxDataTerm(double max_data_term) = 0;
  125. //! data weight
  126. CV_WRAP virtual double getDataWeight() const = 0;
  127. CV_WRAP virtual void setDataWeight(double data_weight) = 0;
  128. //! truncation of discontinuity cost
  129. CV_WRAP virtual double getMaxDiscTerm() const = 0;
  130. CV_WRAP virtual void setMaxDiscTerm(double max_disc_term) = 0;
  131. //! discontinuity single jump
  132. CV_WRAP virtual double getDiscSingleJump() const = 0;
  133. CV_WRAP virtual void setDiscSingleJump(double disc_single_jump) = 0;
  134. //! type for messages (CV_16SC1 or CV_32FC1)
  135. CV_WRAP virtual int getMsgType() const = 0;
  136. CV_WRAP 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. CV_WRAP 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_W 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_W StereoConstantSpaceBP : public cuda::StereoBeliefPropagation
  168. {
  169. public:
  170. //! number of active disparity on the first level
  171. CV_WRAP virtual int getNrPlane() const = 0;
  172. CV_WRAP virtual void setNrPlane(int nr_plane) = 0;
  173. CV_WRAP virtual bool getUseLocalInitDataCost() const = 0;
  174. CV_WRAP 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. CV_WRAP 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_W 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. // StereoSGM
  191. /** @brief The class implements the modified H. Hirschmuller algorithm @cite HH08.
  192. Limitation and difference are as follows:
  193. - By default, the algorithm uses only 4 directions which are horizontal and vertical path instead of 8.
  194. Set mode=StereoSGM::MODE_HH in createStereoSGM to run the full variant of the algorithm.
  195. - Mutual Information cost function is not implemented.
  196. Instead, Center-Symmetric Census Transform with \f$9 \times 7\f$ window size from @cite Spangenberg2013
  197. is used for robustness.
  198. @sa cv::StereoSGBM
  199. */
  200. class CV_EXPORTS_W StereoSGM : public cv::StereoSGBM
  201. {
  202. public:
  203. /** @brief Computes disparity map for the specified stereo pair
  204. @param left Left 8-bit or 16-bit unsigned single-channel image.
  205. @param right Right image of the same size and the same type as the left one.
  206. @param disparity Output disparity map. It has the same size as the input images.
  207. StereoSGM computes 16-bit fixed-point disparity map (where each disparity value has 4 fractional bits).
  208. */
  209. CV_WRAP virtual void compute(InputArray left, InputArray right, OutputArray disparity) CV_OVERRIDE = 0;
  210. /** @brief Computes disparity map with specified CUDA Stream
  211. @sa compute
  212. */
  213. CV_WRAP_AS(compute_with_stream) virtual void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream) = 0;
  214. };
  215. /** @brief Creates StereoSGM object.
  216. @param minDisparity Minimum possible disparity value. Normally, it is zero but sometimes rectification algorithms can shift images, so this parameter needs to be adjusted accordingly.
  217. @param numDisparities Maximum disparity minus minimum disparity. The value must be 64, 128 or 256.
  218. @param P1 The first parameter controlling the disparity smoothness.This parameter is used for the case of slanted surfaces (not fronto parallel).
  219. @param P2 The second parameter controlling the disparity smoothness.This parameter is used for "solving" the depth discontinuities problem.
  220. @param uniquenessRatio Margin in percentage by which the best (minimum) computed cost function
  221. value should "win" the second best value to consider the found match correct. Normally, a value
  222. within the 5-15 range is good enough.
  223. @param mode Set it to StereoSGM::MODE_HH to run the full-scale two-pass dynamic programming algorithm.
  224. It will consume O(W\*H\*numDisparities) bytes. By default, it is set to StereoSGM::MODE_HH4.
  225. */
  226. CV_EXPORTS_W Ptr<cuda::StereoSGM> createStereoSGM(int minDisparity = 0, int numDisparities = 128, int P1 = 10, int P2 = 120, int uniquenessRatio = 5, int mode = cv::cuda::StereoSGM::MODE_HH4);
  227. /////////////////////////////////////////
  228. // DisparityBilateralFilter
  229. /** @brief Class refining a disparity map using joint bilateral filtering. :
  230. The class implements @cite Yang2010 algorithm.
  231. */
  232. class CV_EXPORTS_W DisparityBilateralFilter : public cv::Algorithm
  233. {
  234. public:
  235. /** @brief Refines a disparity map using joint bilateral filtering.
  236. @param disparity Input disparity map. CV_8UC1 and CV_16SC1 types are supported.
  237. @param image Input image. CV_8UC1 and CV_8UC3 types are supported.
  238. @param dst Destination disparity map. It has the same size and type as disparity .
  239. @param stream Stream for the asynchronous version.
  240. */
  241. CV_WRAP virtual void apply(InputArray disparity, InputArray image, OutputArray dst, Stream& stream = Stream::Null()) = 0;
  242. CV_WRAP virtual int getNumDisparities() const = 0;
  243. CV_WRAP virtual void setNumDisparities(int numDisparities) = 0;
  244. CV_WRAP virtual int getRadius() const = 0;
  245. CV_WRAP virtual void setRadius(int radius) = 0;
  246. CV_WRAP virtual int getNumIters() const = 0;
  247. CV_WRAP virtual void setNumIters(int iters) = 0;
  248. //! truncation of data continuity
  249. CV_WRAP virtual double getEdgeThreshold() const = 0;
  250. CV_WRAP virtual void setEdgeThreshold(double edge_threshold) = 0;
  251. //! truncation of disparity continuity
  252. CV_WRAP virtual double getMaxDiscThreshold() const = 0;
  253. CV_WRAP virtual void setMaxDiscThreshold(double max_disc_threshold) = 0;
  254. //! filter range sigma
  255. CV_WRAP virtual double getSigmaRange() const = 0;
  256. CV_WRAP virtual void setSigmaRange(double sigma_range) = 0;
  257. };
  258. /** @brief Creates DisparityBilateralFilter object.
  259. @param ndisp Number of disparities.
  260. @param radius Filter radius.
  261. @param iters Number of iterations.
  262. */
  263. CV_EXPORTS_W Ptr<cuda::DisparityBilateralFilter>
  264. createDisparityBilateralFilter(int ndisp = 64, int radius = 3, int iters = 1);
  265. /////////////////////////////////////////
  266. // Utility
  267. /** @brief Reprojects a disparity image to 3D space.
  268. @param disp Input single-channel 8-bit unsigned, 16-bit signed, 32-bit signed or 32-bit
  269. floating-point disparity image. If 16-bit signed format is used, the values are assumed to have no
  270. fractional bits.
  271. @param xyzw Output 3- or 4-channel floating-point image of the same size as disp . Each element of
  272. xyzw(x,y) contains 3D coordinates (x,y,z) or (x,y,z,1) of the point (x,y) , computed from the
  273. disparity map.
  274. @param Q \f$4 \times 4\f$ perspective transformation matrix that can be obtained via stereoRectify .
  275. @param dst_cn The number of channels for output image. Can be 3 or 4.
  276. @param stream Stream for the asynchronous version.
  277. @sa reprojectImageTo3D
  278. */
  279. CV_EXPORTS_W void reprojectImageTo3D(InputArray disp, OutputArray xyzw, InputArray Q, int dst_cn = 4, Stream& stream = Stream::Null());
  280. /** @brief Colors a disparity image.
  281. @param src_disp Input single-channel 8-bit unsigned, 16-bit signed, 32-bit signed or 32-bit
  282. floating-point disparity image. If 16-bit signed format is used, the values are assumed to have no
  283. fractional bits.
  284. @param dst_disp Output disparity image. It has the same size as src_disp. The type is CV_8UC4
  285. in BGRA format (alpha = 255).
  286. @param ndisp Number of disparities.
  287. @param stream Stream for the asynchronous version.
  288. This function draws a colored disparity map by converting disparity values from [0..ndisp) interval
  289. first to HSV color space (where different disparity values correspond to different hues) and then
  290. converting the pixels to RGB for visualization.
  291. */
  292. CV_EXPORTS_W void drawColorDisp(InputArray src_disp, OutputArray dst_disp, int ndisp, Stream& stream = Stream::Null());
  293. //! @}
  294. }} // namespace cv { namespace cuda {
  295. #endif /* OPENCV_CUDASTEREO_HPP */