1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #include "precomp.hpp"
44
45 using namespace cv;
46 using namespace cv::cuda;
47
48 #if !defined (HAVE_CUDA) || defined (CUDA_DISABLER)
49
createStereoBM(int,int)50 Ptr<cuda::StereoBM> cv::cuda::createStereoBM(int, int) { throw_no_cuda(); return Ptr<cuda::StereoBM>(); }
51
52 #else /* !defined (HAVE_CUDA) */
53
54 namespace cv { namespace cuda { namespace device
55 {
56 namespace stereobm
57 {
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);
61 }
62 }}}
63
64 namespace
65 {
66 class StereoBMImpl : public cuda::StereoBM
67 {
68 public:
69 StereoBMImpl(int numDisparities, int blockSize);
70
71 void compute(InputArray left, InputArray right, OutputArray disparity);
72 void compute(InputArray left, InputArray right, OutputArray disparity, Stream& stream);
73
getMinDisparity() const74 int getMinDisparity() const { return 0; }
setMinDisparity(int)75 void setMinDisparity(int /*minDisparity*/) {}
76
getNumDisparities() const77 int getNumDisparities() const { return ndisp_; }
setNumDisparities(int numDisparities)78 void setNumDisparities(int numDisparities) { ndisp_ = numDisparities; }
79
getBlockSize() const80 int getBlockSize() const { return winSize_; }
setBlockSize(int blockSize)81 void setBlockSize(int blockSize) { winSize_ = blockSize; }
82
getSpeckleWindowSize() const83 int getSpeckleWindowSize() const { return 0; }
setSpeckleWindowSize(int)84 void setSpeckleWindowSize(int /*speckleWindowSize*/) {}
85
getSpeckleRange() const86 int getSpeckleRange() const { return 0; }
setSpeckleRange(int)87 void setSpeckleRange(int /*speckleRange*/) {}
88
getDisp12MaxDiff() const89 int getDisp12MaxDiff() const { return 0; }
setDisp12MaxDiff(int)90 void setDisp12MaxDiff(int /*disp12MaxDiff*/) {}
91
getPreFilterType() const92 int getPreFilterType() const { return preset_; }
setPreFilterType(int preFilterType)93 void setPreFilterType(int preFilterType) { preset_ = preFilterType; }
94
getPreFilterSize() const95 int getPreFilterSize() const { return 0; }
setPreFilterSize(int)96 void setPreFilterSize(int /*preFilterSize*/) {}
97
getPreFilterCap() const98 int getPreFilterCap() const { return preFilterCap_; }
setPreFilterCap(int preFilterCap)99 void setPreFilterCap(int preFilterCap) { preFilterCap_ = preFilterCap; }
100
getTextureThreshold() const101 int getTextureThreshold() const { return static_cast<int>(avergeTexThreshold_); }
setTextureThreshold(int textureThreshold)102 void setTextureThreshold(int textureThreshold) { avergeTexThreshold_ = static_cast<float>(textureThreshold); }
103
getUniquenessRatio() const104 int getUniquenessRatio() const { return 0; }
setUniquenessRatio(int)105 void setUniquenessRatio(int /*uniquenessRatio*/) {}
106
getSmallerBlockSize() const107 int getSmallerBlockSize() const { return 0; }
setSmallerBlockSize(int)108 void setSmallerBlockSize(int /*blockSize*/){}
109
getROI1() const110 Rect getROI1() const { return Rect(); }
setROI1(Rect)111 void setROI1(Rect /*roi1*/) {}
112
getROI2() const113 Rect getROI2() const { return Rect(); }
setROI2(Rect)114 void setROI2(Rect /*roi2*/) {}
115
116 private:
117 int preset_;
118 int ndisp_;
119 int winSize_;
120 int preFilterCap_;
121 float avergeTexThreshold_;
122
123 GpuMat minSSD_, leBuf_, riBuf_;
124 };
125
StereoBMImpl(int numDisparities,int blockSize)126 StereoBMImpl::StereoBMImpl(int numDisparities, int blockSize)
127 : preset_(0), ndisp_(numDisparities), winSize_(blockSize), preFilterCap_(31), avergeTexThreshold_(3)
128 {
129 }
130
compute(InputArray left,InputArray right,OutputArray disparity)131 void StereoBMImpl::compute(InputArray left, InputArray right, OutputArray disparity)
132 {
133 compute(left, right, disparity, Stream::Null());
134 }
135
compute(InputArray _left,InputArray _right,OutputArray _disparity,Stream & _stream)136 void StereoBMImpl::compute(InputArray _left, InputArray _right, OutputArray _disparity, Stream& _stream)
137 {
138 using namespace ::cv::cuda::device::stereobm;
139
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 );
144
145 GpuMat left = _left.getGpuMat();
146 GpuMat right = _right.getGpuMat();
147
148 CV_Assert( left.type() == CV_8UC1 );
149 CV_Assert( left.size() == right.size() && left.type() == right.type() );
150
151 _disparity.create(left.size(), CV_8UC1);
152 GpuMat disparity = _disparity.getGpuMat();
153
154 cudaStream_t stream = StreamAccessor::getStream(_stream);
155
156 cuda::ensureSizeIsEnough(left.size(), CV_32SC1, minSSD_);
157
158 PtrStepSzb le_for_bm = left;
159 PtrStepSzb ri_for_bm = right;
160
161 if (preset_ == cv::StereoBM::PREFILTER_XSOBEL)
162 {
163 cuda::ensureSizeIsEnough(left.size(), left.type(), leBuf_);
164 cuda::ensureSizeIsEnough(right.size(), right.type(), riBuf_);
165
166 prefilter_xsobel( left, leBuf_, preFilterCap_, stream);
167 prefilter_xsobel(right, riBuf_, preFilterCap_, stream);
168
169 le_for_bm = leBuf_;
170 ri_for_bm = riBuf_;
171 }
172
173 stereoBM_CUDA(le_for_bm, ri_for_bm, disparity, ndisp_, winSize_, minSSD_, stream);
174
175 if (avergeTexThreshold_ > 0)
176 postfilter_textureness(le_for_bm, winSize_, avergeTexThreshold_, disparity, stream);
177 }
178 }
179
createStereoBM(int numDisparities,int blockSize)180 Ptr<cuda::StereoBM> cv::cuda::createStereoBM(int numDisparities, int blockSize)
181 {
182 return makePtr<StereoBMImpl>(numDisparities, blockSize);
183 }
184
185 #endif /* !defined (HAVE_CUDA) */
186