Merge pull request #1014 from jet47:gpustereo-refactoring

pull/1038/head
Roman Donchenko 12 years ago committed by OpenCV Buildbot
commit eff6dccb3b
  1. 399
      modules/gpustereo/doc/stereo.rst
  2. 234
      modules/gpustereo/include/opencv2/gpustereo.hpp
  3. 17
      modules/gpustereo/perf/perf_stereo.cpp
  4. 138
      modules/gpustereo/src/disparity_bilateral_filter.cpp
  5. 1
      modules/gpustereo/src/precomp.hpp
  6. 145
      modules/gpustereo/src/stereobm.cpp
  7. 432
      modules/gpustereo/src/stereobp.cpp
  8. 378
      modules/gpustereo/src/stereocsbp.cpp
  9. 36
      modules/gpustereo/src/util.cpp
  10. 17
      modules/gpustereo/test/test_stereo.cpp
  11. 13
      samples/gpu/driver_api_stereo_multi.cpp
  12. 94
      samples/gpu/stereo_match.cpp
  13. 13
      samples/gpu/stereo_multi.cpp

@ -5,135 +5,75 @@ Stereo Correspondence
gpu::StereoBM_GPU
-----------------
.. ocv:class:: gpu::StereoBM_GPU
gpu::StereoBM
-------------
.. ocv:class:: gpu::StereoBM : public cv::StereoBM
Class computing stereo correspondence (disparity map) using the block matching algorithm. ::
class StereoBM_GPU
{
public:
enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
StereoBM_GPU();
StereoBM_GPU(int preset, int ndisparities = DEFAULT_NDISP,
int winSize = DEFAULT_WINSZ);
void operator() (const GpuMat& left, const GpuMat& right,
GpuMat& disparity, Stream& stream = Stream::Null());
static bool checkIfGpuCallReasonable();
int preset;
int ndisp;
int winSize;
float avergeTexThreshold;
...
};
The class also performs pre- and post-filtering steps: Sobel pre-filtering (if ``PREFILTER_XSOBEL`` flag is set) and low textureness filtering (if ``averageTexThreshols > 0`` ). If ``avergeTexThreshold = 0`` , low textureness filtering is disabled. Otherwise, the disparity is set to 0 in each point ``(x, y)`` , where for the left image
.. math::
\sum HorizontalGradiensInWindow(x, y, winSize) < (winSize \cdot winSize) \cdot avergeTexThreshold
This means that the input left image is low textured.
gpu::StereoBM_GPU::StereoBM_GPU
-----------------------------------
Enables :ocv:class:`gpu::StereoBM_GPU` constructors.
.. seealso:: :ocv:class:`StereoBM`
.. ocv:function:: gpu::StereoBM_GPU::StereoBM_GPU()
.. ocv:function:: gpu::StereoBM_GPU::StereoBM_GPU(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ)
:param preset: Parameter presetting:
gpu::createStereoBM
-------------------
Creates StereoBM object.
* **BASIC_PRESET** Basic mode without pre-processing.
.. ocv:function:: Ptr<gpu::StereoBM> gpu::createStereoBM(int numDisparities = 64, int blockSize = 19)
* **PREFILTER_XSOBEL** Sobel pre-filtering mode.
:param numDisparities: the disparity search range. For each pixel algorithm will find the best disparity from 0 (default minimum disparity) to ``numDisparities``. The search range can then be shifted by changing the minimum disparity.
:param ndisparities: Number of disparities. It must be a multiple of 8 and less or equal to 256.
:param winSize: Block size.
gpu::StereoBM_GPU::operator ()
----------------------------------
Enables the stereo correspondence operator that finds the disparity for the specified rectified stereo pair.
.. ocv:function:: void gpu::StereoBM_GPU::operator ()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null())
:param left: Left image. Only ``CV_8UC1`` type is supported.
:param right: Right image with the same size and the same type as the left one.
:param disparity: Output disparity map. It is a ``CV_8UC1`` image with the same size as the input images.
:param stream: Stream for the asynchronous version.
gpu::StereoBM_GPU::checkIfGpuCallReasonable
-----------------------------------------------
Uses a heuristic method to estimate whether the current GPU is faster than the CPU in this algorithm. It queries the currently active device.
.. ocv:function:: bool gpu::StereoBM_GPU::checkIfGpuCallReasonable()
:param blockSize: the linear size of the blocks compared by the algorithm. The size should be odd (as the block is centered at the current pixel). Larger block size implies smoother, though less accurate disparity map. Smaller block size gives more detailed disparity map, but there is higher chance for algorithm to find a wrong correspondence.
gpu::StereoBeliefPropagation
----------------------------
.. ocv:class:: gpu::StereoBeliefPropagation
.. ocv:class:: gpu::StereoBeliefPropagation : public cv::StereoMatcher
Class computing stereo correspondence using the belief propagation algorithm. ::
class StereoBeliefPropagation
class CV_EXPORTS StereoBeliefPropagation : public cv::StereoMatcher
{
public:
enum { DEFAULT_NDISP = 64 };
enum { DEFAULT_ITERS = 5 };
enum { DEFAULT_LEVELS = 5 };
using cv::StereoMatcher::compute;
static void estimateRecommendedParams(int width, int height,
int& ndisp, int& iters, int& levels);
virtual void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream) = 0;
explicit StereoBeliefPropagation(int ndisp = DEFAULT_NDISP,
int iters = DEFAULT_ITERS,
int levels = DEFAULT_LEVELS,
int msg_type = CV_32F);
StereoBeliefPropagation(int ndisp, int iters, int levels,
float max_data_term, float data_weight,
float max_disc_term, float disc_single_jump,
int msg_type = CV_32F);
//! version for user specified data term
virtual void compute(InputArray data, OutputArray disparity, Stream& stream = Stream::Null()) = 0;
void operator()(const GpuMat& left, const GpuMat& right,
GpuMat& disparity, Stream& stream = Stream::Null());
void operator()(const GpuMat& data, GpuMat& disparity, Stream& stream = Stream::Null());
//! number of BP iterations on each level
virtual int getNumIters() const = 0;
virtual void setNumIters(int iters) = 0;
int ndisp;
//! number of levels
virtual int getNumLevels() const = 0;
virtual void setNumLevels(int levels) = 0;
int iters;
int levels;
//! truncation of data cost
virtual double getMaxDataTerm() const = 0;
virtual void setMaxDataTerm(double max_data_term) = 0;
float max_data_term;
float data_weight;
float max_disc_term;
float disc_single_jump;
//! data weight
virtual double getDataWeight() const = 0;
virtual void setDataWeight(double data_weight) = 0;
int msg_type;
//! truncation of discontinuity cost
virtual double getMaxDiscTerm() const = 0;
virtual void setMaxDiscTerm(double max_disc_term) = 0;
...
//! discontinuity single jump
virtual double getDiscSingleJump() const = 0;
virtual void setDiscSingleJump(double disc_single_jump) = 0;
virtual int getMsgType() const = 0;
virtual void setMsgType(int msg_type) = 0;
static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels);
};
The class implements algorithm described in [Felzenszwalb2006]_ . It can compute own data cost (using a truncated linear model) or use a user-provided data cost.
.. note::
@ -152,71 +92,57 @@ The class implements algorithm described in [Felzenszwalb2006]_ . It can compute
``width_step`` is the number of bytes in a line including padding.
``StereoBeliefPropagation`` uses a truncated linear model for the data cost and discontinuity terms:
.. math::
gpu::StereoBeliefPropagation::StereoBeliefPropagation
---------------------------------------------------------
Enables the :ocv:class:`gpu::StereoBeliefPropagation` constructors.
.. ocv:function:: gpu::StereoBeliefPropagation::StereoBeliefPropagation(int ndisp = DEFAULT_NDISP, int iters = DEFAULT_ITERS, int levels = DEFAULT_LEVELS, int msg_type = CV_32F)
.. ocv:function:: gpu::StereoBeliefPropagation::StereoBeliefPropagation(int ndisp, int iters, int levels, float max_data_term, float data_weight, float max_disc_term, float disc_single_jump, int msg_type = CV_32F)
:param ndisp: Number of disparities.
:param iters: Number of BP iterations on each level.
DataCost = data \_ weight \cdot \min ( \lvert Img_Left(x,y)-Img_Right(x-d,y) \rvert , max \_ data \_ term)
:param levels: Number of levels.
.. math::
:param max_data_term: Threshold for data cost truncation.
DiscTerm = \min (disc \_ single \_ jump \cdot \lvert f_1-f_2 \rvert , max \_ disc \_ term)
:param data_weight: Data weight.
For more details, see [Felzenszwalb2006]_.
:param max_disc_term: Threshold for discontinuity truncation.
By default, ``StereoBeliefPropagation`` uses floating-point arithmetics and the ``CV_32FC1`` type for messages. But it can also use fixed-point arithmetics and the ``CV_16SC1`` message type for better performance. To avoid an overflow in this case, the parameters must satisfy the following requirement:
:param disc_single_jump: Discontinuity single jump.
.. math::
:param msg_type: Type for messages. ``CV_16SC1`` and ``CV_32FC1`` types are supported.
10 \cdot 2^{levels-1} \cdot max \_ data \_ term < SHRT \_ MAX
``StereoBeliefPropagation`` uses a truncated linear model for the data cost and discontinuity terms:
.. seealso:: :ocv:class:`StereoMatcher`
.. math::
DataCost = data \_ weight \cdot \min ( \lvert Img_Left(x,y)-Img_Right(x-d,y) \rvert , max \_ data \_ term)
.. math::
gpu::createStereoBeliefPropagation
----------------------------------
Creates StereoBeliefPropagation object.
DiscTerm = \min (disc \_ single \_ jump \cdot \lvert f_1-f_2 \rvert , max \_ disc \_ term)
.. ocv:function:: Ptr<gpu::StereoBeliefPropagation> gpu::createStereoBeliefPropagation(int ndisp = 64, int iters = 5, int levels = 5, int msg_type = CV_32F)
For more details, see [Felzenszwalb2006]_.
:param ndisp: Number of disparities.
By default, :ocv:class:`gpu::StereoBeliefPropagation` uses floating-point arithmetics and the ``CV_32FC1`` type for messages. But it can also use fixed-point arithmetics and the ``CV_16SC1`` message type for better performance. To avoid an overflow in this case, the parameters must satisfy the following requirement:
:param iters: Number of BP iterations on each level.
.. math::
:param levels: Number of levels.
10 \cdot 2^{levels-1} \cdot max \_ data \_ term < SHRT \_ MAX
:param msg_type: Type for messages. ``CV_16SC1`` and ``CV_32FC1`` types are supported.
gpu::StereoBeliefPropagation::estimateRecommendedParams
-----------------------------------------------------------
-------------------------------------------------------
Uses a heuristic method to compute the recommended parameters ( ``ndisp``, ``iters`` and ``levels`` ) for the specified image size ( ``width`` and ``height`` ).
.. ocv:function:: void gpu::StereoBeliefPropagation::estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels)
gpu::StereoBeliefPropagation::operator ()
---------------------------------------------
Enables the stereo correspondence operator that finds the disparity for the specified rectified stereo pair or data cost.
gpu::StereoBeliefPropagation::compute
-------------------------------------
Enables the stereo correspondence operator that finds the disparity for the specified data cost.
.. ocv:function:: void gpu::StereoBeliefPropagation::operator ()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null())
.. ocv:function:: void gpu::StereoBeliefPropagation::operator ()(const GpuMat& data, GpuMat& disparity, Stream& stream = Stream::Null())
:param left: Left image. ``CV_8UC1`` , ``CV_8UC3`` and ``CV_8UC4`` types are supported.
:param right: Right image with the same size and the same type as the left one.
.. ocv:function:: void gpu::StereoBeliefPropagation::compute(InputArray data, OutputArray disparity, Stream& stream = Stream::Null())
:param data: User-specified data cost, a matrix of ``msg_type`` type and ``Size(<image columns>*ndisp, <image rows>)`` size.
@ -228,89 +154,26 @@ Enables the stereo correspondence operator that finds the disparity for the spec
gpu::StereoConstantSpaceBP
--------------------------
.. ocv:class:: gpu::StereoConstantSpaceBP
.. ocv:class:: gpu::StereoConstantSpaceBP : public gpu::StereoBeliefPropagation
Class computing stereo correspondence using the constant space belief propagation algorithm. ::
class StereoConstantSpaceBP
class CV_EXPORTS StereoConstantSpaceBP : public gpu::StereoBeliefPropagation
{
public:
enum { DEFAULT_NDISP = 128 };
enum { DEFAULT_ITERS = 8 };
enum { DEFAULT_LEVELS = 4 };
enum { DEFAULT_NR_PLANE = 4 };
static void estimateRecommendedParams(int width, int height,
int& ndisp, int& iters, int& levels, int& nr_plane);
explicit StereoConstantSpaceBP(int ndisp = DEFAULT_NDISP,
int iters = DEFAULT_ITERS,
int levels = DEFAULT_LEVELS,
int nr_plane = DEFAULT_NR_PLANE,
int msg_type = CV_32F);
StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
float max_data_term, float data_weight,
float max_disc_term, float disc_single_jump,
int min_disp_th = 0,
int msg_type = CV_32F);
void operator()(const GpuMat& left, const GpuMat& right,
GpuMat& disparity, Stream& stream = Stream::Null());
int ndisp;
int iters;
int levels;
int nr_plane;
//! number of active disparity on the first level
virtual int getNrPlane() const = 0;
virtual void setNrPlane(int nr_plane) = 0;
float max_data_term;
float data_weight;
float max_disc_term;
float disc_single_jump;
virtual bool getUseLocalInitDataCost() const = 0;
virtual void setUseLocalInitDataCost(bool use_local_init_data_cost) = 0;
int min_disp_th;
int msg_type;
bool use_local_init_data_cost;
...
static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane);
};
The class implements algorithm described in [Yang2010]_. ``StereoConstantSpaceBP`` supports both local minimum and global minimum data cost initialization algorithms. For more details, see the paper mentioned above. By default, a local algorithm is used. To enable a global algorithm, set ``use_local_init_data_cost`` to ``false`` .
gpu::StereoConstantSpaceBP::StereoConstantSpaceBP
-----------------------------------------------------
Enables the :ocv:class:`gpu::StereoConstantSpaceBP` constructors.
.. ocv:function:: gpu::StereoConstantSpaceBP::StereoConstantSpaceBP(int ndisp = DEFAULT_NDISP, int iters = DEFAULT_ITERS, int levels = DEFAULT_LEVELS, int nr_plane = DEFAULT_NR_PLANE, int msg_type = CV_32F)
.. ocv:function:: gpu::StereoConstantSpaceBP::StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane, float max_data_term, float data_weight, float max_disc_term, float disc_single_jump, int min_disp_th = 0, int msg_type = CV_32F)
:param ndisp: Number of disparities.
:param iters: Number of BP iterations on each level.
:param levels: Number of levels.
:param nr_plane: Number of disparity levels on the first level.
:param max_data_term: Truncation of data cost.
:param data_weight: Data weight.
:param max_disc_term: Truncation of discontinuity.
:param disc_single_jump: Discontinuity single jump.
:param min_disp_th: Minimal disparity threshold.
:param msg_type: Type for messages. ``CV_16SC1`` and ``CV_32FC1`` types are supported.
``StereoConstantSpaceBP`` uses a truncated linear model for the data cost and discontinuity terms:
.. math::
@ -331,54 +194,65 @@ By default, ``StereoConstantSpaceBP`` uses floating-point arithmetics and the ``
gpu::StereoConstantSpaceBP::estimateRecommendedParams
---------------------------------------------------------
Uses a heuristic method to compute parameters (ndisp, iters, levelsand nrplane) for the specified image size (widthand height).
gpu::createStereoConstantSpaceBP
--------------------------------
Creates StereoConstantSpaceBP object.
.. ocv:function:: void gpu::StereoConstantSpaceBP::estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane)
.. ocv:function:: Ptr<gpu::StereoConstantSpaceBP> gpu::createStereoConstantSpaceBP(int ndisp = 128, int iters = 8, int levels = 4, int nr_plane = 4, int msg_type = CV_32F)
:param ndisp: Number of disparities.
:param iters: Number of BP iterations on each level.
:param levels: Number of levels.
gpu::StereoConstantSpaceBP::operator ()
-------------------------------------------
Enables the stereo correspondence operator that finds the disparity for the specified rectified stereo pair.
:param nr_plane: Number of disparity levels on the first level.
.. ocv:function:: void gpu::StereoConstantSpaceBP::operator ()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null())
:param msg_type: Type for messages. ``CV_16SC1`` and ``CV_32FC1`` types are supported.
:param left: Left image. ``CV_8UC1`` , ``CV_8UC3`` and ``CV_8UC4`` types are supported.
:param right: Right image with the same size and the same type as the left one.
:param disparity: Output disparity map. If ``disparity`` is empty, the output type is ``CV_16SC1`` . Otherwise, the output type is ``disparity.type()`` .
gpu::StereoConstantSpaceBP::estimateRecommendedParams
-----------------------------------------------------
Uses a heuristic method to compute parameters (ndisp, iters, levelsand nrplane) for the specified image size (widthand height).
:param stream: Stream for the asynchronous version.
.. ocv:function:: void gpu::StereoConstantSpaceBP::estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane)
gpu::DisparityBilateralFilter
-----------------------------
.. ocv:class:: gpu::DisparityBilateralFilter
.. ocv:class:: gpu::DisparityBilateralFilter : public cv::Algorithm
Class refining a disparity map using joint bilateral filtering. ::
class CV_EXPORTS DisparityBilateralFilter
class CV_EXPORTS DisparityBilateralFilter : public cv::Algorithm
{
public:
enum { DEFAULT_NDISP = 64 };
enum { DEFAULT_RADIUS = 3 };
enum { DEFAULT_ITERS = 1 };
//! the disparity map refinement operator. Refine disparity map using joint bilateral filtering given a single color image.
//! disparity must have CV_8U or CV_16S type, image must have CV_8UC1 or CV_8UC3 type.
virtual void apply(InputArray disparity, InputArray image, OutputArray dst, Stream& stream = Stream::Null()) = 0;
virtual int getNumDisparities() const = 0;
virtual void setNumDisparities(int numDisparities) = 0;
virtual int getRadius() const = 0;
virtual void setRadius(int radius) = 0;
explicit DisparityBilateralFilter(int ndisp = DEFAULT_NDISP,
int radius = DEFAULT_RADIUS, int iters = DEFAULT_ITERS);
virtual int getNumIters() const = 0;
virtual void setNumIters(int iters) = 0;
DisparityBilateralFilter(int ndisp, int radius, int iters,
float edge_threshold, float max_disc_threshold,
float sigma_range);
//! truncation of data continuity
virtual double getEdgeThreshold() const = 0;
virtual void setEdgeThreshold(double edge_threshold) = 0;
void operator()(const GpuMat& disparity, const GpuMat& image,
GpuMat& dst, Stream& stream = Stream::Null());
//! truncation of disparity continuity
virtual double getMaxDiscThreshold() const = 0;
virtual void setMaxDiscThreshold(double max_disc_threshold) = 0;
...
//! filter range sigma
virtual double getSigmaRange() const = 0;
virtual void setSigmaRange(double sigma_range) = 0;
};
@ -386,13 +260,11 @@ The class implements [Yang2010]_ algorithm.
gpu::DisparityBilateralFilter::DisparityBilateralFilter
-----------------------------------------------------------
Enables the :ocv:class:`gpu::DisparityBilateralFilter` constructors.
.. ocv:function:: gpu::DisparityBilateralFilter::DisparityBilateralFilter(int ndisp = DEFAULT_NDISP, int radius = DEFAULT_RADIUS, int iters = DEFAULT_ITERS)
gpu::createDisparityBilateralFilter
-----------------------------------
Creates DisparityBilateralFilter object.
.. ocv:function:: gpu::DisparityBilateralFilter::DisparityBilateralFilter(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold, float sigma_range)
.. ocv:function:: Ptr<gpu::DisparityBilateralFilter> gpu::createDisparityBilateralFilter(int ndisp = 64, int radius = 3, int iters = 1)
:param ndisp: Number of disparities.
@ -400,19 +272,13 @@ Enables the :ocv:class:`gpu::DisparityBilateralFilter` constructors.
:param iters: Number of iterations.
:param edge_threshold: Threshold for edges.
:param max_disc_threshold: Constant to reject outliers.
:param sigma_range: Filter range.
gpu::DisparityBilateralFilter::operator ()
----------------------------------------------
gpu::DisparityBilateralFilter::apply
------------------------------------
Refines a disparity map using joint bilateral filtering.
.. ocv:function:: void gpu::DisparityBilateralFilter::operator ()(const GpuMat& disparity, const GpuMat& image, GpuMat& dst, Stream& stream = Stream::Null())
.. ocv:function:: void gpu::DisparityBilateralFilter::apply(InputArray disparity, InputArray image, OutputArray dst, Stream& stream = Stream::Null())
:param disparity: Input disparity map. ``CV_8UC1`` and ``CV_16SC1`` types are supported.
@ -424,44 +290,43 @@ Refines a disparity map using joint bilateral filtering.
gpu::drawColorDisp
----------------------
Colors a disparity image.
gpu::reprojectImageTo3D
-----------------------
Reprojects a disparity image to 3D space.
.. ocv:function:: void gpu::drawColorDisp(const GpuMat& src_disp, GpuMat& dst_disp, int ndisp, Stream& stream = Stream::Null())
.. ocv:function:: void gpu::reprojectImageTo3D(InputArray disp, OutputArray xyzw, InputArray Q, int dst_cn = 4, Stream& stream = Stream::Null())
:param src_disp: Source disparity image. ``CV_8UC1`` and ``CV_16SC1`` types are supported.
:param disp: Input disparity image. ``CV_8U`` and ``CV_16S`` types are supported.
:param dst_disp: Output disparity image. It has the same size as ``src_disp`` . The type is ``CV_8UC4`` in ``BGRA`` format (alpha = 255).
:param xyzw: Output 3- or 4-channel floating-point image of the same size as ``disp`` . Each element of ``xyzw(x,y)`` contains 3D coordinates ``(x,y,z)`` or ``(x,y,z,1)`` of the point ``(x,y)`` , computed from the disparity map.
:param ndisp: Number of disparities.
:param Q: :math:`4 \times 4` perspective transformation matrix that can be obtained via :ocv:func:`stereoRectify` .
:param stream: Stream for the asynchronous version.
:param dst_cn: The number of channels for output image. Can be 3 or 4.
This function draws a colored disparity map by converting disparity values from ``[0..ndisp)`` interval first to ``HSV`` color space (where different disparity values correspond to different hues) and then converting the pixels to ``RGB`` for visualization.
:param stream: Stream for the asynchronous version.
.. seealso:: :ocv:func:`reprojectImageTo3D`
gpu::reprojectImageTo3D
---------------------------
Reprojects a disparity image to 3D space.
.. ocv:function:: void gpu::reprojectImageTo3D(const GpuMat& disp, GpuMat& xyzw, const Mat& Q, int dst_cn = 4, Stream& stream = Stream::Null())
gpu::drawColorDisp
------------------
Colors a disparity image.
:param disp: Input disparity image. ``CV_8U`` and ``CV_16S`` types are supported.
.. ocv:function:: void gpu::drawColorDisp(InputArray src_disp, OutputArray dst_disp, int ndisp, Stream& stream = Stream::Null())
:param xyzw: Output 3- or 4-channel floating-point image of the same size as ``disp`` . Each element of ``xyzw(x,y)`` contains 3D coordinates ``(x,y,z)`` or ``(x,y,z,1)`` of the point ``(x,y)`` , computed from the disparity map.
:param src_disp: Source disparity image. ``CV_8UC1`` and ``CV_16SC1`` types are supported.
:param Q: :math:`4 \times 4` perspective transformation matrix that can be obtained via :ocv:func:`stereoRectify` .
:param dst_disp: Output disparity image. It has the same size as ``src_disp`` . The type is ``CV_8UC4`` in ``BGRA`` format (alpha = 255).
:param dst_cn: The number of channels for output image. Can be 3 or 4.
:param ndisp: Number of disparities.
:param stream: Stream for the asynchronous version.
.. seealso:: :ocv:func:`reprojectImageTo3D`
This function draws a colored disparity map by converting disparity values from ``[0..ndisp)`` interval first to ``HSV`` color space (where different disparity values correspond to different hues) and then converting the pixels to ``RGB`` for visualization.
.. [Felzenszwalb2006] Pedro F. Felzenszwalb algorithm [Pedro F. Felzenszwalb and Daniel P. Huttenlocher. *Efficient belief propagation for early vision*. International Journal of Computer Vision, 70(1), October 2006
.. [Yang2010] Q. Yang, L. Wang, and N. Ahuja. *A constant-space belief propagation algorithm for stereo matching*. In CVPR, 2010.

@ -48,199 +48,145 @@
#endif
#include "opencv2/core/gpu.hpp"
#include "opencv2/calib3d.hpp"
namespace cv { namespace gpu {
class CV_EXPORTS StereoBM_GPU
/////////////////////////////////////////
// StereoBM
class CV_EXPORTS StereoBM : public cv::StereoBM
{
public:
enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
//! the default constructor
StereoBM_GPU();
//! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
StereoBM_GPU(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
//! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
//! Output disparity has CV_8U type.
void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null());
using cv::StereoBM::compute;
//! Some heuristics that tries to estmate
// if current GPU will be faster than CPU in this algorithm.
// It queries current active device.
static bool checkIfGpuCallReasonable();
int preset;
int ndisp;
int winSize;
virtual void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream) = 0;
};
// If avergeTexThreshold == 0 => post procesing is disabled
// If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
// SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
// i.e. input left image is low textured.
float avergeTexThreshold;
CV_EXPORTS Ptr<gpu::StereoBM> createStereoBM(int numDisparities = 64, int blockSize = 19);
private:
GpuMat minSSD, leBuf, riBuf;
};
/////////////////////////////////////////
// StereoBeliefPropagation
// "Efficient Belief Propagation for Early Vision"
// P.Felzenszwalb
class CV_EXPORTS StereoBeliefPropagation
//! "Efficient Belief Propagation for Early Vision" P.Felzenszwalb
class CV_EXPORTS StereoBeliefPropagation : public cv::StereoMatcher
{
public:
enum { DEFAULT_NDISP = 64 };
enum { DEFAULT_ITERS = 5 };
enum { DEFAULT_LEVELS = 5 };
static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels);
//! the default constructor
explicit StereoBeliefPropagation(int ndisp = DEFAULT_NDISP,
int iters = DEFAULT_ITERS,
int levels = DEFAULT_LEVELS,
int msg_type = CV_32F);
//! the full constructor taking the number of disparities, number of BP iterations on each level,
//! number of levels, truncation of data cost, data weight,
//! truncation of discontinuity cost and discontinuity single jump
//! DataTerm = data_weight * min(fabs(I2-I1), max_data_term)
//! DiscTerm = min(disc_single_jump * fabs(f1-f2), max_disc_term)
//! please see paper for more details
StereoBeliefPropagation(int ndisp, int iters, int levels,
float max_data_term, float data_weight,
float max_disc_term, float disc_single_jump,
int msg_type = CV_32F);
//! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair,
//! if disparity is empty output type will be CV_16S else output type will be disparity.type().
void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null());
using cv::StereoMatcher::compute;
virtual void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream) = 0;
//! version for user specified data term
void operator()(const GpuMat& data, GpuMat& disparity, Stream& stream = Stream::Null());
virtual void compute(InputArray data, OutputArray disparity, Stream& stream = Stream::Null()) = 0;
int ndisp;
//! number of BP iterations on each level
virtual int getNumIters() const = 0;
virtual void setNumIters(int iters) = 0;
int iters;
int levels;
//! number of levels
virtual int getNumLevels() const = 0;
virtual void setNumLevels(int levels) = 0;
float max_data_term;
float data_weight;
float max_disc_term;
float disc_single_jump;
//! truncation of data cost
virtual double getMaxDataTerm() const = 0;
virtual void setMaxDataTerm(double max_data_term) = 0;
int msg_type;
private:
GpuMat u, d, l, r, u2, d2, l2, r2;
std::vector<GpuMat> datas;
GpuMat out;
};
//! data weight
virtual double getDataWeight() const = 0;
virtual void setDataWeight(double data_weight) = 0;
// "A Constant-Space Belief Propagation Algorithm for Stereo Matching"
// Qingxiong Yang, Liang Wang, Narendra Ahuja
// http://vision.ai.uiuc.edu/~qyang6/
class CV_EXPORTS StereoConstantSpaceBP
{
public:
enum { DEFAULT_NDISP = 128 };
enum { DEFAULT_ITERS = 8 };
enum { DEFAULT_LEVELS = 4 };
enum { DEFAULT_NR_PLANE = 4 };
//! truncation of discontinuity cost
virtual double getMaxDiscTerm() const = 0;
virtual void setMaxDiscTerm(double max_disc_term) = 0;
static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane);
//! discontinuity single jump
virtual double getDiscSingleJump() const = 0;
virtual void setDiscSingleJump(double disc_single_jump) = 0;
//! the default constructor
explicit StereoConstantSpaceBP(int ndisp = DEFAULT_NDISP,
int iters = DEFAULT_ITERS,
int levels = DEFAULT_LEVELS,
int nr_plane = DEFAULT_NR_PLANE,
int msg_type = CV_32F);
//! type for messages (CV_16SC1 or CV_32FC1)
virtual int getMsgType() const = 0;
virtual void setMsgType(int msg_type) = 0;
//! the full constructor taking the number of disparities, number of BP iterations on each level,
//! number of levels, number of active disparity on the first level, truncation of data cost, data weight,
//! truncation of discontinuity cost, discontinuity single jump and minimum disparity threshold
StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
int min_disp_th = 0,
int msg_type = CV_32F);
//! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair,
//! if disparity is empty output type will be CV_16S else output type will be disparity.type().
void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null());
static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels);
};
int ndisp;
CV_EXPORTS Ptr<gpu::StereoBeliefPropagation>
createStereoBeliefPropagation(int ndisp = 64, int iters = 5, int levels = 5, int msg_type = CV_32F);
int iters;
int levels;
/////////////////////////////////////////
// StereoConstantSpaceBP
int nr_plane;
//! "A Constant-Space Belief Propagation Algorithm for Stereo Matching"
//! Qingxiong Yang, Liang Wang, Narendra Ahuja
//! http://vision.ai.uiuc.edu/~qyang6/
class CV_EXPORTS StereoConstantSpaceBP : public gpu::StereoBeliefPropagation
{
public:
//! number of active disparity on the first level
virtual int getNrPlane() const = 0;
virtual void setNrPlane(int nr_plane) = 0;
float max_data_term;
float data_weight;
float max_disc_term;
float disc_single_jump;
virtual bool getUseLocalInitDataCost() const = 0;
virtual void setUseLocalInitDataCost(bool use_local_init_data_cost) = 0;
int min_disp_th;
static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane);
};
int msg_type;
CV_EXPORTS Ptr<gpu::StereoConstantSpaceBP>
createStereoConstantSpaceBP(int ndisp = 128, int iters = 8, int levels = 4, int nr_plane = 4, int msg_type = CV_32F);
bool use_local_init_data_cost;
private:
GpuMat messages_buffers;
/////////////////////////////////////////
// DisparityBilateralFilter
GpuMat temp;
GpuMat out;
};
// Disparity map refinement using joint bilateral filtering given a single color image.
// Qingxiong Yang, Liang Wang, Narendra Ahuja
// http://vision.ai.uiuc.edu/~qyang6/
class CV_EXPORTS DisparityBilateralFilter
//! Disparity map refinement using joint bilateral filtering given a single color image.
//! Qingxiong Yang, Liang Wang, Narendra Ahuja
//! http://vision.ai.uiuc.edu/~qyang6/
class CV_EXPORTS DisparityBilateralFilter : public cv::Algorithm
{
public:
enum { DEFAULT_NDISP = 64 };
enum { DEFAULT_RADIUS = 3 };
enum { DEFAULT_ITERS = 1 };
//! the disparity map refinement operator. Refine disparity map using joint bilateral filtering given a single color image.
//! disparity must have CV_8U or CV_16S type, image must have CV_8UC1 or CV_8UC3 type.
virtual void apply(InputArray disparity, InputArray image, OutputArray dst, Stream& stream = Stream::Null()) = 0;
//! the default constructor
explicit DisparityBilateralFilter(int ndisp = DEFAULT_NDISP, int radius = DEFAULT_RADIUS, int iters = DEFAULT_ITERS);
virtual int getNumDisparities() const = 0;
virtual void setNumDisparities(int numDisparities) = 0;
//! the full constructor taking the number of disparities, filter radius,
//! number of iterations, truncation of data continuity, truncation of disparity continuity
//! and filter range sigma
DisparityBilateralFilter(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold, float sigma_range);
virtual int getRadius() const = 0;
virtual void setRadius(int radius) = 0;
//! the disparity map refinement operator. Refine disparity map using joint bilateral filtering given a single color image.
//! disparity must have CV_8U or CV_16S type, image must have CV_8UC1 or CV_8UC3 type.
void operator()(const GpuMat& disparity, const GpuMat& image, GpuMat& dst, Stream& stream = Stream::Null());
virtual int getNumIters() const = 0;
virtual void setNumIters(int iters) = 0;
private:
int ndisp;
int radius;
int iters;
//! truncation of data continuity
virtual double getEdgeThreshold() const = 0;
virtual void setEdgeThreshold(double edge_threshold) = 0;
float edge_threshold;
float max_disc_threshold;
float sigma_range;
//! truncation of disparity continuity
virtual double getMaxDiscThreshold() const = 0;
virtual void setMaxDiscThreshold(double max_disc_threshold) = 0;
GpuMat table_color;
GpuMat table_space;
//! filter range sigma
virtual double getSigmaRange() const = 0;
virtual void setSigmaRange(double sigma_range) = 0;
};
CV_EXPORTS Ptr<gpu::DisparityBilateralFilter>
createDisparityBilateralFilter(int ndisp = 64, int radius = 3, int iters = 1);
/////////////////////////////////////////
// Utility
//! Reprojects disparity image to 3D space.
//! Supports CV_8U and CV_16S types of input disparity.
//! The output is a 3- or 4-channel floating-point matrix.
//! Each element of this matrix will contain the 3D coordinates of the point (x,y,z,1), computed from the disparity map.
//! Q is the 4x4 perspective transformation matrix that can be obtained with cvStereoRectify.
CV_EXPORTS void reprojectImageTo3D(const GpuMat& disp, GpuMat& xyzw, const Mat& Q, int dst_cn = 4, Stream& stream = Stream::Null());
CV_EXPORTS void reprojectImageTo3D(InputArray disp, OutputArray xyzw, InputArray Q, int dst_cn = 4, Stream& stream = Stream::Null());
//! Does coloring of disparity image: [0..ndisp) -> [0..240, 1, 1] in HSV.
//! Supported types of input disparity: CV_8U, CV_16S.
//! Output disparity has CV_8UC4 type in BGRA format (alpha = 255).
CV_EXPORTS void drawColorDisp(const GpuMat& src_disp, GpuMat& dst_disp, int ndisp, Stream& stream = Stream::Null());
CV_EXPORTS void drawColorDisp(InputArray src_disp, OutputArray dst_disp, int ndisp, Stream& stream = Stream::Null());
}} // namespace cv { namespace gpu {

@ -63,18 +63,17 @@ PERF_TEST_P(ImagePair, StereoBM,
const cv::Mat imgRight = readImage(GET_PARAM(1), cv::IMREAD_GRAYSCALE);
ASSERT_FALSE(imgRight.empty());
const int preset = 0;
const int ndisp = 256;
if (PERF_RUN_GPU())
{
cv::gpu::StereoBM_GPU d_bm(preset, ndisp);
cv::Ptr<cv::StereoBM> d_bm = cv::gpu::createStereoBM(ndisp);
const cv::gpu::GpuMat d_imgLeft(imgLeft);
const cv::gpu::GpuMat d_imgRight(imgRight);
cv::gpu::GpuMat dst;
TEST_CYCLE() d_bm(d_imgLeft, d_imgRight, dst);
TEST_CYCLE() d_bm->compute(d_imgLeft, d_imgRight, dst);
GPU_SANITY_CHECK(dst);
}
@ -108,13 +107,13 @@ PERF_TEST_P(ImagePair, StereoBeliefPropagation,
if (PERF_RUN_GPU())
{
cv::gpu::StereoBeliefPropagation d_bp(ndisp);
cv::Ptr<cv::gpu::StereoBeliefPropagation> d_bp = cv::gpu::createStereoBeliefPropagation(ndisp);
const cv::gpu::GpuMat d_imgLeft(imgLeft);
const cv::gpu::GpuMat d_imgRight(imgRight);
cv::gpu::GpuMat dst;
TEST_CYCLE() d_bp(d_imgLeft, d_imgRight, dst);
TEST_CYCLE() d_bp->compute(d_imgLeft, d_imgRight, dst);
GPU_SANITY_CHECK(dst);
}
@ -142,13 +141,13 @@ PERF_TEST_P(ImagePair, StereoConstantSpaceBP,
if (PERF_RUN_GPU())
{
cv::gpu::StereoConstantSpaceBP d_csbp(ndisp);
cv::Ptr<cv::gpu::StereoConstantSpaceBP> d_csbp = cv::gpu::createStereoConstantSpaceBP(ndisp);
const cv::gpu::GpuMat d_imgLeft(imgLeft);
const cv::gpu::GpuMat d_imgRight(imgRight);
cv::gpu::GpuMat dst;
TEST_CYCLE() d_csbp(d_imgLeft, d_imgRight, dst);
TEST_CYCLE() d_csbp->compute(d_imgLeft, d_imgRight, dst);
GPU_SANITY_CHECK(dst);
}
@ -174,13 +173,13 @@ PERF_TEST_P(ImagePair, DisparityBilateralFilter,
if (PERF_RUN_GPU())
{
cv::gpu::DisparityBilateralFilter d_filter(ndisp);
cv::Ptr<cv::gpu::DisparityBilateralFilter> d_filter = cv::gpu::createDisparityBilateralFilter(ndisp);
const cv::gpu::GpuMat d_img(img);
const cv::gpu::GpuMat d_disp(disp);
cv::gpu::GpuMat dst;
TEST_CYCLE() d_filter(d_disp, d_img, dst);
TEST_CYCLE() d_filter->apply(d_disp, d_img, dst);
GPU_SANITY_CHECK(dst);
}

@ -47,10 +47,7 @@ using namespace cv::gpu;
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int, int, int) { throw_no_cuda(); }
cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int, int, int, float, float, float) { throw_no_cuda(); }
void cv::gpu::DisparityBilateralFilter::operator()(const GpuMat&, const GpuMat&, GpuMat&, Stream&) { throw_no_cuda(); }
Ptr<gpu::DisparityBilateralFilter> cv::gpu::createDisparityBilateralFilter(int, int, int) { throw_no_cuda(); return Ptr<gpu::DisparityBilateralFilter>(); }
#else /* !defined (HAVE_CUDA) */
@ -65,15 +62,46 @@ namespace cv { namespace gpu { namespace cudev
}
}}}
using namespace ::cv::gpu::cudev::disp_bilateral_filter;
namespace
{
const float DEFAULT_EDGE_THRESHOLD = 0.1f;
const float DEFAULT_MAX_DISC_THRESHOLD = 0.2f;
const float DEFAULT_SIGMA_RANGE = 10.0f;
class DispBilateralFilterImpl : public gpu::DisparityBilateralFilter
{
public:
DispBilateralFilterImpl(int ndisp, int radius, int iters);
void apply(InputArray disparity, InputArray image, OutputArray dst, Stream& stream);
int getNumDisparities() const { return ndisp_; }
void setNumDisparities(int numDisparities) { ndisp_ = numDisparities; }
int getRadius() const { return radius_; }
void setRadius(int radius);
int getNumIters() const { return iters_; }
void setNumIters(int iters) { iters_ = iters; }
inline void calc_color_weighted_table(GpuMat& table_color, float sigma_range, int len)
double getEdgeThreshold() const { return edge_threshold_; }
void setEdgeThreshold(double edge_threshold) { edge_threshold_ = (float) edge_threshold; }
double getMaxDiscThreshold() const { return max_disc_threshold_; }
void setMaxDiscThreshold(double max_disc_threshold) { max_disc_threshold_ = (float) max_disc_threshold; }
double getSigmaRange() const { return sigma_range_; }
void setSigmaRange(double sigma_range);
private:
int ndisp_;
int radius_;
int iters_;
float edge_threshold_;
float max_disc_threshold_;
float sigma_range_;
GpuMat table_color_;
GpuMat table_space_;
};
void calc_color_weighted_table(GpuMat& table_color, float sigma_range, int len)
{
Mat cpu_table_color(1, len, CV_32F);
@ -85,7 +113,7 @@ namespace
table_color.upload(cpu_table_color);
}
inline void calc_space_weighted_filter(GpuMat& table_space, int win_size, float dist_space)
void calc_space_weighted_filter(GpuMat& table_space, int win_size, float dist_space)
{
int half = (win_size >> 1);
@ -101,54 +129,78 @@ namespace
table_space.upload(cpu_table_space);
}
const float DEFAULT_EDGE_THRESHOLD = 0.1f;
const float DEFAULT_MAX_DISC_THRESHOLD = 0.2f;
const float DEFAULT_SIGMA_RANGE = 10.0f;
DispBilateralFilterImpl::DispBilateralFilterImpl(int ndisp, int radius, int iters) :
ndisp_(ndisp), radius_(radius), iters_(iters),
edge_threshold_(DEFAULT_EDGE_THRESHOLD), max_disc_threshold_(DEFAULT_MAX_DISC_THRESHOLD),
sigma_range_(DEFAULT_SIGMA_RANGE)
{
calc_color_weighted_table(table_color_, sigma_range_, 255);
calc_space_weighted_filter(table_space_, radius_ * 2 + 1, radius_ + 1.0f);
}
void DispBilateralFilterImpl::setRadius(int radius)
{
radius_ = radius;
calc_space_weighted_filter(table_space_, radius_ * 2 + 1, radius_ + 1.0f);
}
void DispBilateralFilterImpl::setSigmaRange(double sigma_range)
{
sigma_range_ = (float) sigma_range;
calc_color_weighted_table(table_color_, sigma_range_, 255);
}
template <typename T>
void disp_bilateral_filter_operator(int ndisp, int radius, int iters, float edge_threshold,float max_disc_threshold,
GpuMat& table_color, GpuMat& table_space,
const GpuMat& disp, const GpuMat& img, GpuMat& dst, Stream& stream)
void disp_bilateral_filter_operator(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold,
GpuMat& table_color, GpuMat& table_space,
const GpuMat& disp, const GpuMat& img,
OutputArray _dst, Stream& stream)
{
short edge_disc = std::max<short>(short(1), short(ndisp * edge_threshold + 0.5));
short max_disc = short(ndisp * max_disc_threshold + 0.5);
using namespace cv::gpu::cudev::disp_bilateral_filter;
const short edge_disc = std::max<short>(short(1), short(ndisp * edge_threshold + 0.5));
const short max_disc = short(ndisp * max_disc_threshold + 0.5);
disp_load_constants(table_color.ptr<float>(), table_space, ndisp, radius, edge_disc, max_disc);
if (&dst != &disp)
{
_dst.create(disp.size(), disp.type());
GpuMat dst = _dst.getGpuMat();
if (dst.data != disp.data)
disp.copyTo(dst, stream);
}
disp_bilateral_filter<T>(dst, img, img.channels(), iters, StreamAccessor::getStream(stream));
}
typedef void (*bilateral_filter_operator_t)(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold,
GpuMat& table_color, GpuMat& table_space,
const GpuMat& disp, const GpuMat& img, GpuMat& dst, Stream& stream);
void DispBilateralFilterImpl::apply(InputArray _disp, InputArray _image, OutputArray dst, Stream& stream)
{
typedef void (*bilateral_filter_operator_t)(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold,
GpuMat& table_color, GpuMat& table_space,
const GpuMat& disp, const GpuMat& img, OutputArray dst, Stream& stream);
const bilateral_filter_operator_t operators[] =
{disp_bilateral_filter_operator<unsigned char>, 0, 0, disp_bilateral_filter_operator<short>, 0, 0, 0, 0};
const bilateral_filter_operator_t operators[] =
{disp_bilateral_filter_operator<unsigned char>, 0, 0, disp_bilateral_filter_operator<short>, 0, 0, 0, 0};
}
CV_Assert( 0 < ndisp_ && 0 < radius_ && 0 < iters_ );
cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int ndisp_, int radius_, int iters_)
: ndisp(ndisp_), radius(radius_), iters(iters_), edge_threshold(DEFAULT_EDGE_THRESHOLD), max_disc_threshold(DEFAULT_MAX_DISC_THRESHOLD),
sigma_range(DEFAULT_SIGMA_RANGE)
{
calc_color_weighted_table(table_color, sigma_range, 255);
calc_space_weighted_filter(table_space, radius * 2 + 1, radius + 1.0f);
}
GpuMat disp = _disp.getGpuMat();
GpuMat img = _image.getGpuMat();
cv::gpu::DisparityBilateralFilter::DisparityBilateralFilter(int ndisp_, int radius_, int iters_, float edge_threshold_,
float max_disc_threshold_, float sigma_range_)
: ndisp(ndisp_), radius(radius_), iters(iters_), edge_threshold(edge_threshold_), max_disc_threshold(max_disc_threshold_),
sigma_range(sigma_range_)
{
calc_color_weighted_table(table_color, sigma_range, 255);
calc_space_weighted_filter(table_space, radius * 2 + 1, radius + 1.0f);
CV_Assert( disp.type() == CV_8U || disp.type() == CV_16S );
CV_Assert( img.type() == CV_8UC1 || img.type() == CV_8UC3 );
CV_Assert( disp.size() == img.size() );
operators[disp.type()](ndisp_, radius_, iters_, edge_threshold_, max_disc_threshold_,
table_color_, table_space_, disp, img, dst, stream);
}
}
void cv::gpu::DisparityBilateralFilter::operator()(const GpuMat& disp, const GpuMat& img, GpuMat& dst, Stream& stream)
Ptr<gpu::DisparityBilateralFilter> cv::gpu::createDisparityBilateralFilter(int ndisp, int radius, int iters)
{
CV_DbgAssert(0 < ndisp && 0 < radius && 0 < iters);
CV_Assert(disp.rows == img.rows && disp.cols == img.cols && (disp.type() == CV_8U || disp.type() == CV_16S) && (img.type() == CV_8UC1 || img.type() == CV_8UC3));
operators[disp.type()](ndisp, radius, iters, edge_threshold, max_disc_threshold, table_color, table_space, disp, img, dst, stream);
return new DispBilateralFilterImpl(ndisp, radius, iters);
}
#endif /* !defined (HAVE_CUDA) */

@ -48,5 +48,6 @@
#include "opencv2/gpustereo.hpp"
#include "opencv2/core/private.gpu.hpp"
#include "opencv2/core/utility.hpp"
#endif /* __OPENCV_PRECOMP_H__ */

@ -47,11 +47,7 @@ using namespace cv::gpu;
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
cv::gpu::StereoBM_GPU::StereoBM_GPU() { throw_no_cuda(); }
cv::gpu::StereoBM_GPU::StereoBM_GPU(int, int, int) { throw_no_cuda(); }
bool cv::gpu::StereoBM_GPU::checkIfGpuCallReasonable() { throw_no_cuda(); return false; }
void cv::gpu::StereoBM_GPU::operator() ( const GpuMat&, const GpuMat&, GpuMat&, Stream&) { throw_no_cuda(); }
Ptr<gpu::StereoBM> cv::gpu::createStereoBM(int, int) { throw_no_cuda(); return Ptr<gpu::StereoBM>(); }
#else /* !defined (HAVE_CUDA) */
@ -67,74 +63,123 @@ namespace cv { namespace gpu { namespace cudev
namespace
{
const float defaultAvgTexThreshold = 3;
}
class StereoBMImpl : public gpu::StereoBM
{
public:
StereoBMImpl(int numDisparities, int blockSize);
cv::gpu::StereoBM_GPU::StereoBM_GPU()
: preset(BASIC_PRESET), ndisp(DEFAULT_NDISP), winSize(DEFAULT_WINSZ), avergeTexThreshold(defaultAvgTexThreshold)
{
}
void compute(InputArray left, InputArray right, OutputArray disparity);
void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream);
cv::gpu::StereoBM_GPU::StereoBM_GPU(int preset_, int ndisparities_, int winSize_)
: preset(preset_), ndisp(ndisparities_), winSize(winSize_), avergeTexThreshold(defaultAvgTexThreshold)
{
const int max_supported_ndisp = 1 << (sizeof(unsigned char) * 8);
CV_Assert(0 < ndisp && ndisp <= max_supported_ndisp);
CV_Assert(ndisp % 8 == 0);
CV_Assert(winSize % 2 == 1);
}
int getMinDisparity() const { return 0; }
void setMinDisparity(int /*minDisparity*/) {}
bool cv::gpu::StereoBM_GPU::checkIfGpuCallReasonable()
{
if (0 == getCudaEnabledDeviceCount())
return false;
int getNumDisparities() const { return ndisp_; }
void setNumDisparities(int numDisparities) { ndisp_ = numDisparities; }
DeviceInfo device_info;
int getBlockSize() const { return winSize_; }
void setBlockSize(int blockSize) { winSize_ = blockSize; }
if (device_info.major() > 1 || device_info.multiProcessorCount() > 16)
return true;
int getSpeckleWindowSize() const { return 0; }
void setSpeckleWindowSize(int /*speckleWindowSize*/) {}
return false;
}
int getSpeckleRange() const { return 0; }
void setSpeckleRange(int /*speckleRange*/) {}
namespace
{
void stereo_bm_gpu_operator( GpuMat& minSSD, GpuMat& leBuf, GpuMat& riBuf, int preset, int ndisp, int winSize, float avergeTexThreshold, const GpuMat& left, const GpuMat& right, GpuMat& disparity, cudaStream_t stream)
int getDisp12MaxDiff() const { return 0; }
void setDisp12MaxDiff(int /*disp12MaxDiff*/) {}
int getPreFilterType() const { return preset_; }
void setPreFilterType(int preFilterType) { preset_ = preFilterType; }
int getPreFilterSize() const { return 0; }
void setPreFilterSize(int /*preFilterSize*/) {}
int getPreFilterCap() const { return preFilterCap_; }
void setPreFilterCap(int preFilterCap) { preFilterCap_ = preFilterCap; }
int getTextureThreshold() const { return avergeTexThreshold_; }
void setTextureThreshold(int textureThreshold) { avergeTexThreshold_ = textureThreshold; }
int getUniquenessRatio() const { return 0; }
void setUniquenessRatio(int /*uniquenessRatio*/) {}
int getSmallerBlockSize() const { return 0; }
void setSmallerBlockSize(int /*blockSize*/){}
Rect getROI1() const { return Rect(); }
void setROI1(Rect /*roi1*/) {}
Rect getROI2() const { return Rect(); }
void setROI2(Rect /*roi2*/) {}
private:
int preset_;
int ndisp_;
int winSize_;
int preFilterCap_;
float avergeTexThreshold_;
GpuMat minSSD_, leBuf_, riBuf_;
};
StereoBMImpl::StereoBMImpl(int numDisparities, int blockSize)
: preset_(0), ndisp_(numDisparities), winSize_(blockSize), preFilterCap_(31), avergeTexThreshold_(3)
{
}
void StereoBMImpl::compute(InputArray left, InputArray right, OutputArray disparity)
{
compute(left, right, disparity, Stream::Null());
}
void StereoBMImpl::compute(InputArray _left, InputArray _right, OutputArray _disparity, Stream& _stream)
{
using namespace ::cv::gpu::cudev::stereobm;
CV_Assert(left.rows == right.rows && left.cols == right.cols);
CV_Assert(left.type() == CV_8UC1);
CV_Assert(right.type() == CV_8UC1);
const int max_supported_ndisp = 1 << (sizeof(unsigned char) * 8);
CV_Assert( 0 < ndisp_ && ndisp_ <= max_supported_ndisp );
CV_Assert( ndisp_ % 8 == 0 );
CV_Assert( winSize_ % 2 == 1 );
GpuMat left = _left.getGpuMat();
GpuMat right = _right.getGpuMat();
CV_Assert( left.type() == CV_8UC1 );
CV_Assert( left.size() == right.size() && left.type() == right.type() );
_disparity.create(left.size(), CV_8UC1);
GpuMat disparity = _disparity.getGpuMat();
cudaStream_t stream = StreamAccessor::getStream(_stream);
disparity.create(left.size(), CV_8U);
minSSD.create(left.size(), CV_32S);
gpu::ensureSizeIsEnough(left.size(), CV_32SC1, minSSD_);
GpuMat le_for_bm = left;
GpuMat ri_for_bm = right;
PtrStepSzb le_for_bm = left;
PtrStepSzb ri_for_bm = right;
if (preset == StereoBM_GPU::PREFILTER_XSOBEL)
if (preset_ == cv::StereoBM::PREFILTER_XSOBEL)
{
leBuf.create( left.size(), left.type());
riBuf.create(right.size(), right.type());
gpu::ensureSizeIsEnough(left.size(), left.type(), leBuf_);
gpu::ensureSizeIsEnough(right.size(), right.type(), riBuf_);
prefilter_xsobel( left, leBuf, 31, stream);
prefilter_xsobel(right, riBuf, 31, stream);
prefilter_xsobel( left, leBuf_, preFilterCap_, stream);
prefilter_xsobel(right, riBuf_, preFilterCap_, stream);
le_for_bm = leBuf;
ri_for_bm = riBuf;
le_for_bm = leBuf_;
ri_for_bm = riBuf_;
}
stereoBM_GPU(le_for_bm, ri_for_bm, disparity, ndisp, winSize, minSSD, stream);
stereoBM_GPU(le_for_bm, ri_for_bm, disparity, ndisp_, winSize_, minSSD_, stream);
if (avergeTexThreshold)
postfilter_textureness(le_for_bm, winSize, avergeTexThreshold, disparity, stream);
if (avergeTexThreshold_ > 0)
postfilter_textureness(le_for_bm, winSize_, avergeTexThreshold_, disparity, stream);
}
}
void cv::gpu::StereoBM_GPU::operator() ( const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream)
Ptr<gpu::StereoBM> cv::gpu::createStereoBM(int numDisparities, int blockSize)
{
stereo_bm_gpu_operator(minSSD, leBuf, riBuf, preset, ndisp, winSize, avergeTexThreshold, left, right, disparity, StreamAccessor::getStream(stream));
return new StereoBMImpl(numDisparities, blockSize);
}
#endif /* !defined (HAVE_CUDA) */

@ -49,12 +49,7 @@ using namespace cv::gpu;
void cv::gpu::StereoBeliefPropagation::estimateRecommendedParams(int, int, int&, int&, int&) { throw_no_cuda(); }
cv::gpu::StereoBeliefPropagation::StereoBeliefPropagation(int, int, int, int) { throw_no_cuda(); }
cv::gpu::StereoBeliefPropagation::StereoBeliefPropagation(int, int, int, float, float, float, float, int) { throw_no_cuda(); }
void cv::gpu::StereoBeliefPropagation::operator()(const GpuMat&, const GpuMat&, GpuMat&, Stream&) { throw_no_cuda(); }
void cv::gpu::StereoBeliefPropagation::operator()(const GpuMat&, GpuMat&, Stream&) { throw_no_cuda(); }
Ptr<gpu::StereoBeliefPropagation> cv::gpu::createStereoBeliefPropagation(int, int, int, int) { throw_no_cuda(); return Ptr<gpu::StereoBeliefPropagation>(); }
#else /* !defined (HAVE_CUDA) */
@ -78,263 +73,308 @@ namespace cv { namespace gpu { namespace cudev
}
}}}
using namespace ::cv::gpu::cudev::stereobp;
namespace
{
class StereoBPImpl : public gpu::StereoBeliefPropagation
{
public:
StereoBPImpl(int ndisp, int iters, int levels, int msg_type);
void compute(InputArray left, InputArray right, OutputArray disparity);
void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream);
void compute(InputArray data, OutputArray disparity, Stream& stream);
int getMinDisparity() const { return 0; }
void setMinDisparity(int /*minDisparity*/) {}
int getNumDisparities() const { return ndisp_; }
void setNumDisparities(int numDisparities) { ndisp_ = numDisparities; }
int getBlockSize() const { return 0; }
void setBlockSize(int /*blockSize*/) {}
int getSpeckleWindowSize() const { return 0; }
void setSpeckleWindowSize(int /*speckleWindowSize*/) {}
int getSpeckleRange() const { return 0; }
void setSpeckleRange(int /*speckleRange*/) {}
int getDisp12MaxDiff() const { return 0; }
void setDisp12MaxDiff(int /*disp12MaxDiff*/) {}
int getNumIters() const { return iters_; }
void setNumIters(int iters) { iters_ = iters; }
int getNumLevels() const { return levels_; }
void setNumLevels(int levels) { levels_ = levels; }
double getMaxDataTerm() const { return max_data_term_; }
void setMaxDataTerm(double max_data_term) { max_data_term_ = (float) max_data_term; }
double getDataWeight() const { return data_weight_; }
void setDataWeight(double data_weight) { data_weight_ = (float) data_weight; }
double getMaxDiscTerm() const { return max_disc_term_; }
void setMaxDiscTerm(double max_disc_term) { max_disc_term_ = (float) max_disc_term; }
double getDiscSingleJump() const { return disc_single_jump_; }
void setDiscSingleJump(double disc_single_jump) { disc_single_jump_ = (float) disc_single_jump; }
int getMsgType() const { return msg_type_; }
void setMsgType(int msg_type) { msg_type_ = msg_type; }
private:
void init(Stream& stream);
void calcBP(OutputArray disp, Stream& stream);
int ndisp_;
int iters_;
int levels_;
float max_data_term_;
float data_weight_;
float max_disc_term_;
float disc_single_jump_;
int msg_type_;
float scale_;
int rows_, cols_;
std::vector<int> cols_all_, rows_all_;
GpuMat u_, d_, l_, r_, u2_, d2_, l2_, r2_;
std::vector<GpuMat> datas_;
GpuMat outBuf_;
};
const float DEFAULT_MAX_DATA_TERM = 10.0f;
const float DEFAULT_DATA_WEIGHT = 0.07f;
const float DEFAULT_MAX_DISC_TERM = 1.7f;
const float DEFAULT_DISC_SINGLE_JUMP = 1.0f;
}
void cv::gpu::StereoBeliefPropagation::estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels)
{
ndisp = width / 4;
if ((ndisp & 1) != 0)
ndisp++;
StereoBPImpl::StereoBPImpl(int ndisp, int iters, int levels, int msg_type) :
ndisp_(ndisp), iters_(iters), levels_(levels),
max_data_term_(DEFAULT_MAX_DATA_TERM), data_weight_(DEFAULT_DATA_WEIGHT),
max_disc_term_(DEFAULT_MAX_DISC_TERM), disc_single_jump_(DEFAULT_DISC_SINGLE_JUMP),
msg_type_(msg_type)
{
}
int mm = std::max(width, height);
iters = mm / 100 + 2;
void StereoBPImpl::compute(InputArray left, InputArray right, OutputArray disparity)
{
compute(left, right, disparity, Stream::Null());
}
levels = (int)(::log(static_cast<double>(mm)) + 1) * 4 / 5;
if (levels == 0) levels++;
}
void StereoBPImpl::compute(InputArray _left, InputArray _right, OutputArray disparity, Stream& stream)
{
using namespace cv::gpu::cudev::stereobp;
cv::gpu::StereoBeliefPropagation::StereoBeliefPropagation(int ndisp_, int iters_, int levels_, int msg_type_)
: ndisp(ndisp_), iters(iters_), levels(levels_),
max_data_term(DEFAULT_MAX_DATA_TERM), data_weight(DEFAULT_DATA_WEIGHT),
max_disc_term(DEFAULT_MAX_DISC_TERM), disc_single_jump(DEFAULT_DISC_SINGLE_JUMP),
msg_type(msg_type_), datas(levels_)
{
}
typedef void (*comp_data_t)(const PtrStepSzb& left, const PtrStepSzb& right, const PtrStepSzb& data, cudaStream_t stream);
static const comp_data_t comp_data_callers[2][5] =
{
{0, comp_data_gpu<unsigned char, short>, 0, comp_data_gpu<uchar3, short>, comp_data_gpu<uchar4, short>},
{0, comp_data_gpu<unsigned char, float>, 0, comp_data_gpu<uchar3, float>, comp_data_gpu<uchar4, float>}
};
cv::gpu::StereoBeliefPropagation::StereoBeliefPropagation(int ndisp_, int iters_, int levels_, float max_data_term_, float data_weight_, float max_disc_term_, float disc_single_jump_, int msg_type_)
: ndisp(ndisp_), iters(iters_), levels(levels_),
max_data_term(max_data_term_), data_weight(data_weight_),
max_disc_term(max_disc_term_), disc_single_jump(disc_single_jump_),
msg_type(msg_type_), datas(levels_)
{
}
scale_ = msg_type_ == CV_32F ? 1.0f : 10.0f;
namespace
{
class StereoBeliefPropagationImpl
{
public:
StereoBeliefPropagationImpl(StereoBeliefPropagation& rthis_,
GpuMat& u_, GpuMat& d_, GpuMat& l_, GpuMat& r_,
GpuMat& u2_, GpuMat& d2_, GpuMat& l2_, GpuMat& r2_,
std::vector<GpuMat>& datas_, GpuMat& out_)
: rthis(rthis_), u(u_), d(d_), l(l_), r(r_), u2(u2_), d2(d2_), l2(l2_), r2(r2_), datas(datas_), out(out_),
zero(Scalar::all(0)), scale(rthis_.msg_type == CV_32F ? 1.0f : 10.0f)
{
CV_Assert(0 < rthis.ndisp && 0 < rthis.iters && 0 < rthis.levels);
CV_Assert(rthis.msg_type == CV_32F || rthis.msg_type == CV_16S);
CV_Assert(rthis.msg_type == CV_32F || (1 << (rthis.levels - 1)) * scale * rthis.max_data_term < std::numeric_limits<short>::max());
}
CV_Assert( 0 < ndisp_ && 0 < iters_ && 0 < levels_ );
CV_Assert( msg_type_ == CV_32F || msg_type_ == CV_16S );
CV_Assert( msg_type_ == CV_32F || (1 << (levels_ - 1)) * scale_ * max_data_term_ < std::numeric_limits<short>::max() );
void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disp, Stream& stream)
{
typedef void (*comp_data_t)(const PtrStepSzb& left, const PtrStepSzb& right, const PtrStepSzb& data, cudaStream_t stream);
static const comp_data_t comp_data_callers[2][5] =
{
{0, comp_data_gpu<unsigned char, short>, 0, comp_data_gpu<uchar3, short>, comp_data_gpu<uchar4, short>},
{0, comp_data_gpu<unsigned char, float>, 0, comp_data_gpu<uchar3, float>, comp_data_gpu<uchar4, float>}
};
GpuMat left = _left.getGpuMat();
GpuMat right = _right.getGpuMat();
CV_Assert(left.size() == right.size() && left.type() == right.type());
CV_Assert(left.type() == CV_8UC1 || left.type() == CV_8UC3 || left.type() == CV_8UC4);
CV_Assert( left.type() == CV_8UC1 || left.type() == CV_8UC3 || left.type() == CV_8UC4 );
CV_Assert( left.size() == right.size() && left.type() == right.type() );
rows = left.rows;
cols = left.cols;
rows_ = left.rows;
cols_ = left.cols;
int divisor = (int)pow(2.f, rthis.levels - 1.0f);
int lowest_cols = cols / divisor;
int lowest_rows = rows / divisor;
const int min_image_dim_size = 2;
CV_Assert(std::min(lowest_cols, lowest_rows) > min_image_dim_size);
const int divisor = (int) pow(2.f, levels_ - 1.0f);
const int lowest_cols = cols_ / divisor;
const int lowest_rows = rows_ / divisor;
const int min_image_dim_size = 2;
CV_Assert( std::min(lowest_cols, lowest_rows) > min_image_dim_size );
init(stream);
init(stream);
datas[0].create(rows * rthis.ndisp, cols, rthis.msg_type);
datas_[0].create(rows_ * ndisp_, cols_, msg_type_);
comp_data_callers[rthis.msg_type == CV_32F][left.channels()](left, right, datas[0], StreamAccessor::getStream(stream));
comp_data_callers[msg_type_ == CV_32F][left.channels()](left, right, datas_[0], StreamAccessor::getStream(stream));
calcBP(disp, stream);
}
calcBP(disparity, stream);
}
void operator()(const GpuMat& data, GpuMat& disp, Stream& stream)
{
CV_Assert((data.type() == rthis.msg_type) && (data.rows % rthis.ndisp == 0));
void StereoBPImpl::compute(InputArray _data, OutputArray disparity, Stream& stream)
{
scale_ = msg_type_ == CV_32F ? 1.0f : 10.0f;
rows = data.rows / rthis.ndisp;
cols = data.cols;
CV_Assert( 0 < ndisp_ && 0 < iters_ && 0 < levels_ );
CV_Assert( msg_type_ == CV_32F || msg_type_ == CV_16S );
CV_Assert( msg_type_ == CV_32F || (1 << (levels_ - 1)) * scale_ * max_data_term_ < std::numeric_limits<short>::max() );
int divisor = (int)pow(2.f, rthis.levels - 1.0f);
int lowest_cols = cols / divisor;
int lowest_rows = rows / divisor;
const int min_image_dim_size = 2;
CV_Assert(std::min(lowest_cols, lowest_rows) > min_image_dim_size);
GpuMat data = _data.getGpuMat();
init(stream);
CV_Assert( (data.type() == msg_type_) && (data.rows % ndisp_ == 0) );
datas[0] = data;
rows_ = data.rows / ndisp_;
cols_ = data.cols;
calcBP(disp, stream);
}
private:
void init(Stream& stream)
{
u.create(rows * rthis.ndisp, cols, rthis.msg_type);
d.create(rows * rthis.ndisp, cols, rthis.msg_type);
l.create(rows * rthis.ndisp, cols, rthis.msg_type);
r.create(rows * rthis.ndisp, cols, rthis.msg_type);
const int divisor = (int) pow(2.f, levels_ - 1.0f);
const int lowest_cols = cols_ / divisor;
const int lowest_rows = rows_ / divisor;
const int min_image_dim_size = 2;
CV_Assert( std::min(lowest_cols, lowest_rows) > min_image_dim_size );
if (rthis.levels & 1)
{
//can clear less area
u.setTo(zero, stream);
d.setTo(zero, stream);
l.setTo(zero, stream);
r.setTo(zero, stream);
}
init(stream);
if (rthis.levels > 1)
{
int less_rows = (rows + 1) / 2;
int less_cols = (cols + 1) / 2;
u2.create(less_rows * rthis.ndisp, less_cols, rthis.msg_type);
d2.create(less_rows * rthis.ndisp, less_cols, rthis.msg_type);
l2.create(less_rows * rthis.ndisp, less_cols, rthis.msg_type);
r2.create(less_rows * rthis.ndisp, less_cols, rthis.msg_type);
if ((rthis.levels & 1) == 0)
{
u2.setTo(zero, stream);
d2.setTo(zero, stream);
l2.setTo(zero, stream);
r2.setTo(zero, stream);
}
}
data.copyTo(datas_[0], stream);
load_constants(rthis.ndisp, rthis.max_data_term, scale * rthis.data_weight, scale * rthis.max_disc_term, scale * rthis.disc_single_jump);
calcBP(disparity, stream);
}
datas.resize(rthis.levels);
void StereoBPImpl::init(Stream& stream)
{
using namespace cv::gpu::cudev::stereobp;
cols_all.resize(rthis.levels);
rows_all.resize(rthis.levels);
u_.create(rows_ * ndisp_, cols_, msg_type_);
d_.create(rows_ * ndisp_, cols_, msg_type_);
l_.create(rows_ * ndisp_, cols_, msg_type_);
r_.create(rows_ * ndisp_, cols_, msg_type_);
cols_all[0] = cols;
rows_all[0] = rows;
if (levels_ & 1)
{
//can clear less area
u_.setTo(0, stream);
d_.setTo(0, stream);
l_.setTo(0, stream);
r_.setTo(0, stream);
}
void calcBP(GpuMat& disp, Stream& stream)
if (levels_ > 1)
{
typedef void (*data_step_down_t)(int dst_cols, int dst_rows, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream);
static const data_step_down_t data_step_down_callers[2] =
{
data_step_down_gpu<short>, data_step_down_gpu<float>
};
int less_rows = (rows_ + 1) / 2;
int less_cols = (cols_ + 1) / 2;
typedef void (*level_up_messages_t)(int dst_idx, int dst_cols, int dst_rows, int src_rows, PtrStepSzb* mus, PtrStepSzb* mds, PtrStepSzb* mls, PtrStepSzb* mrs, cudaStream_t stream);
static const level_up_messages_t level_up_messages_callers[2] =
{
level_up_messages_gpu<short>, level_up_messages_gpu<float>
};
u2_.create(less_rows * ndisp_, less_cols, msg_type_);
d2_.create(less_rows * ndisp_, less_cols, msg_type_);
l2_.create(less_rows * ndisp_, less_cols, msg_type_);
r2_.create(less_rows * ndisp_, less_cols, msg_type_);
typedef void (*calc_all_iterations_t)(int cols, int rows, int iters, const PtrStepSzb& u, const PtrStepSzb& d, const PtrStepSzb& l, const PtrStepSzb& r, const PtrStepSzb& data, cudaStream_t stream);
static const calc_all_iterations_t calc_all_iterations_callers[2] =
if ((levels_ & 1) == 0)
{
calc_all_iterations_gpu<short>, calc_all_iterations_gpu<float>
};
u2_.setTo(0, stream);
d2_.setTo(0, stream);
l2_.setTo(0, stream);
r2_.setTo(0, stream);
}
}
typedef void (*output_t)(const PtrStepSzb& u, const PtrStepSzb& d, const PtrStepSzb& l, const PtrStepSzb& r, const PtrStepSzb& data, const PtrStepSz<short>& disp, cudaStream_t stream);
static const output_t output_callers[2] =
{
output_gpu<short>, output_gpu<float>
};
load_constants(ndisp_, max_data_term_, scale_ * data_weight_, scale_ * max_disc_term_, scale_ * disc_single_jump_);
const int funcIdx = rthis.msg_type == CV_32F;
datas_.resize(levels_);
cudaStream_t cudaStream = StreamAccessor::getStream(stream);
cols_all_.resize(levels_);
rows_all_.resize(levels_);
for (int i = 1; i < rthis.levels; ++i)
{
cols_all[i] = (cols_all[i-1] + 1) / 2;
rows_all[i] = (rows_all[i-1] + 1) / 2;
cols_all_[0] = cols_;
rows_all_[0] = rows_;
}
datas[i].create(rows_all[i] * rthis.ndisp, cols_all[i], rthis.msg_type);
void StereoBPImpl::calcBP(OutputArray disp, Stream& _stream)
{
using namespace cv::gpu::cudev::stereobp;
data_step_down_callers[funcIdx](cols_all[i], rows_all[i], rows_all[i-1], datas[i-1], datas[i], cudaStream);
}
typedef void (*data_step_down_t)(int dst_cols, int dst_rows, int src_rows, const PtrStepSzb& src, const PtrStepSzb& dst, cudaStream_t stream);
static const data_step_down_t data_step_down_callers[2] =
{
data_step_down_gpu<short>, data_step_down_gpu<float>
};
PtrStepSzb mus[] = {u, u2};
PtrStepSzb mds[] = {d, d2};
PtrStepSzb mrs[] = {r, r2};
PtrStepSzb mls[] = {l, l2};
typedef void (*level_up_messages_t)(int dst_idx, int dst_cols, int dst_rows, int src_rows, PtrStepSzb* mus, PtrStepSzb* mds, PtrStepSzb* mls, PtrStepSzb* mrs, cudaStream_t stream);
static const level_up_messages_t level_up_messages_callers[2] =
{
level_up_messages_gpu<short>, level_up_messages_gpu<float>
};
int mem_idx = (rthis.levels & 1) ? 0 : 1;
typedef void (*calc_all_iterations_t)(int cols, int rows, int iters, const PtrStepSzb& u, const PtrStepSzb& d, const PtrStepSzb& l, const PtrStepSzb& r, const PtrStepSzb& data, cudaStream_t stream);
static const calc_all_iterations_t calc_all_iterations_callers[2] =
{
calc_all_iterations_gpu<short>, calc_all_iterations_gpu<float>
};
for (int i = rthis.levels - 1; i >= 0; --i)
{
// for lower level we have already computed messages by setting to zero
if (i != rthis.levels - 1)
level_up_messages_callers[funcIdx](mem_idx, cols_all[i], rows_all[i], rows_all[i+1], mus, mds, mls, mrs, cudaStream);
typedef void (*output_t)(const PtrStepSzb& u, const PtrStepSzb& d, const PtrStepSzb& l, const PtrStepSzb& r, const PtrStepSzb& data, const PtrStepSz<short>& disp, cudaStream_t stream);
static const output_t output_callers[2] =
{
output_gpu<short>, output_gpu<float>
};
calc_all_iterations_callers[funcIdx](cols_all[i], rows_all[i], rthis.iters, mus[mem_idx], mds[mem_idx], mls[mem_idx], mrs[mem_idx], datas[i], cudaStream);
const int funcIdx = msg_type_ == CV_32F;
mem_idx = (mem_idx + 1) & 1;
}
cudaStream_t stream = StreamAccessor::getStream(_stream);
if (disp.empty())
disp.create(rows, cols, CV_16S);
for (int i = 1; i < levels_; ++i)
{
cols_all_[i] = (cols_all_[i-1] + 1) / 2;
rows_all_[i] = (rows_all_[i-1] + 1) / 2;
out = ((disp.type() == CV_16S) ? disp : (out.create(rows, cols, CV_16S), out));
datas_[i].create(rows_all_[i] * ndisp_, cols_all_[i], msg_type_);
out.setTo(zero, stream);
data_step_down_callers[funcIdx](cols_all_[i], rows_all_[i], rows_all_[i-1], datas_[i-1], datas_[i], stream);
}
output_callers[funcIdx](u, d, l, r, datas.front(), out, cudaStream);
PtrStepSzb mus[] = {u_, u2_};
PtrStepSzb mds[] = {d_, d2_};
PtrStepSzb mrs[] = {r_, r2_};
PtrStepSzb mls[] = {l_, l2_};
if (disp.type() != CV_16S)
out.convertTo(disp, disp.type(), stream);
}
int mem_idx = (levels_ & 1) ? 0 : 1;
StereoBeliefPropagation& rthis;
for (int i = levels_ - 1; i >= 0; --i)
{
// for lower level we have already computed messages by setting to zero
if (i != levels_ - 1)
level_up_messages_callers[funcIdx](mem_idx, cols_all_[i], rows_all_[i], rows_all_[i+1], mus, mds, mls, mrs, stream);
calc_all_iterations_callers[funcIdx](cols_all_[i], rows_all_[i], iters_, mus[mem_idx], mds[mem_idx], mls[mem_idx], mrs[mem_idx], datas_[i], stream);
GpuMat& u;
GpuMat& d;
GpuMat& l;
GpuMat& r;
mem_idx = (mem_idx + 1) & 1;
}
GpuMat& u2;
GpuMat& d2;
GpuMat& l2;
GpuMat& r2;
const int dtype = disp.fixedType() ? disp.type() : CV_16SC1;
std::vector<GpuMat>& datas;
GpuMat& out;
disp.create(rows_, cols_, dtype);
GpuMat out = disp.getGpuMat();
const Scalar zero;
const float scale;
if (dtype != CV_16SC1)
{
outBuf_.create(rows_, cols_, CV_16SC1);
out = outBuf_;
}
int rows, cols;
out.setTo(0, _stream);
std::vector<int> cols_all, rows_all;
};
output_callers[funcIdx](u_, d_, l_, r_, datas_.front(), out, stream);
if (dtype != CV_16SC1)
out.convertTo(disp, dtype, _stream);
}
}
void cv::gpu::StereoBeliefPropagation::operator()(const GpuMat& left, const GpuMat& right, GpuMat& disp, Stream& stream)
Ptr<gpu::StereoBeliefPropagation> cv::gpu::createStereoBeliefPropagation(int ndisp, int iters, int levels, int msg_type)
{
StereoBeliefPropagationImpl impl(*this, u, d, l, r, u2, d2, l2, r2, datas, out);
impl(left, right, disp, stream);
return new StereoBPImpl(ndisp, iters, levels, msg_type);
}
void cv::gpu::StereoBeliefPropagation::operator()(const GpuMat& data, GpuMat& disp, Stream& stream)
void cv::gpu::StereoBeliefPropagation::estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels)
{
StereoBeliefPropagationImpl impl(*this, u, d, l, r, u2, d2, l2, r2, datas, out);
impl(data, disp, stream);
ndisp = width / 4;
if ((ndisp & 1) != 0)
ndisp++;
int mm = std::max(width, height);
iters = mm / 100 + 2;
levels = (int)(::log(static_cast<double>(mm)) + 1) * 4 / 5;
if (levels == 0) levels++;
}
#endif /* !defined (HAVE_CUDA) */

@ -49,13 +49,9 @@ using namespace cv::gpu;
void cv::gpu::StereoConstantSpaceBP::estimateRecommendedParams(int, int, int&, int&, int&, int&) { throw_no_cuda(); }
cv::gpu::StereoConstantSpaceBP::StereoConstantSpaceBP(int, int, int, int, int) { throw_no_cuda(); }
cv::gpu::StereoConstantSpaceBP::StereoConstantSpaceBP(int, int, int, int, float, float, float, float, int, int) { throw_no_cuda(); }
void cv::gpu::StereoConstantSpaceBP::operator()(const GpuMat&, const GpuMat&, GpuMat&, Stream&) { throw_no_cuda(); }
Ptr<gpu::StereoConstantSpaceBP> cv::gpu::createStereoConstantSpaceBP(int, int, int, int, int) { throw_no_cuda(); return Ptr<gpu::StereoConstantSpaceBP>(); }
#else /* !defined (HAVE_CUDA) */
#include "opencv2/core/utility.hpp"
namespace cv { namespace gpu { namespace cudev
{
@ -89,199 +85,303 @@ namespace cv { namespace gpu { namespace cudev
}
}}}
using namespace ::cv::gpu::cudev::stereocsbp;
namespace
{
const float DEFAULT_MAX_DATA_TERM = 30.0f;
const float DEFAULT_DATA_WEIGHT = 1.0f;
const float DEFAULT_MAX_DISC_TERM = 160.0f;
const float DEFAULT_DISC_SINGLE_JUMP = 10.0f;
}
class StereoCSBPImpl : public gpu::StereoConstantSpaceBP
{
public:
StereoCSBPImpl(int ndisp, int iters, int levels, int nr_plane, int msg_type);
void cv::gpu::StereoConstantSpaceBP::estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane)
{
ndisp = (int) ((float) width / 3.14f);
if ((ndisp & 1) != 0)
ndisp++;
void compute(InputArray left, InputArray right, OutputArray disparity);
void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream);
void compute(InputArray data, OutputArray disparity, Stream& stream);
int mm = std::max(width, height);
iters = mm / 100 + ((mm > 1200)? - 4 : 4);
int getMinDisparity() const { return min_disp_th_; }
void setMinDisparity(int minDisparity) { min_disp_th_ = minDisparity; }
levels = (int)::log(static_cast<double>(mm)) * 2 / 3;
if (levels == 0) levels++;
int getNumDisparities() const { return ndisp_; }
void setNumDisparities(int numDisparities) { ndisp_ = numDisparities; }
nr_plane = (int) ((float) ndisp / std::pow(2.0, levels + 1));
}
int getBlockSize() const { return 0; }
void setBlockSize(int /*blockSize*/) {}
cv::gpu::StereoConstantSpaceBP::StereoConstantSpaceBP(int ndisp_, int iters_, int levels_, int nr_plane_,
int msg_type_)
int getSpeckleWindowSize() const { return 0; }
void setSpeckleWindowSize(int /*speckleWindowSize*/) {}
: ndisp(ndisp_), iters(iters_), levels(levels_), nr_plane(nr_plane_),
max_data_term(DEFAULT_MAX_DATA_TERM), data_weight(DEFAULT_DATA_WEIGHT),
max_disc_term(DEFAULT_MAX_DISC_TERM), disc_single_jump(DEFAULT_DISC_SINGLE_JUMP), min_disp_th(0),
msg_type(msg_type_), use_local_init_data_cost(true)
{
CV_Assert(msg_type_ == CV_32F || msg_type_ == CV_16S);
}
int getSpeckleRange() const { return 0; }
void setSpeckleRange(int /*speckleRange*/) {}
cv::gpu::StereoConstantSpaceBP::StereoConstantSpaceBP(int ndisp_, int iters_, int levels_, int nr_plane_,
float max_data_term_, float data_weight_, float max_disc_term_, float disc_single_jump_,
int min_disp_th_, int msg_type_)
: ndisp(ndisp_), iters(iters_), levels(levels_), nr_plane(nr_plane_),
max_data_term(max_data_term_), data_weight(data_weight_),
max_disc_term(max_disc_term_), disc_single_jump(disc_single_jump_), min_disp_th(min_disp_th_),
msg_type(msg_type_), use_local_init_data_cost(true)
{
CV_Assert(msg_type_ == CV_32F || msg_type_ == CV_16S);
}
int getDisp12MaxDiff() const { return 0; }
void setDisp12MaxDiff(int /*disp12MaxDiff*/) {}
template<class T>
static void csbp_operator(StereoConstantSpaceBP& rthis, GpuMat& mbuf, GpuMat& temp, GpuMat& out, const GpuMat& left, const GpuMat& right, GpuMat& disp, Stream& stream)
{
CV_DbgAssert(0 < rthis.ndisp && 0 < rthis.iters && 0 < rthis.levels && 0 < rthis.nr_plane
&& left.rows == right.rows && left.cols == right.cols && left.type() == right.type());
int getNumIters() const { return iters_; }
void setNumIters(int iters) { iters_ = iters; }
int getNumLevels() const { return levels_; }
void setNumLevels(int levels) { levels_ = levels; }
double getMaxDataTerm() const { return max_data_term_; }
void setMaxDataTerm(double max_data_term) { max_data_term_ = (float) max_data_term; }
double getDataWeight() const { return data_weight_; }
void setDataWeight(double data_weight) { data_weight_ = (float) data_weight; }
double getMaxDiscTerm() const { return max_disc_term_; }
void setMaxDiscTerm(double max_disc_term) { max_disc_term_ = (float) max_disc_term; }
CV_Assert(rthis.levels <= 8 && (left.type() == CV_8UC1 || left.type() == CV_8UC3 || left.type() == CV_8UC4));
double getDiscSingleJump() const { return disc_single_jump_; }
void setDiscSingleJump(double disc_single_jump) { disc_single_jump_ = (float) disc_single_jump; }
const Scalar zero = Scalar::all(0);
int getMsgType() const { return msg_type_; }
void setMsgType(int msg_type) { msg_type_ = msg_type; }
cudaStream_t cudaStream = StreamAccessor::getStream(stream);
int getNrPlane() const { return nr_plane_; }
void setNrPlane(int nr_plane) { nr_plane_ = nr_plane; }
////////////////////////////////////////////////////////////////////////////////////////////
// Init
bool getUseLocalInitDataCost() const { return use_local_init_data_cost_; }
void setUseLocalInitDataCost(bool use_local_init_data_cost) { use_local_init_data_cost_ = use_local_init_data_cost; }
int rows = left.rows;
int cols = left.cols;
private:
int min_disp_th_;
int ndisp_;
int iters_;
int levels_;
float max_data_term_;
float data_weight_;
float max_disc_term_;
float disc_single_jump_;
int msg_type_;
int nr_plane_;
bool use_local_init_data_cost_;
rthis.levels = std::min(rthis.levels, int(log((double)rthis.ndisp) / log(2.0)));
int levels = rthis.levels;
GpuMat mbuf_;
GpuMat temp_;
GpuMat outBuf_;
};
// compute sizes
AutoBuffer<int> buf(levels * 3);
int* cols_pyr = buf;
int* rows_pyr = cols_pyr + levels;
int* nr_plane_pyr = rows_pyr + levels;
const float DEFAULT_MAX_DATA_TERM = 30.0f;
const float DEFAULT_DATA_WEIGHT = 1.0f;
const float DEFAULT_MAX_DISC_TERM = 160.0f;
const float DEFAULT_DISC_SINGLE_JUMP = 10.0f;
cols_pyr[0] = cols;
rows_pyr[0] = rows;
nr_plane_pyr[0] = rthis.nr_plane;
StereoCSBPImpl::StereoCSBPImpl(int ndisp, int iters, int levels, int nr_plane, int msg_type) :
min_disp_th_(0), ndisp_(ndisp), iters_(iters), levels_(levels),
max_data_term_(DEFAULT_MAX_DATA_TERM), data_weight_(DEFAULT_DATA_WEIGHT),
max_disc_term_(DEFAULT_MAX_DISC_TERM), disc_single_jump_(DEFAULT_DISC_SINGLE_JUMP),
msg_type_(msg_type), nr_plane_(nr_plane), use_local_init_data_cost_(true)
{
}
for (int i = 1; i < levels; i++)
void StereoCSBPImpl::compute(InputArray left, InputArray right, OutputArray disparity)
{
cols_pyr[i] = cols_pyr[i-1] / 2;
rows_pyr[i] = rows_pyr[i-1] / 2;
nr_plane_pyr[i] = nr_plane_pyr[i-1] * 2;
compute(left, right, disparity, Stream::Null());
}
void StereoCSBPImpl::compute(InputArray _left, InputArray _right, OutputArray disp, Stream& _stream)
{
using namespace cv::gpu::cudev::stereocsbp;
GpuMat u[2], d[2], l[2], r[2], disp_selected_pyr[2], data_cost, data_cost_selected;
CV_Assert( msg_type_ == CV_32F || msg_type_ == CV_16S );
CV_Assert( 0 < ndisp_ && 0 < iters_ && 0 < levels_ && 0 < nr_plane_ && levels_ <= 8 );
GpuMat left = _left.getGpuMat();
GpuMat right = _right.getGpuMat();
//allocate buffers
int buffers_count = 10; // (up + down + left + right + disp_selected_pyr) * 2
buffers_count += 2; // data_cost has twice more rows than other buffers, what's why +2, not +1;
buffers_count += 1; // data_cost_selected
mbuf.create(rows * rthis.nr_plane * buffers_count, cols, DataType<T>::type);
CV_Assert( left.type() == CV_8UC1 || left.type() == CV_8UC3 || left.type() == CV_8UC4 );
CV_Assert( left.size() == right.size() && left.type() == right.type() );
data_cost = mbuf.rowRange(0, rows * rthis.nr_plane * 2);
data_cost_selected = mbuf.rowRange(data_cost.rows, data_cost.rows + rows * rthis.nr_plane);
cudaStream_t stream = StreamAccessor::getStream(_stream);
for(int k = 0; k < 2; ++k) // in/out
{
GpuMat sub1 = mbuf.rowRange(data_cost.rows + data_cost_selected.rows, mbuf.rows);
GpuMat sub2 = sub1.rowRange((k+0)*sub1.rows/2, (k+1)*sub1.rows/2);
////////////////////////////////////////////////////////////////////////////////////////////
// Init
GpuMat *buf_ptrs[] = { &u[k], &d[k], &l[k], &r[k], &disp_selected_pyr[k] };
for(int _r = 0; _r < 5; ++_r)
int rows = left.rows;
int cols = left.cols;
levels_ = std::min(levels_, int(log((double)ndisp_) / log(2.0)));
// compute sizes
AutoBuffer<int> buf(levels_ * 3);
int* cols_pyr = buf;
int* rows_pyr = cols_pyr + levels_;
int* nr_plane_pyr = rows_pyr + levels_;
cols_pyr[0] = cols;
rows_pyr[0] = rows;
nr_plane_pyr[0] = nr_plane_;
for (int i = 1; i < levels_; i++)
{
*buf_ptrs[_r] = sub2.rowRange(_r * sub2.rows/5, (_r+1) * sub2.rows/5);
CV_DbgAssert(buf_ptrs[_r]->cols == cols && buf_ptrs[_r]->rows == rows * rthis.nr_plane);
cols_pyr[i] = cols_pyr[i-1] / 2;
rows_pyr[i] = rows_pyr[i-1] / 2;
nr_plane_pyr[i] = nr_plane_pyr[i-1] * 2;
}
};
size_t elem_step = mbuf.step / sizeof(T);
GpuMat u[2], d[2], l[2], r[2], disp_selected_pyr[2], data_cost, data_cost_selected;
Size temp_size = data_cost.size();
if ((size_t)temp_size.area() < elem_step * rows_pyr[levels - 1] * rthis.ndisp)
temp_size = Size(static_cast<int>(elem_step), rows_pyr[levels - 1] * rthis.ndisp);
//allocate buffers
int buffers_count = 10; // (up + down + left + right + disp_selected_pyr) * 2
buffers_count += 2; // data_cost has twice more rows than other buffers, what's why +2, not +1;
buffers_count += 1; // data_cost_selected
mbuf_.create(rows * nr_plane_ * buffers_count, cols, msg_type_);
temp.create(temp_size, DataType<T>::type);
data_cost = mbuf_.rowRange(0, rows * nr_plane_ * 2);
data_cost_selected = mbuf_.rowRange(data_cost.rows, data_cost.rows + rows * nr_plane_);
////////////////////////////////////////////////////////////////////////////
// Compute
for(int k = 0; k < 2; ++k) // in/out
{
GpuMat sub1 = mbuf_.rowRange(data_cost.rows + data_cost_selected.rows, mbuf_.rows);
GpuMat sub2 = sub1.rowRange((k+0)*sub1.rows/2, (k+1)*sub1.rows/2);
load_constants(rthis.ndisp, rthis.max_data_term, rthis.data_weight, rthis.max_disc_term, rthis.disc_single_jump, rthis.min_disp_th, left, right, temp);
GpuMat *buf_ptrs[] = { &u[k], &d[k], &l[k], &r[k], &disp_selected_pyr[k] };
for(int _r = 0; _r < 5; ++_r)
{
*buf_ptrs[_r] = sub2.rowRange(_r * sub2.rows/5, (_r+1) * sub2.rows/5);
CV_DbgAssert( buf_ptrs[_r]->cols == cols && buf_ptrs[_r]->rows == rows * nr_plane_ );
}
};
l[0].setTo(zero, stream);
d[0].setTo(zero, stream);
r[0].setTo(zero, stream);
u[0].setTo(zero, stream);
size_t elem_step = mbuf_.step / mbuf_.elemSize();
l[1].setTo(zero, stream);
d[1].setTo(zero, stream);
r[1].setTo(zero, stream);
u[1].setTo(zero, stream);
Size temp_size = data_cost.size();
if ((size_t)temp_size.area() < elem_step * rows_pyr[levels_ - 1] * ndisp_)
temp_size = Size(static_cast<int>(elem_step), rows_pyr[levels_ - 1] * ndisp_);
data_cost.setTo(zero, stream);
data_cost_selected.setTo(zero, stream);
temp_.create(temp_size, msg_type_);
int cur_idx = 0;
////////////////////////////////////////////////////////////////////////////
// Compute
for (int i = levels - 1; i >= 0; i--)
{
if (i == levels - 1)
load_constants(ndisp_, max_data_term_, data_weight_, max_disc_term_, disc_single_jump_, min_disp_th_, left, right, temp_);
l[0].setTo(0, _stream);
d[0].setTo(0, _stream);
r[0].setTo(0, _stream);
u[0].setTo(0, _stream);
l[1].setTo(0, _stream);
d[1].setTo(0, _stream);
r[1].setTo(0, _stream);
u[1].setTo(0, _stream);
data_cost.setTo(0, _stream);
data_cost_selected.setTo(0, _stream);
int cur_idx = 0;
if (msg_type_ == CV_32F)
{
init_data_cost(left.rows, left.cols, disp_selected_pyr[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(),
elem_step, rows_pyr[i], cols_pyr[i], i, nr_plane_pyr[i], rthis.ndisp, left.channels(), rthis.use_local_init_data_cost, cudaStream);
for (int i = levels_ - 1; i >= 0; i--)
{
if (i == levels_ - 1)
{
init_data_cost(left.rows, left.cols, disp_selected_pyr[cur_idx].ptr<float>(), data_cost_selected.ptr<float>(),
elem_step, rows_pyr[i], cols_pyr[i], i, nr_plane_pyr[i], ndisp_, left.channels(), use_local_init_data_cost_, stream);
}
else
{
compute_data_cost(disp_selected_pyr[cur_idx].ptr<float>(), data_cost.ptr<float>(), elem_step,
left.rows, left.cols, rows_pyr[i], cols_pyr[i], rows_pyr[i+1], i, nr_plane_pyr[i+1], left.channels(), stream);
int new_idx = (cur_idx + 1) & 1;
init_message(u[new_idx].ptr<float>(), d[new_idx].ptr<float>(), l[new_idx].ptr<float>(), r[new_idx].ptr<float>(),
u[cur_idx].ptr<float>(), d[cur_idx].ptr<float>(), l[cur_idx].ptr<float>(), r[cur_idx].ptr<float>(),
disp_selected_pyr[new_idx].ptr<float>(), disp_selected_pyr[cur_idx].ptr<float>(),
data_cost_selected.ptr<float>(), data_cost.ptr<float>(), elem_step, rows_pyr[i],
cols_pyr[i], nr_plane_pyr[i], rows_pyr[i+1], cols_pyr[i+1], nr_plane_pyr[i+1], stream);
cur_idx = new_idx;
}
calc_all_iterations(u[cur_idx].ptr<float>(), d[cur_idx].ptr<float>(), l[cur_idx].ptr<float>(), r[cur_idx].ptr<float>(),
data_cost_selected.ptr<float>(), disp_selected_pyr[cur_idx].ptr<float>(), elem_step,
rows_pyr[i], cols_pyr[i], nr_plane_pyr[i], iters_, stream);
}
}
else
{
compute_data_cost(disp_selected_pyr[cur_idx].ptr<T>(), data_cost.ptr<T>(), elem_step,
left.rows, left.cols, rows_pyr[i], cols_pyr[i], rows_pyr[i+1], i, nr_plane_pyr[i+1], left.channels(), cudaStream);
for (int i = levels_ - 1; i >= 0; i--)
{
if (i == levels_ - 1)
{
init_data_cost(left.rows, left.cols, disp_selected_pyr[cur_idx].ptr<short>(), data_cost_selected.ptr<short>(),
elem_step, rows_pyr[i], cols_pyr[i], i, nr_plane_pyr[i], ndisp_, left.channels(), use_local_init_data_cost_, stream);
}
else
{
compute_data_cost(disp_selected_pyr[cur_idx].ptr<short>(), data_cost.ptr<short>(), elem_step,
left.rows, left.cols, rows_pyr[i], cols_pyr[i], rows_pyr[i+1], i, nr_plane_pyr[i+1], left.channels(), stream);
int new_idx = (cur_idx + 1) & 1;
init_message(u[new_idx].ptr<short>(), d[new_idx].ptr<short>(), l[new_idx].ptr<short>(), r[new_idx].ptr<short>(),
u[cur_idx].ptr<short>(), d[cur_idx].ptr<short>(), l[cur_idx].ptr<short>(), r[cur_idx].ptr<short>(),
disp_selected_pyr[new_idx].ptr<short>(), disp_selected_pyr[cur_idx].ptr<short>(),
data_cost_selected.ptr<short>(), data_cost.ptr<short>(), elem_step, rows_pyr[i],
cols_pyr[i], nr_plane_pyr[i], rows_pyr[i+1], cols_pyr[i+1], nr_plane_pyr[i+1], stream);
cur_idx = new_idx;
}
calc_all_iterations(u[cur_idx].ptr<short>(), d[cur_idx].ptr<short>(), l[cur_idx].ptr<short>(), r[cur_idx].ptr<short>(),
data_cost_selected.ptr<short>(), disp_selected_pyr[cur_idx].ptr<short>(), elem_step,
rows_pyr[i], cols_pyr[i], nr_plane_pyr[i], iters_, stream);
}
}
int new_idx = (cur_idx + 1) & 1;
const int dtype = disp.fixedType() ? disp.type() : CV_16SC1;
init_message(u[new_idx].ptr<T>(), d[new_idx].ptr<T>(), l[new_idx].ptr<T>(), r[new_idx].ptr<T>(),
u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(),
disp_selected_pyr[new_idx].ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(),
data_cost_selected.ptr<T>(), data_cost.ptr<T>(), elem_step, rows_pyr[i],
cols_pyr[i], nr_plane_pyr[i], rows_pyr[i+1], cols_pyr[i+1], nr_plane_pyr[i+1], cudaStream);
disp.create(rows, cols, dtype);
GpuMat out = disp.getGpuMat();
cur_idx = new_idx;
if (dtype != CV_16SC1)
{
outBuf_.create(rows, cols, CV_16SC1);
out = outBuf_;
}
calc_all_iterations(u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(),
data_cost_selected.ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(), elem_step,
rows_pyr[i], cols_pyr[i], nr_plane_pyr[i], rthis.iters, cudaStream);
}
out.setTo(0, _stream);
if (disp.empty())
disp.create(rows, cols, CV_16S);
out = ((disp.type() == CV_16S) ? disp : (out.create(rows, cols, CV_16S), out));
out.setTo(zero, stream);
if (msg_type_ == CV_32F)
{
compute_disp(u[cur_idx].ptr<float>(), d[cur_idx].ptr<float>(), l[cur_idx].ptr<float>(), r[cur_idx].ptr<float>(),
data_cost_selected.ptr<float>(), disp_selected_pyr[cur_idx].ptr<float>(), elem_step, out, nr_plane_pyr[0], stream);
}
else
{
compute_disp(u[cur_idx].ptr<short>(), d[cur_idx].ptr<short>(), l[cur_idx].ptr<short>(), r[cur_idx].ptr<short>(),
data_cost_selected.ptr<short>(), disp_selected_pyr[cur_idx].ptr<short>(), elem_step, out, nr_plane_pyr[0], stream);
}
compute_disp(u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(),
data_cost_selected.ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(), elem_step, out, nr_plane_pyr[0], cudaStream);
if (dtype != CV_16SC1)
out.convertTo(disp, dtype, _stream);
}
if (disp.type() != CV_16S)
void StereoCSBPImpl::compute(InputArray /*data*/, OutputArray /*disparity*/, Stream& /*stream*/)
{
out.convertTo(disp, disp.type(), stream);
CV_Error(Error::StsNotImplemented, "Not implemented");
}
}
Ptr<gpu::StereoConstantSpaceBP> cv::gpu::createStereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane, int msg_type)
{
return new StereoCSBPImpl(ndisp, iters, levels, nr_plane, msg_type);
}
void cv::gpu::StereoConstantSpaceBP::estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane)
{
ndisp = (int) ((float) width / 3.14f);
if ((ndisp & 1) != 0)
ndisp++;
typedef void (*csbp_operator_t)(StereoConstantSpaceBP& rthis, GpuMat& mbuf,
GpuMat& temp, GpuMat& out, const GpuMat& left, const GpuMat& right, GpuMat& disp, Stream& stream);
int mm = std::max(width, height);
iters = mm / 100 + ((mm > 1200)? - 4 : 4);
const static csbp_operator_t operators[] = {0, 0, 0, csbp_operator<short>, 0, csbp_operator<float>, 0, 0};
levels = (int)::log(static_cast<double>(mm)) * 2 / 3;
if (levels == 0) levels++;
void cv::gpu::StereoConstantSpaceBP::operator()(const GpuMat& left, const GpuMat& right, GpuMat& disp, Stream& stream)
{
CV_Assert(msg_type == CV_32F || msg_type == CV_16S);
operators[msg_type](*this, messages_buffers, temp, out, left, right, disp, stream);
nr_plane = (int) ((float) ndisp / std::pow(2.0, levels + 1));
}
#endif /* !defined (HAVE_CUDA) */

@ -47,8 +47,8 @@ using namespace cv::gpu;
#if !defined HAVE_CUDA || defined(CUDA_DISABLER)
void cv::gpu::reprojectImageTo3D(const GpuMat&, GpuMat&, const Mat&, int, Stream&) { throw_no_cuda(); }
void cv::gpu::drawColorDisp(const GpuMat&, GpuMat&, int, Stream&) { throw_no_cuda(); }
void cv::gpu::reprojectImageTo3D(InputArray, OutputArray, InputArray, int, Stream&) { throw_no_cuda(); }
void cv::gpu::drawColorDisp(InputArray, OutputArray, int, Stream&) { throw_no_cuda(); }
#else
@ -61,7 +61,7 @@ namespace cv { namespace gpu { namespace cudev
void reprojectImageTo3D_gpu(const PtrStepSzb disp, PtrStepSzb xyz, const float* q, cudaStream_t stream);
}}}
void cv::gpu::reprojectImageTo3D(const GpuMat& disp, GpuMat& xyz, const Mat& Q, int dst_cn, Stream& stream)
void cv::gpu::reprojectImageTo3D(InputArray _disp, OutputArray _xyz, InputArray _Q, int dst_cn, Stream& stream)
{
using namespace cv::gpu::cudev;
@ -72,11 +72,15 @@ void cv::gpu::reprojectImageTo3D(const GpuMat& disp, GpuMat& xyz, const Mat& Q,
{reprojectImageTo3D_gpu<uchar, float4>, 0, 0, reprojectImageTo3D_gpu<short, float4>}
};
CV_Assert(disp.type() == CV_8U || disp.type() == CV_16S);
CV_Assert(Q.type() == CV_32F && Q.rows == 4 && Q.cols == 4 && Q.isContinuous());
CV_Assert(dst_cn == 3 || dst_cn == 4);
GpuMat disp = _disp.getGpuMat();
Mat Q = _Q.getMat();
xyz.create(disp.size(), CV_MAKE_TYPE(CV_32F, dst_cn));
CV_Assert( disp.type() == CV_8U || disp.type() == CV_16S );
CV_Assert( Q.type() == CV_32F && Q.rows == 4 && Q.cols == 4 && Q.isContinuous() );
CV_Assert( dst_cn == 3 || dst_cn == 4 );
_xyz.create(disp.size(), CV_MAKE_TYPE(CV_32F, dst_cn));
GpuMat xyz = _xyz.getGpuMat();
funcs[dst_cn == 4][disp.type()](disp, xyz, Q.ptr<float>(), StreamAccessor::getStream(stream));
}
@ -93,23 +97,25 @@ namespace cv { namespace gpu { namespace cudev
namespace
{
template <typename T>
void drawColorDisp_caller(const GpuMat& src, GpuMat& dst, int ndisp, const cudaStream_t& stream)
void drawColorDisp_caller(const GpuMat& src, OutputArray _dst, int ndisp, const cudaStream_t& stream)
{
using namespace ::cv::gpu::cudev;
dst.create(src.size(), CV_8UC4);
_dst.create(src.size(), CV_8UC4);
GpuMat dst = _dst.getGpuMat();
drawColorDisp_gpu((PtrStepSz<T>)src, dst, ndisp, stream);
}
typedef void (*drawColorDisp_caller_t)(const GpuMat& src, GpuMat& dst, int ndisp, const cudaStream_t& stream);
const drawColorDisp_caller_t drawColorDisp_callers[] = {drawColorDisp_caller<unsigned char>, 0, 0, drawColorDisp_caller<short>, 0, 0, 0, 0};
}
void cv::gpu::drawColorDisp(const GpuMat& src, GpuMat& dst, int ndisp, Stream& stream)
void cv::gpu::drawColorDisp(InputArray _src, OutputArray dst, int ndisp, Stream& stream)
{
CV_Assert(src.type() == CV_8U || src.type() == CV_16S);
typedef void (*drawColorDisp_caller_t)(const GpuMat& src, OutputArray dst, int ndisp, const cudaStream_t& stream);
const drawColorDisp_caller_t drawColorDisp_callers[] = {drawColorDisp_caller<unsigned char>, 0, 0, drawColorDisp_caller<short>, 0, 0, 0, 0};
GpuMat src = _src.getGpuMat();
CV_Assert( src.type() == CV_8U || src.type() == CV_16S );
drawColorDisp_callers[src.type()](src, dst, ndisp, StreamAccessor::getStream(stream));
}

@ -71,10 +71,10 @@ GPU_TEST_P(StereoBM, Regression)
ASSERT_FALSE(right_image.empty());
ASSERT_FALSE(disp_gold.empty());
cv::gpu::StereoBM_GPU bm(0, 128, 19);
cv::Ptr<cv::StereoBM> bm = cv::gpu::createStereoBM(128, 19);
cv::gpu::GpuMat disp;
bm(loadMat(left_image), loadMat(right_image), disp);
bm->compute(loadMat(left_image), loadMat(right_image), disp);
EXPECT_MAT_NEAR(disp_gold, disp, 0.0);
}
@ -106,10 +106,15 @@ GPU_TEST_P(StereoBeliefPropagation, Regression)
ASSERT_FALSE(right_image.empty());
ASSERT_FALSE(disp_gold.empty());
cv::gpu::StereoBeliefPropagation bp(64, 8, 2, 25, 0.1f, 15, 1, CV_16S);
cv::Ptr<cv::gpu::StereoBeliefPropagation> bp = cv::gpu::createStereoBeliefPropagation(64, 8, 2, CV_16S);
bp->setMaxDataTerm(25.0);
bp->setDataWeight(0.1);
bp->setMaxDiscTerm(15.0);
bp->setDiscSingleJump(1.0);
cv::gpu::GpuMat disp;
bp(loadMat(left_image), loadMat(right_image), disp);
bp->compute(loadMat(left_image), loadMat(right_image), disp);
cv::Mat h_disp(disp);
h_disp.convertTo(h_disp, disp_gold.depth());
@ -150,10 +155,10 @@ GPU_TEST_P(StereoConstantSpaceBP, Regression)
ASSERT_FALSE(right_image.empty());
ASSERT_FALSE(disp_gold.empty());
cv::gpu::StereoConstantSpaceBP csbp(128, 16, 4, 4);
cv::Ptr<cv::gpu::StereoConstantSpaceBP> csbp = cv::gpu::createStereoConstantSpaceBP(128, 16, 4, 4);
cv::gpu::GpuMat disp;
csbp(loadMat(left_image), loadMat(right_image), disp);
csbp->compute(loadMat(left_image), loadMat(right_image), disp);
cv::Mat h_disp(disp);
h_disp.convertTo(h_disp, disp_gold.depth());

@ -85,7 +85,7 @@ void inline contextOff()
// GPUs data
GpuMat d_left[2];
GpuMat d_right[2];
StereoBM_GPU* bm[2];
Ptr<gpu::StereoBM> bm[2];
GpuMat d_result[2];
static void printHelp()
@ -162,14 +162,14 @@ int main(int argc, char** argv)
contextOn(0);
d_left[0].upload(left.rowRange(0, left.rows / 2));
d_right[0].upload(right.rowRange(0, right.rows / 2));
bm[0] = new StereoBM_GPU();
bm[0] = gpu::createStereoBM();
contextOff();
// Split source images for processing on the GPU #1
contextOn(1);
d_left[1].upload(left.rowRange(left.rows / 2, left.rows));
d_right[1].upload(right.rowRange(right.rows / 2, right.rows));
bm[1] = new StereoBM_GPU();
bm[1] = gpu::createStereoBM();
contextOff();
// Execute calculation in two threads using two GPUs
@ -182,7 +182,7 @@ int main(int argc, char** argv)
d_left[0].release();
d_right[0].release();
d_result[0].release();
delete bm[0];
bm[0].release();
contextOff();
// Release the second GPU resources
@ -191,7 +191,7 @@ int main(int argc, char** argv)
d_left[1].release();
d_right[1].release();
d_result[1].release();
delete bm[1];
bm[1].release();
contextOff();
waitKey();
@ -204,8 +204,7 @@ void Worker::operator()(int device_id) const
{
contextOn(device_id);
bm[device_id]->operator()(d_left[device_id], d_right[device_id],
d_result[device_id]);
bm[device_id]->compute(d_left[device_id], d_right[device_id], d_result[device_id]);
std::cout << "GPU #" << device_id << " (" << DeviceInfo().name()
<< "): finished\n";

@ -65,9 +65,9 @@ private:
Mat left, right;
gpu::GpuMat d_left, d_right;
gpu::StereoBM_GPU bm;
gpu::StereoBeliefPropagation bp;
gpu::StereoConstantSpaceBP csbp;
Ptr<gpu::StereoBM> bm;
Ptr<gpu::StereoBeliefPropagation> bp;
Ptr<gpu::StereoConstantSpaceBP> csbp;
int64 work_begin;
double work_fps;
@ -172,9 +172,9 @@ void App::run()
imshow("right", right);
// Set common parameters
bm.ndisp = p.ndisp;
bp.ndisp = p.ndisp;
csbp.ndisp = p.ndisp;
bm = gpu::createStereoBM(p.ndisp);
bp = gpu::createStereoBeliefPropagation(p.ndisp);
csbp = cv::gpu::createStereoConstantSpaceBP(p.ndisp);
// Prepare disparity map of specified type
Mat disp(left.size(), CV_8U);
@ -201,10 +201,10 @@ void App::run()
imshow("left", left);
imshow("right", right);
}
bm(d_left, d_right, d_disp);
bm->compute(d_left, d_right, d_disp);
break;
case Params::BP: bp(d_left, d_right, d_disp); break;
case Params::CSBP: csbp(d_left, d_right, d_disp); break;
case Params::BP: bp->compute(d_left, d_right, d_disp); break;
case Params::CSBP: csbp->compute(d_left, d_right, d_disp); break;
}
workEnd();
@ -228,16 +228,16 @@ void App::printParams() const
switch (p.method)
{
case Params::BM:
cout << "win_size: " << bm.winSize << endl;
cout << "prefilter_sobel: " << bm.preset << endl;
cout << "win_size: " << bm->getBlockSize() << endl;
cout << "prefilter_sobel: " << bm->getPreFilterType() << endl;
break;
case Params::BP:
cout << "iter_count: " << bp.iters << endl;
cout << "level_count: " << bp.levels << endl;
cout << "iter_count: " << bp->getNumIters() << endl;
cout << "level_count: " << bp->getNumLevels() << endl;
break;
case Params::CSBP:
cout << "iter_count: " << csbp.iters << endl;
cout << "level_count: " << csbp.levels << endl;
cout << "iter_count: " << csbp->getNumIters() << endl;
cout << "level_count: " << csbp->getNumLevels() << endl;
break;
}
cout << endl;
@ -289,92 +289,92 @@ void App::handleKey(char key)
case 's': case 'S':
if (p.method == Params::BM)
{
switch (bm.preset)
switch (bm->getPreFilterType())
{
case gpu::StereoBM_GPU::BASIC_PRESET:
bm.preset = gpu::StereoBM_GPU::PREFILTER_XSOBEL;
case 0:
bm->setPreFilterType(cv::StereoBM::PREFILTER_XSOBEL);
break;
case gpu::StereoBM_GPU::PREFILTER_XSOBEL:
bm.preset = gpu::StereoBM_GPU::BASIC_PRESET;
case cv::StereoBM::PREFILTER_XSOBEL:
bm->setPreFilterType(0);
break;
}
cout << "prefilter_sobel: " << bm.preset << endl;
cout << "prefilter_sobel: " << bm->getPreFilterType() << endl;
}
break;
case '1':
p.ndisp = p.ndisp == 1 ? 8 : p.ndisp + 8;
cout << "ndisp: " << p.ndisp << endl;
bm.ndisp = p.ndisp;
bp.ndisp = p.ndisp;
csbp.ndisp = p.ndisp;
bm->setNumDisparities(p.ndisp);
bp->setNumDisparities(p.ndisp);
csbp->setNumDisparities(p.ndisp);
break;
case 'q': case 'Q':
p.ndisp = max(p.ndisp - 8, 1);
cout << "ndisp: " << p.ndisp << endl;
bm.ndisp = p.ndisp;
bp.ndisp = p.ndisp;
csbp.ndisp = p.ndisp;
bm->setNumDisparities(p.ndisp);
bp->setNumDisparities(p.ndisp);
csbp->setNumDisparities(p.ndisp);
break;
case '2':
if (p.method == Params::BM)
{
bm.winSize = min(bm.winSize + 1, 51);
cout << "win_size: " << bm.winSize << endl;
bm->setBlockSize(min(bm->getBlockSize() + 1, 51));
cout << "win_size: " << bm->getBlockSize() << endl;
}
break;
case 'w': case 'W':
if (p.method == Params::BM)
{
bm.winSize = max(bm.winSize - 1, 2);
cout << "win_size: " << bm.winSize << endl;
bm->setBlockSize(max(bm->getBlockSize() - 1, 2));
cout << "win_size: " << bm->getBlockSize() << endl;
}
break;
case '3':
if (p.method == Params::BP)
{
bp.iters += 1;
cout << "iter_count: " << bp.iters << endl;
bp->setNumIters(bp->getNumIters() + 1);
cout << "iter_count: " << bp->getNumIters() << endl;
}
else if (p.method == Params::CSBP)
{
csbp.iters += 1;
cout << "iter_count: " << csbp.iters << endl;
csbp->setNumIters(csbp->getNumIters() + 1);
cout << "iter_count: " << csbp->getNumIters() << endl;
}
break;
case 'e': case 'E':
if (p.method == Params::BP)
{
bp.iters = max(bp.iters - 1, 1);
cout << "iter_count: " << bp.iters << endl;
bp->setNumIters(max(bp->getNumIters() - 1, 1));
cout << "iter_count: " << bp->getNumIters() << endl;
}
else if (p.method == Params::CSBP)
{
csbp.iters = max(csbp.iters - 1, 1);
cout << "iter_count: " << csbp.iters << endl;
csbp->setNumIters(max(csbp->getNumIters() - 1, 1));
cout << "iter_count: " << csbp->getNumIters() << endl;
}
break;
case '4':
if (p.method == Params::BP)
{
bp.levels += 1;
cout << "level_count: " << bp.levels << endl;
bp->setNumLevels(bp->getNumLevels() + 1);
cout << "level_count: " << bp->getNumLevels() << endl;
}
else if (p.method == Params::CSBP)
{
csbp.levels += 1;
cout << "level_count: " << csbp.levels << endl;
csbp->setNumLevels(csbp->getNumLevels() + 1);
cout << "level_count: " << csbp->getNumLevels() << endl;
}
break;
case 'r': case 'R':
if (p.method == Params::BP)
{
bp.levels = max(bp.levels - 1, 1);
cout << "level_count: " << bp.levels << endl;
bp->setNumLevels(max(bp->getNumLevels() - 1, 1));
cout << "level_count: " << bp->getNumLevels() << endl;
}
else if (p.method == Params::CSBP)
{
csbp.levels = max(csbp.levels - 1, 1);
cout << "level_count: " << csbp.levels << endl;
csbp->setNumLevels(max(csbp->getNumLevels() - 1, 1));
cout << "level_count: " << csbp->getNumLevels() << endl;
}
break;
}

@ -51,7 +51,7 @@ struct Worker { void operator()(int device_id) const; };
// GPUs data
GpuMat d_left[2];
GpuMat d_right[2];
StereoBM_GPU* bm[2];
Ptr<gpu::StereoBM> bm[2];
GpuMat d_result[2];
static void printHelp()
@ -112,13 +112,13 @@ int main(int argc, char** argv)
setDevice(0);
d_left[0].upload(left.rowRange(0, left.rows / 2));
d_right[0].upload(right.rowRange(0, right.rows / 2));
bm[0] = new StereoBM_GPU();
bm[0] = gpu::createStereoBM();
// Split source images for processing on the GPU #1
setDevice(1);
d_left[1].upload(left.rowRange(left.rows / 2, left.rows));
d_right[1].upload(right.rowRange(right.rows / 2, right.rows));
bm[1] = new StereoBM_GPU();
bm[1] = gpu::createStereoBM();
// Execute calculation in two threads using two GPUs
int devices[] = {0, 1};
@ -130,7 +130,7 @@ int main(int argc, char** argv)
d_left[0].release();
d_right[0].release();
d_result[0].release();
delete bm[0];
bm[0].release();
// Release the second GPU resources
setDevice(1);
@ -138,7 +138,7 @@ int main(int argc, char** argv)
d_left[1].release();
d_right[1].release();
d_result[1].release();
delete bm[1];
bm[1].release();
waitKey();
return 0;
@ -149,8 +149,7 @@ void Worker::operator()(int device_id) const
{
setDevice(device_id);
bm[device_id]->operator()(d_left[device_id], d_right[device_id],
d_result[device_id]);
bm[device_id]->compute(d_left[device_id], d_right[device_id], d_result[device_id]);
std::cout << "GPU #" << device_id << " (" << DeviceInfo().name()
<< "): finished\n";

Loading…
Cancel
Save