stereobm.cpp 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other GpuMaterials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

using namespace cv;
using namespace cv::gpu;
47 48 49 50

#if !defined (HAVE_CUDA)

cv::gpu::StereoBM_GPU::StereoBM_GPU() { throw_nogpu(); }
51
cv::gpu::StereoBM_GPU::StereoBM_GPU(int, int, int) { throw_nogpu(); }
52 53

bool cv::gpu::StereoBM_GPU::checkIfGpuCallReasonable() { throw_nogpu(); return false; }
54
void cv::gpu::StereoBM_GPU::operator() ( const GpuMat&, const GpuMat&, GpuMat&, Stream&) { throw_nogpu(); }
55 56

#else /* !defined (HAVE_CUDA) */
57

58 59 60
BEGIN_OPENCV_DEVICE_NAMESPACE

namespace stereobm
61
{
62 63 64 65 66 67
    void stereoBM_GPU(const DevMem2Db& left, const DevMem2Db& right, const DevMem2Db& disp, int ndisp, int winsz, const DevMem2D_<uint>& minSSD_buf, cudaStream_t & stream);
    void prefilter_xsobel(const DevMem2Db& input, const DevMem2Db& output, int prefilterCap /*= 31*/, cudaStream_t & stream);
    void postfilter_textureness(const DevMem2Db& input, int winsz, float avgTexturenessThreshold, const DevMem2Db& disp, cudaStream_t & stream);
}

END_OPENCV_DEVICE_NAMESPACE
68 69

const float defaultAvgTexThreshold = 3;
70 71

cv::gpu::StereoBM_GPU::StereoBM_GPU()
72 73 74
    : preset(BASIC_PRESET), ndisp(DEFAULT_NDISP), winSize(DEFAULT_WINSZ), avergeTexThreshold(defaultAvgTexThreshold)  
{
}
75

76
cv::gpu::StereoBM_GPU::StereoBM_GPU(int preset_, int ndisparities_, int winSize_)
A
Andrey Morozov 已提交
77
    : preset(preset_), ndisp(ndisparities_), winSize(winSize_), avergeTexThreshold(defaultAvgTexThreshold)
78
{
79
    const int max_supported_ndisp = 1 << (sizeof(unsigned char) * 8);
80
    CV_Assert(0 < ndisp && ndisp <= max_supported_ndisp);
81
    CV_Assert(ndisp % 8 == 0);
82
    CV_Assert(winSize % 2 == 1);
83 84 85 86 87 88 89
}

bool cv::gpu::StereoBM_GPU::checkIfGpuCallReasonable()
{
    if (0 == getCudaEnabledDeviceCount())
        return false;

90
    DeviceInfo device_info;
91

92
    if (device_info.majorVersion() > 1 || device_info.multiProcessorCount() > 16)
93 94
        return true;

95
    return false;
96
}
97

98
namespace
99
{
100 101 102
    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)
    {
        using namespace OPENCV_DEVICE_NAMESPACE_ stereobm;
103

104 105 106
        CV_DbgAssert(left.rows == right.rows && left.cols == right.cols);
        CV_DbgAssert(left.type() == CV_8UC1);
        CV_DbgAssert(right.type() == CV_8UC1);
107

108 109
        disparity.create(left.size(), CV_8U);
        minSSD.create(left.size(), CV_32S);
110

111 112
        GpuMat le_for_bm =  left;
        GpuMat ri_for_bm = right;
113

114 115 116 117
        if (preset == StereoBM_GPU::PREFILTER_XSOBEL)
        {
            leBuf.create( left.size(),  left.type());
            riBuf.create(right.size(), right.type());
118

119 120 121 122 123 124
		    prefilter_xsobel( left, leBuf, 31, stream);
            prefilter_xsobel(right, riBuf, 31, stream);

            le_for_bm = leBuf;
            ri_for_bm = riBuf;
        }
125

126
        stereoBM_GPU(le_for_bm, ri_for_bm, disparity, ndisp, winSize, minSSD, stream);
127

128 129 130
        if (avergeTexThreshold)
            postfilter_textureness(le_for_bm, winSize, avergeTexThreshold, disparity, stream);
    }
131
}
132

133
void cv::gpu::StereoBM_GPU::operator() ( const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream)
134
{
135
    stereo_bm_gpu_operator(minSSD, leBuf, riBuf, preset, ndisp, winSize, avergeTexThreshold, left, right, disparity, StreamAccessor::getStream(stream));
136 137
}

138
#endif /* !defined (HAVE_CUDA) */