pyrlk.cpp 12.4 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
yao's avatar
yao committed
30
//     and/or other oclMaterials 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
// 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*/

yao's avatar
yao committed
48

yao's avatar
yao committed
49
#include "precomp.hpp"
50

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

namespace cv
{
57 58 59 60 61
namespace ocl
{
extern const char *pyrlk;
extern const char *pyrlk_no_image;
}
yao's avatar
yao committed
62 63 64 65 66 67
}
struct dim3
{
    unsigned int x, y, z;
};

yao's avatar
yao committed
68
static void calcPatchSize(cv::Size winSize, int cn, dim3 &block, dim3 &patch, bool isDeviceArch11)
69 70
{
    winSize.width *= cn;
yao's avatar
yao committed
71

72 73 74 75 76 77 78 79 80 81
    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
82

83 84
    patch.x = (winSize.width  + block.x - 1) / block.x;
    patch.y = (winSize.height + block.y - 1) / block.y;
yao's avatar
yao committed
85

86 87
    block.z = patch.z = 1;
}
88

89
static void lkSparse_run(oclMat &I, oclMat &J,
90 91
                         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
92 93
{
    Context  *clCxt = I.clCxt;
94
    int elemCntPerRow = I.step / I.elemSize();
yao's avatar
yao committed
95
    string kernelName = "lkSparse";
96 97 98
    bool isImageSupported = support_image2d();
    size_t localThreads[3]  = { 8, isImageSupported ? 8 : 32, 1 };
    size_t globalThreads[3] = { 8 * ptcount, isImageSupported ? 8 : 32, 1};
99
    int cn = I.oclchannels();
yao's avatar
yao committed
100
    char calcErr = level==0?1:0;
yao's avatar
yao committed
101 102

    vector<pair<size_t , const void *> > args;
103 104 105

    cl_mem ITex = isImageSupported ? bindTexture(I) : (cl_mem)I.data;
    cl_mem JTex = isImageSupported ? bindTexture(J) : (cl_mem)J.data;
yao's avatar
yao committed
106 107 108 109 110 111 112 113

    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 ));
114
    args.push_back( make_pair( sizeof(cl_mem), (void *)&err.data ));
yao's avatar
yao committed
115 116 117
    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 ));
118
    if (!isImageSupported)
119
        args.push_back( make_pair( sizeof(cl_int), (void *)&elemCntPerRow ) );
yao's avatar
yao committed
120 121 122 123 124 125 126 127
    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 ));

128
    bool is_cpu = queryDeviceInfo<IS_CPU_DEVICE, bool>();
129
    if (is_cpu)
130
    {
131
        openCLExecuteKernel(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth(), (char*)" -D CPU");
yao's avatar
yao committed
132 133
        releaseTexture(ITex);
        releaseTexture(JTex);
134 135 136
    }
    else
    {
137 138
        if(isImageSupported)
        {
yao's avatar
yao committed
139 140 141
            stringstream idxStr;
            idxStr << kernelName << "_C" << I.oclchannels() << "_D" << I.depth();
            cl_kernel kernel = openCLGetKernelFromSource(clCxt, &pyrlk, idxStr.str());
yao's avatar
yao committed
142
            int wave_size = queryDeviceInfo<WAVEFRONT_SIZE, int>(kernel);
yao's avatar
yao committed
143 144
            openCLSafeCall(clReleaseKernel(kernel));

yao's avatar
yao committed
145
            static char opt[32] = {0};
yao's avatar
yao committed
146 147 148 149
            sprintf(opt, " -D WAVE_SIZE=%d", wave_size);

            openCLExecuteKernel(clCxt, &pyrlk, kernelName, globalThreads, localThreads, 
                                args, I.oclchannels(), I.depth(), opt);
150 151 152 153 154 155 156
            releaseTexture(ITex);
            releaseTexture(JTex);
        }
        else
        {
            openCLExecuteKernel(clCxt, &pyrlk_no_image, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth());
        }
157
    }
yao's avatar
yao committed
158 159
}

160
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
161 162 163 164 165
{
    if (prevPts.empty())
    {
        nextPts.release();
        status.release();
166
        if (err) err->release();
yao's avatar
yao committed
167 168 169 170 171 172 173
        return;
    }

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

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

174
    const int cn = prevImg.oclchannels();
yao's avatar
yao committed
175 176

    dim3 block, patch;
177
    calcPatchSize(winSize, cn, block, patch, isDeviceArch11_);
yao's avatar
yao committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191

    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
192
    multiply(1.0f/(1<<maxLevel)/2.0f, temp1, temp2);
yao's avatar
yao committed
193 194

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

197 198 199 200 201 202 203 204
    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
205 206 207 208 209 210 211

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

    if (cn == 1 || cn == 4)
    {
212 213
        prevImg.convertTo(prevPyr_[0], CV_32F);
        nextImg.convertTo(nextPyr_[0], CV_32F);
yao's avatar
yao committed
214 215 216 217
    }

    for (int level = 1; level <= maxLevel; ++level)
    {
218 219
        pyrDown(prevPyr_[level - 1], prevPyr_[level]);
        pyrDown(nextPyr_[level - 1], nextPyr_[level]);
yao's avatar
yao committed
220 221 222 223 224
    }

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

230 231
    if(errMat)
        delete err;
yao's avatar
yao committed
232 233
}

234
static void lkDense_run(oclMat &I, oclMat &J, oclMat &u, oclMat &v,
235
                        oclMat &prevU, oclMat &prevV, oclMat *err, Size winSize, int iters)
yao's avatar
yao committed
236 237
{
    Context  *clCxt = I.clCxt;
238
    bool isImageSupported = support_image2d();
239
    int elemCntPerRow = I.step / I.elemSize();
yao's avatar
yao committed
240 241 242 243 244 245

    string kernelName = "lkDense";

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

246
    bool calcErr;
yao's avatar
yao committed
247 248
    if (err)
    {
249
        calcErr = true;
yao's avatar
yao committed
250 251 252
    }
    else
    {
253
        calcErr = false;
yao's avatar
yao committed
254 255
    }

256 257 258 259 260
    cl_mem ITex;
    cl_mem JTex;

    if (isImageSupported)
    {
yao's avatar
yao committed
261 262
        ITex = bindTexture(I);
        JTex = bindTexture(J);
263 264 265 266 267 268
    }
    else
    {
        ITex = (cl_mem)I.data;
        JTex = (cl_mem)J.data;
    }
yao's avatar
yao committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286

    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 ));
    //args.push_back( make_pair( sizeof(cl_mem), (void *)&(*err).data ));
    //args.push_back( make_pair( sizeof(cl_int), (void *)&(*err).step ));
287 288 289 290
    if (!isImageSupported)
    {
        args.push_back( make_pair( sizeof(cl_int), (void *)&elemCntPerRow ) );
    }
291 292
    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
293 294 295
    args.push_back( make_pair( sizeof(cl_int), (void *)&iters ));
    args.push_back( make_pair( sizeof(cl_char), (void *)&calcErr ));

296 297
    if (isImageSupported)
    {
298
        openCLExecuteKernel(clCxt, &pyrlk, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth());
299

300 301 302 303 304
        releaseTexture(ITex);
        releaseTexture(JTex);
    }
    else
    {
305
        openCLExecuteKernel(clCxt, &pyrlk_no_image, kernelName, globalThreads, localThreads, args, I.oclchannels(), I.depth());
306
    }
yao's avatar
yao committed
307 308
}

309
void cv::ocl::PyrLKOpticalFlow::dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err)
yao's avatar
yao committed
310 311 312 313 314 315 316 317 318 319 320 321 322
{
    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;
323
    nextImg.convertTo(nextPyr_[0], CV_32F);
yao's avatar
yao committed
324 325 326

    for (int level = 1; level <= maxLevel; ++level)
    {
327 328
        pyrDown(prevPyr_[level - 1], prevPyr_[level]);
        pyrDown(nextPyr_[level - 1], nextPyr_[level]);
yao's avatar
yao committed
329 330 331 332 333 334
    }

    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]);
335 336
    uPyr_[1].setTo(Scalar::all(0));
    vPyr_[1].setTo(Scalar::all(0));
yao's avatar
yao committed
337

338
    Size winSize2i(winSize.width, winSize.height);
yao's avatar
yao committed
339 340 341 342 343 344 345 346

    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],
347
                    level == 0 ? err : 0, winSize2i, iters);
yao's avatar
yao committed
348 349 350 351 352

        if (level > 0)
            idx = idx2;
    }

353 354
    uPyr_[idx].copyTo(u);
    vPyr_[idx].copyTo(v);
yao's avatar
yao committed
355
}