1
/*M///////////////////////////////////////////////////////////////////////////////////////
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
11
// For Open Source Computer Vision Library
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
43
#include "precomp.hpp"
46
using namespace cv::cuda;
48
#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
50
Ptr<cuda::StereoBM> cv::cuda::createStereoBM(int, int) { throw_no_cuda(); return Ptr<cuda::StereoBM>(); }
52
#else /* !defined (HAVE_CUDA) */
54
namespace cv { namespace cuda { namespace device
58
void stereoBM_CUDA(const PtrStepSzb& left, const PtrStepSzb& right, const PtrStepSzb& disp, int ndisp, int winsz, const PtrStepSz<unsigned int>& minSSD_buf, cudaStream_t & stream);
59
void prefilter_xsobel(const PtrStepSzb& input, const PtrStepSzb& output, int prefilterCap /*= 31*/, cudaStream_t & stream);
60
void postfilter_textureness(const PtrStepSzb& input, int winsz, float avgTexturenessThreshold, const PtrStepSzb& disp, cudaStream_t & stream);
66
class StereoBMImpl : public cuda::StereoBM
69
StereoBMImpl(int numDisparities, int blockSize);
71
void compute(InputArray left, InputArray right, OutputArray disparity);
72
void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream);
74
int getMinDisparity() const { return 0; }
75
void setMinDisparity(int /*minDisparity*/) {}
77
int getNumDisparities() const { return ndisp_; }
78
void setNumDisparities(int numDisparities) { ndisp_ = numDisparities; }
80
int getBlockSize() const { return winSize_; }
81
void setBlockSize(int blockSize) { winSize_ = blockSize; }
83
int getSpeckleWindowSize() const { return 0; }
84
void setSpeckleWindowSize(int /*speckleWindowSize*/) {}
86
int getSpeckleRange() const { return 0; }
87
void setSpeckleRange(int /*speckleRange*/) {}
89
int getDisp12MaxDiff() const { return 0; }
90
void setDisp12MaxDiff(int /*disp12MaxDiff*/) {}
92
int getPreFilterType() const { return preset_; }
93
void setPreFilterType(int preFilterType) { preset_ = preFilterType; }
95
int getPreFilterSize() const { return 0; }
96
void setPreFilterSize(int /*preFilterSize*/) {}
98
int getPreFilterCap() const { return preFilterCap_; }
99
void setPreFilterCap(int preFilterCap) { preFilterCap_ = preFilterCap; }
101
int getTextureThreshold() const { return static_cast<int>(avergeTexThreshold_); }
102
void setTextureThreshold(int textureThreshold) { avergeTexThreshold_ = static_cast<float>(textureThreshold); }
104
int getUniquenessRatio() const { return 0; }
105
void setUniquenessRatio(int /*uniquenessRatio*/) {}
107
int getSmallerBlockSize() const { return 0; }
108
void setSmallerBlockSize(int /*blockSize*/){}
110
Rect getROI1() const { return Rect(); }
111
void setROI1(Rect /*roi1*/) {}
113
Rect getROI2() const { return Rect(); }
114
void setROI2(Rect /*roi2*/) {}
121
float avergeTexThreshold_;
123
GpuMat minSSD_, leBuf_, riBuf_;
126
StereoBMImpl::StereoBMImpl(int numDisparities, int blockSize)
127
: preset_(0), ndisp_(numDisparities), winSize_(blockSize), preFilterCap_(31), avergeTexThreshold_(3)
131
void StereoBMImpl::compute(InputArray left, InputArray right, OutputArray disparity)
133
compute(left, right, disparity, Stream::Null());
136
void StereoBMImpl::compute(InputArray _left, InputArray _right, OutputArray _disparity, Stream& _stream)
138
using namespace ::cv::cuda::device::stereobm;
140
const int max_supported_ndisp = 1 << (sizeof(unsigned char) * 8);
141
CV_Assert( 0 < ndisp_ && ndisp_ <= max_supported_ndisp );
142
CV_Assert( ndisp_ % 8 == 0 );
143
CV_Assert( winSize_ % 2 == 1 );
145
GpuMat left = _left.getGpuMat();
146
GpuMat right = _right.getGpuMat();
148
CV_Assert( left.type() == CV_8UC1 );
149
CV_Assert( left.size() == right.size() && left.type() == right.type() );
151
_disparity.create(left.size(), CV_8UC1);
152
GpuMat disparity = _disparity.getGpuMat();
154
cudaStream_t stream = StreamAccessor::getStream(_stream);
156
cuda::ensureSizeIsEnough(left.size(), CV_32SC1, minSSD_);
158
PtrStepSzb le_for_bm = left;
159
PtrStepSzb ri_for_bm = right;
161
if (preset_ == cv::StereoBM::PREFILTER_XSOBEL)
163
cuda::ensureSizeIsEnough(left.size(), left.type(), leBuf_);
164
cuda::ensureSizeIsEnough(right.size(), right.type(), riBuf_);
166
prefilter_xsobel( left, leBuf_, preFilterCap_, stream);
167
prefilter_xsobel(right, riBuf_, preFilterCap_, stream);
173
stereoBM_CUDA(le_for_bm, ri_for_bm, disparity, ndisp_, winSize_, minSSD_, stream);
175
if (avergeTexThreshold_ > 0)
176
postfilter_textureness(le_for_bm, winSize_, avergeTexThreshold_, disparity, stream);
180
Ptr<cuda::StereoBM> cv::cuda::createStereoBM(int numDisparities, int blockSize)
182
return makePtr<StereoBMImpl>(numDisparities, blockSize);
185
#endif /* !defined (HAVE_CUDA) */