stereobm.cpp 9.9 KB
Newer Older
Y
yao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
Y
yao 已提交
15
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
Y
yao 已提交
16 17 18 19
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Jia Haipeng, jiahaipeng95@gmail.com
Y
yao 已提交
20
//    Xiaopeng Fu, xiaopeng@multicorewareinc.com
Y
yao 已提交
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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
//
// 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 oclMaterials 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"
#include <vector>

using namespace cv;
using namespace cv::ocl;
using namespace std;


namespace cv
{
namespace ocl
{

///////////////////////////OpenCL kernel strings///////////////////////////
extern const char *stereobm;

}
}
namespace cv
{
namespace ocl
{
namespace stereoBM
{
/////////////////////////////////////////////////////////////////////////
//////////////////////////prefilter_xsbel////////////////////////////////
////////////////////////////////////////////////////////////////////////
Y
yao 已提交
75
static void prefilter_xsobel(const oclMat &input, oclMat &output, int prefilterCap)
Y
yao 已提交
76 77 78 79 80 81 82
{
    string kernelName = "prefilter_xsobel";

    size_t blockSize = 1;
    size_t globalThreads[3] = { input.cols, input.rows, 1 };
    size_t localThreads[3]  = { blockSize, blockSize, 1 };

P
peng xiao 已提交
83
    std::vector< std::pair<size_t, const void *> > args;
P
peng xiao 已提交
84 85 86 87 88
    args.push_back(std::make_pair(sizeof(cl_mem), (void *)&input.data));
    args.push_back(std::make_pair(sizeof(cl_mem), (void *)&output.data));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&input.rows));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&input.cols));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&prefilterCap));
Y
yao 已提交
89

P
peng xiao 已提交
90 91
    openCLExecuteKernel(Context::getContext(), &stereobm, kernelName,
        globalThreads, localThreads, args, -1, -1);
Y
yao 已提交
92 93 94 95 96 97 98 99 100 101 102 103 104 105
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////common////////////////////////////////////
////////////////////////////////////////////////////////////////////////
#define N_DISPARITIES 8
#define ROWSperTHREAD 21
#define BLOCK_W 128
static inline int divUp(int total, int grain)
{
    return (total + grain - 1) / grain;
}
////////////////////////////////////////////////////////////////////////////
///////////////////////////////stereoBM_GPU////////////////////////////////
////////////////////////////////////////////////////////////////////////////
Y
yao 已提交
106
static void stereo_bm(const oclMat &left, const oclMat &right,  oclMat &disp,
Y
yao 已提交
107 108 109 110 111 112 113 114 115 116
               int maxdisp, int winSize,  oclMat &minSSD_buf)
{
    int winsz2 = winSize >> 1;

    string kernelName = "stereoKernel";

    disp.setTo(Scalar_<unsigned char>::all(0));
    minSSD_buf.setTo(Scalar_<unsigned int>::all(0xFFFFFFFF));

    size_t minssd_step = minSSD_buf.step / minSSD_buf.elemSize();
P
peng xiao 已提交
117
    size_t local_mem_size = (N_DISPARITIES * (BLOCK_W + 2 * winsz2)) *
Y
yao 已提交
118 119 120 121 122 123 124 125
                            sizeof(cl_uint);
    //size_t blockSize = 1;
    size_t localThreads[]  = { BLOCK_W, 1,1};
    size_t globalThreads[] = { divUp(left.cols - maxdisp - 2 * winsz2, BLOCK_W) *BLOCK_W,
                               divUp(left.rows - 2 * winsz2, ROWSperTHREAD),
                               1
                             };

P
peng xiao 已提交
126
    std::vector< std::pair<size_t, const void *> > args;
P
peng xiao 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
    args.push_back(std::make_pair(sizeof(cl_mem), (void *)&left.data));
    args.push_back(std::make_pair(sizeof(cl_mem), (void *)&right.data));
    args.push_back(std::make_pair(sizeof(cl_mem), (void *)&minSSD_buf.data));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&minssd_step));
    args.push_back(std::make_pair(sizeof(cl_mem), (void *)&disp.data));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&disp.step));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&left.cols));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&left.rows));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&left.step));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&maxdisp));
    args.push_back(std::make_pair(local_mem_size, (void *)NULL));

    char opt [128];
    sprintf(opt, "-D radius=%d", winsz2);
    openCLExecuteKernel(Context::getContext(), &stereobm, kernelName,
        globalThreads, localThreads, args, -1, -1, opt);
Y
yao 已提交
143 144 145 146
}
////////////////////////////////////////////////////////////////////////////
///////////////////////////////postfilter_textureness///////////////////////
////////////////////////////////////////////////////////////////////////////
Y
yao 已提交
147
static void postfilter_textureness(oclMat &left, int winSize,
Y
yao 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160
                            float avergeTexThreshold, oclMat &disparity)
{
    string kernelName = "textureness_kernel";

    size_t blockSize = 1;
    size_t localThreads[]  = { BLOCK_W, blockSize ,1};
    size_t globalThreads[] = { divUp(left.cols, BLOCK_W) *BLOCK_W,
                               divUp(left.rows, 2 * ROWSperTHREAD),
                               1
                             };

    size_t local_mem_size = (localThreads[0] + localThreads[0] + (winSize / 2) * 2) * sizeof(float);

P
peng xiao 已提交
161
    std::vector< std::pair<size_t, const void *> > args;
P
peng xiao 已提交
162 163 164 165 166 167 168 169 170 171 172 173
    args.push_back(std::make_pair(sizeof(cl_mem), (void *)&disparity.data));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&disparity.rows));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&disparity.cols));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&disparity.step));
    args.push_back(std::make_pair(sizeof(cl_mem), (void *)&left.data));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&left.rows));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&left.cols));
    args.push_back(std::make_pair(sizeof(cl_int), (void *)&winSize));
    args.push_back(std::make_pair(sizeof(cl_float), (void *)&avergeTexThreshold));
    args.push_back(std::make_pair(local_mem_size, (void*)NULL));
    openCLExecuteKernel(Context::getContext(), &stereobm, kernelName,
        globalThreads, localThreads, args, -1, -1);
Y
yao 已提交
174 175 176 177
}
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////operator/////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
Y
yao 已提交
178
static void operator_(oclMat &minSSD, oclMat &leBuf, oclMat &riBuf, int preset, int ndisp,
Y
yao 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
               int winSize, float avergeTexThreshold, const oclMat &left,
               const oclMat &right, oclMat &disparity)

{
    CV_DbgAssert(left.rows == right.rows && left.cols == right.cols);
    CV_DbgAssert(left.type() == CV_8UC1);
    CV_DbgAssert(right.type() == CV_8UC1);

    disparity.create(left.size(), CV_8UC1);
    minSSD.create(left.size(), CV_32SC1);

    oclMat le_for_bm =  left;
    oclMat ri_for_bm = right;

    if (preset == cv::ocl::StereoBM_OCL::PREFILTER_XSOBEL)
    {
        leBuf.create( left.size(),  left.type());
        riBuf.create(right.size(), right.type());

        prefilter_xsobel( left, leBuf, 31);
        prefilter_xsobel(right, riBuf, 31);

        le_for_bm = leBuf;
        ri_for_bm = riBuf;
    }

    stereo_bm(le_for_bm, ri_for_bm, disparity, ndisp, winSize, minSSD);

    if (avergeTexThreshold)
    {
        postfilter_textureness(le_for_bm, winSize, avergeTexThreshold, disparity);
    }
}
}
}
}
const float defaultAvgTexThreshold = 3;

cv::ocl::StereoBM_OCL::StereoBM_OCL()
    : preset(BASIC_PRESET), ndisp(DEFAULT_NDISP), winSize(DEFAULT_WINSZ),
      avergeTexThreshold(defaultAvgTexThreshold)  {}

cv::ocl::StereoBM_OCL::StereoBM_OCL(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);
}

bool cv::ocl::StereoBM_OCL::checkIfGpuCallReasonable()
{
    return true;
}

void cv::ocl::StereoBM_OCL::operator() ( const oclMat &left, const oclMat &right,
        oclMat &disparity)
{
    cv::ocl::stereoBM::operator_(minSSD, leBuf, riBuf, preset, ndisp, winSize, avergeTexThreshold, left, right, disparity);
}