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 45using namespace cv; 46using namespace cv::cuda; 47 48#if !defined (HAVE_CUDA) || defined (CUDA_DISABLER) 49 50Ptr<cuda::StereoBM> cv::cuda::createStereoBM(int, int) { throw_no_cuda(); return Ptr<cuda::StereoBM>(); } 51 52#else /* !defined (HAVE_CUDA) */ 53 54namespace 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 64namespace 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 74 int getMinDisparity() const { return 0; } 75 void setMinDisparity(int /*minDisparity*/) {} 76 77 int getNumDisparities() const { return ndisp_; } 78 void setNumDisparities(int numDisparities) { ndisp_ = numDisparities; } 79 80 int getBlockSize() const { return winSize_; } 81 void setBlockSize(int blockSize) { winSize_ = blockSize; } 82 83 int getSpeckleWindowSize() const { return 0; } 84 void setSpeckleWindowSize(int /*speckleWindowSize*/) {} 85 86 int getSpeckleRange() const { return 0; } 87 void setSpeckleRange(int /*speckleRange*/) {} 88 89 int getDisp12MaxDiff() const { return 0; } 90 void setDisp12MaxDiff(int /*disp12MaxDiff*/) {} 91 92 int getPreFilterType() const { return preset_; } 93 void setPreFilterType(int preFilterType) { preset_ = preFilterType; } 94 95 int getPreFilterSize() const { return 0; } 96 void setPreFilterSize(int /*preFilterSize*/) {} 97 98 int getPreFilterCap() const { return preFilterCap_; } 99 void setPreFilterCap(int preFilterCap) { preFilterCap_ = preFilterCap; } 100 101 int getTextureThreshold() const { return static_cast<int>(avergeTexThreshold_); } 102 void setTextureThreshold(int textureThreshold) { avergeTexThreshold_ = static_cast<float>(textureThreshold); } 103 104 int getUniquenessRatio() const { return 0; } 105 void setUniquenessRatio(int /*uniquenessRatio*/) {} 106 107 int getSmallerBlockSize() const { return 0; } 108 void setSmallerBlockSize(int /*blockSize*/){} 109 110 Rect getROI1() const { return Rect(); } 111 void setROI1(Rect /*roi1*/) {} 112 113 Rect getROI2() const { return 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 126 StereoBMImpl::StereoBMImpl(int numDisparities, int blockSize) 127 : preset_(0), ndisp_(numDisparities), winSize_(blockSize), preFilterCap_(31), avergeTexThreshold_(3) 128 { 129 } 130 131 void StereoBMImpl::compute(InputArray left, InputArray right, OutputArray disparity) 132 { 133 compute(left, right, disparity, Stream::Null()); 134 } 135 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 180Ptr<cuda::StereoBM> cv::cuda::createStereoBM(int numDisparities, int blockSize) 181{ 182 return makePtr<StereoBMImpl>(numDisparities, blockSize); 183} 184 185#endif /* !defined (HAVE_CUDA) */ 186