pyrlk.cpp 10.9 KB
Newer Older
yao's avatar
yao committed
1 2 3 4 5 6 7 8 9 10 11 12
/*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
//
yao's avatar
yao committed
13 14
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
yao's avatar
yao committed
15 16
// Third party copyrights are property of their respective owners.
//
yao's avatar
yao committed
17
// @Authors
yao's avatar
yao committed
18 19
//      Dachuan Zhao, dachuan@multicorewareinc.com
//      Yao Wang, bitwangyaoyao@gmail.com
yao's avatar
yao committed
20 21
//      Nathan, liujun@multicorewareinc.com
//
yao's avatar
yao committed
22 23 24 25 26 27 28 29
// 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
Andrey Pavlenko's avatar
Andrey Pavlenko committed
30
//     and/or other materials provided with the distribution.
yao's avatar
yao committed
31 32 33 34 35
//
//   * 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
yao's avatar
yao committed
36
// any express or implied warranties, including, but not limited to, the implied
yao's avatar
yao committed
37 38 39 40 41 42 43 44 45 46 47 48
// 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"
49
#include "opencl_kernels.hpp"
50

yao's avatar
yao committed
51 52 53 54 55 56 57 58
using namespace cv;
using namespace cv::ocl;

struct dim3
{
    unsigned int x, y, z;
};

yao's avatar
yao committed
59
static void calcPatchSize(cv::Size winSize, int cn, dim3 &block, dim3 &patch, bool isDeviceArch11)
60 61
{
    winSize.width *= cn;
yao's avatar
yao committed
62

63 64 65 66 67 68 69 70 71 72
    if (winSize.width > 32 && winSize.width > 2 * winSize.height)
    {
        block.x = isDeviceArch11 ? 16 : 32;
        block.y = 8;
    }
    else
    {
        block.x = 16;
        block.y = isDeviceArch11 ? 8 : 16;
    }
yao's avatar
yao committed
73

74 75
    patch.x = (winSize.width  + block.x - 1) / block.x;
    patch.y = (winSize.height + block.y - 1) / block.y;
yao's avatar
yao committed
76

77 78
    block.z = patch.z = 1;
}
79

80
static void lkSparse_run(oclMat &I, oclMat &J,
81 82
                         const oclMat &prevPts, oclMat &nextPts, oclMat &status, oclMat& err, bool /*GET_MIN_EIGENVALS*/, int ptcount,
                         int level, dim3 patch, Size winSize, int iters)
yao's avatar
yao committed
83 84 85
{
    Context  *clCxt = I.clCxt;
    string kernelName = "lkSparse";
86 87
    size_t localThreads[3]  = { 8, 8, 1 };
    size_t globalThreads[3] = { 8 * ptcount, 8, 1};
88
    int cn = I.oclchannels();
yao's avatar
yao committed
89
    char calcErr = level==0?1:0;
yao's avatar
yao committed
90 91

    vector<pair<size_t , const void *> > args;
92

93 94
    cl_mem ITex = bindTexture(I);
    cl_mem JTex = bindTexture(J);
yao's avatar
yao committed
95 96 97 98 99 100 101 102

    args.push_back( make_pair( sizeof(cl_mem), (void *)&ITex ));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&JTex ));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&prevPts.data ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&prevPts.step ));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&nextPts.data ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&nextPts.step ));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&status.data ));
103
    args.push_back( make_pair( sizeof(cl_mem), (void *)&err.data ));
yao's avatar
yao committed
104 105 106 107 108 109 110 111 112 113 114
    args.push_back( make_pair( sizeof(cl_int), (void *)&level ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&I.rows ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&I.cols ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&patch.x ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&patch.y ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&cn ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.width ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.height ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&iters ));
    args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr ));

115
    bool is_cpu = isCpuDevice();
116
    if (is_cpu)
117
    {
118
        openCLExecuteKernel(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), (char*)" -D CPU");
119 120 121
    }
    else
    {
122 123 124 125 126 127 128 129 130 131 132
        stringstream idxStr;
        idxStr << kernelName << "_C" << I.oclchannels() << "_D" << I.depth();
        cl_kernel kernel = openCLGetKernelFromSource(clCxt, &pyrlk, idxStr.str());
        int wave_size = (int)queryWaveFrontSize(kernel);
        openCLSafeCall(clReleaseKernel(kernel));

        static char opt[32] = {0};
        sprintf(opt, "-D WAVE_SIZE=%d", wave_size);

        openCLExecuteKernel(clCxt, &pyrlk, kernelName, globalThreads, localThreads,
                            args, I.oclchannels(), I.depth(), opt);
133
    }
134 135
    releaseTexture(ITex);
    releaseTexture(JTex);
yao's avatar
yao committed
136 137
}

138
void cv::ocl::PyrLKOpticalFlow::sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts, oclMat &status, oclMat *err)
yao's avatar
yao committed
139 140 141 142 143
{
    if (prevPts.empty())
    {
        nextPts.release();
        status.release();
144
        if (err) err->release();
yao's avatar
yao committed
145 146 147 148 149 150 151
        return;
    }

    derivLambda = std::min(std::max(derivLambda, 0.0), 1.0);

    iters = std::min(std::max(iters, 0), 100);

152
    const int cn = prevImg.oclchannels();
yao's avatar
yao committed
153 154

    dim3 block, patch;
155
    calcPatchSize(winSize, cn, block, patch, isDeviceArch11_);
yao's avatar
yao committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169

    CV_Assert(derivLambda >= 0);
    CV_Assert(maxLevel >= 0 && winSize.width > 2 && winSize.height > 2);
    CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type());
    CV_Assert(patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6);
    CV_Assert(prevPts.rows == 1 && prevPts.type() == CV_32FC2);

    if (useInitialFlow)
        CV_Assert(nextPts.size() == prevPts.size() && nextPts.type() == CV_32FC2);
    else
        ensureSizeIsEnough(1, prevPts.cols, prevPts.type(), nextPts);

    oclMat temp1 = (useInitialFlow ? nextPts : prevPts).reshape(1);
    oclMat temp2 = nextPts.reshape(1);
yao's avatar
yao committed
170
    multiply(1.0f/(1<<maxLevel)/2.0f, temp1, temp2);
yao's avatar
yao committed
171 172

    ensureSizeIsEnough(1, prevPts.cols, CV_8UC1, status);
173
    status.setTo(Scalar::all(1));
yao's avatar
yao committed
174

175 176 177 178 179 180 181 182
    bool errMat = false;
    if (!err)
    {
        err = new oclMat(1, prevPts.cols, CV_32FC1);
        errMat = true;
    }
    else
        ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err);
yao's avatar
yao committed
183 184 185 186 187 188 189

    // build the image pyramids.
    prevPyr_.resize(maxLevel + 1);
    nextPyr_.resize(maxLevel + 1);

    if (cn == 1 || cn == 4)
    {
190 191
        prevImg.convertTo(prevPyr_[0], CV_32F);
        nextImg.convertTo(nextPyr_[0], CV_32F);
yao's avatar
yao committed
192 193 194 195
    }

    for (int level = 1; level <= maxLevel; ++level)
    {
196 197
        pyrDown(prevPyr_[level - 1], prevPyr_[level]);
        pyrDown(nextPyr_[level - 1], nextPyr_[level]);
yao's avatar
yao committed
198 199 200 201 202
    }

    // dI/dx ~ Ix, dI/dy ~ Iy
    for (int level = maxLevel; level >= 0; level--)
    {
203
        lkSparse_run(prevPyr_[level], nextPyr_[level],
204
                     prevPts, nextPts, status, *err, getMinEigenVals, prevPts.cols,
205
                     level, patch, winSize, iters);
yao's avatar
yao committed
206
    }
207

208 209
    if(errMat)
        delete err;
yao's avatar
yao committed
210 211
}

212
static void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v,
213
                        oclMat &prevU, oclMat &prevV, oclMat *err, Size winSize, int iters)
yao's avatar
yao committed
214 215 216 217 218 219 220 221
{
    Context  *clCxt = I.clCxt;

    string kernelName = "lkDense";

    size_t localThreads[3]  = { 16, 16, 1 };
    size_t globalThreads[3] = { I.cols, I.rows, 1};

222
    cl_char calcErr = err ? 1 : 0;
yao's avatar
yao committed
223

224 225 226
    cl_mem ITex;
    cl_mem JTex;

227 228
    ITex = bindTexture(I);
    JTex = bindTexture(J);
yao's avatar
yao committed
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

    vector<pair<size_t , const void *> > args;

    args.push_back( make_pair( sizeof(cl_mem), (void *)&ITex ));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&JTex ));

    args.push_back( make_pair( sizeof(cl_mem), (void *)&u.data ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&u.step ));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&v.data ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&v.step ));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&prevU.data ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&prevU.step ));
    args.push_back( make_pair( sizeof(cl_mem), (void *)&prevV.data ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&prevV.step ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&I.rows ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&I.cols ));
245 246
    args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.width ));
    args.push_back( make_pair( sizeof(cl_int), (void *)&winSize.height ));
yao's avatar
yao committed
247 248 249
    args.push_back( make_pair( sizeof(cl_int), (void *)&iters ));
    args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr ));

250
    openCLExecuteKernel(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth());
251

252 253
    releaseTexture(ITex);
    releaseTexture(JTex);
yao's avatar
yao committed
254 255
}

256
void cv::ocl::PyrLKOpticalFlow::dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err)
yao's avatar
yao committed
257 258 259 260 261 262 263 264 265 266 267 268 269
{
    CV_Assert(prevImg.type() == CV_8UC1);
    CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type());
    CV_Assert(maxLevel >= 0);
    CV_Assert(winSize.width > 2 && winSize.height > 2);

    if (err)
        err->create(prevImg.size(), CV_32FC1);

    prevPyr_.resize(maxLevel + 1);
    nextPyr_.resize(maxLevel + 1);

    prevPyr_[0] = prevImg;
270
    nextImg.convertTo(nextPyr_[0], CV_32F);
yao's avatar
yao committed
271 272 273

    for (int level = 1; level <= maxLevel; ++level)
    {
274 275
        pyrDown(prevPyr_[level - 1], prevPyr_[level]);
        pyrDown(nextPyr_[level - 1], nextPyr_[level]);
yao's avatar
yao committed
276 277 278 279 280 281
    }

    ensureSizeIsEnough(prevImg.size(), CV_32FC1, uPyr_[0]);
    ensureSizeIsEnough(prevImg.size(), CV_32FC1, vPyr_[0]);
    ensureSizeIsEnough(prevImg.size(), CV_32FC1, uPyr_[1]);
    ensureSizeIsEnough(prevImg.size(), CV_32FC1, vPyr_[1]);
282 283
    uPyr_[1].setTo(Scalar::all(0));
    vPyr_[1].setTo(Scalar::all(0));
yao's avatar
yao committed
284

285
    Size winSize2i(winSize.width, winSize.height);
yao's avatar
yao committed
286 287 288 289 290 291 292 293

    int idx = 0;

    for (int level = maxLevel; level >= 0; level--)
    {
        int idx2 = (idx + 1) & 1;

        lkDense_run(prevPyr_[level], nextPyr_[level], uPyr_[idx], vPyr_[idx], uPyr_[idx2], vPyr_[idx2],
294
                    level == 0 ? err : 0, winSize2i, iters);
yao's avatar
yao committed
295 296 297 298 299

        if (level > 0)
            idx = idx2;
    }

300 301
    uPyr_[idx].copyTo(u);
    vPyr_[idx].copyTo(v);
yao's avatar
yao committed
302
}